Merge "Decode input and encode output"

This commit is contained in:
Jenkins
2013-02-18 18:58:14 +00:00
committed by Gerrit Code Review
8 changed files with 198 additions and 23 deletions
+36 -10
View File
@@ -26,24 +26,34 @@ from tests import utils
class TestClient(testtools.TestCase):
def setUp(self):
super(TestClient, self).setUp()
self.mock = mox.Mox()
self.mock.StubOutWithMock(httplib.HTTPConnection, 'request')
self.mock.StubOutWithMock(httplib.HTTPConnection, 'getresponse')
self.endpoint = 'http://example.com:9292'
self.client = http.HTTPClient(self.endpoint, token=u'abc123')
def tearDown(self):
super(TestClient, self).tearDown()
self.mock.UnsetStubs()
def test_connection_refused(self):
"""
Should receive a CommunicationError if connection refused.
And the error should list the host and port that refused the
connection
"""
endpoint = 'http://example.com:9292'
client = http.HTTPClient(endpoint, token=u'abc123')
m = mox.Mox()
m.StubOutWithMock(httplib.HTTPConnection, 'request')
httplib.HTTPConnection.request(
mox.IgnoreArg(),
mox.IgnoreArg(),
headers=mox.IgnoreArg(),
).AndRaise(socket.error())
m.ReplayAll()
self.mock.ReplayAll()
try:
client.json_request('GET', '/v1/images/detail?limit=20')
self.client.json_request('GET', '/v1/images/detail?limit=20')
#NOTE(alaski) We expect exc.CommunicationError to be raised
# so we should never reach this point. try/except is used here
# rather than assertRaises() so that we can check the body of
@@ -51,10 +61,26 @@ class TestClient(testtools.TestCase):
self.fail('An exception should have bypassed this line.')
except exc.CommunicationError, comm_err:
fail_msg = ("Exception message '%s' should contain '%s'" %
(comm_err.message, endpoint))
self.assertTrue(endpoint in comm_err.message, fail_msg)
finally:
m.UnsetStubs()
(comm_err.message, self.endpoint))
self.assertTrue(self.endpoint in comm_err.message, fail_msg)
def test_http_encoding(self):
httplib.HTTPConnection.request(
mox.IgnoreArg(),
mox.IgnoreArg(),
headers=mox.IgnoreArg())
# Lets fake the response
# returned by httplib
expected_response = 'Ok'
fake = utils.FakeResponse({}, StringIO.StringIO(expected_response))
httplib.HTTPConnection.getresponse().AndReturn(fake)
self.mock.ReplayAll()
headers = {"test": u'ni\xf1o'}
resp, body = self.client.raw_request('GET', '/v1/images/detail',
headers=headers)
self.assertEqual(resp, fake)
class TestResponseBodyIterator(testtools.TestCase):
+33
View File
@@ -50,3 +50,36 @@ class TestUtils(testtools.TestCase):
self.assertEqual("1MB", utils.make_size_human_readable(1048576))
self.assertEqual("1.4GB", utils.make_size_human_readable(1476395008))
self.assertEqual("9.3MB", utils.make_size_human_readable(9761280))
def test_ensure_unicode(self):
ensure_unicode = utils.ensure_unicode
self.assertEqual(u'True', ensure_unicode(True))
self.assertEqual(u'ni\xf1o', ensure_unicode("ni\xc3\xb1o",
incoming="utf-8"))
self.assertEqual(u"test", ensure_unicode("dGVzdA==",
incoming='base64'))
self.assertEqual(u"strange", ensure_unicode('\x80strange',
errors='ignore'))
self.assertEqual(u'\xc0', ensure_unicode('\xc0',
incoming='iso-8859-1'))
# Forcing incoming to ascii so it falls back to utf-8
self.assertEqual(u'ni\xf1o', ensure_unicode('ni\xc3\xb1o',
incoming='ascii'))
def test_ensure_str(self):
ensure_str = utils.ensure_str
self.assertEqual("True", ensure_str(True))
self.assertEqual("ni\xc3\xb1o", ensure_str(u'ni\xf1o',
encoding="utf-8"))
self.assertEqual("dGVzdA==\n", ensure_str("test",
encoding='base64'))
self.assertEqual('ni\xf1o', ensure_str("ni\xc3\xb1o",
encoding="iso-8859-1",
incoming="utf-8"))
# Forcing incoming to ascii so it falls back to utf-8
self.assertEqual('ni\xc3\xb1o', ensure_str('ni\xc3\xb1o',
incoming='ascii'))
+9 -2
View File
@@ -40,13 +40,20 @@ class FakeAPI(object):
class FakeResponse(object):
def __init__(self, headers, body=None):
def __init__(self, headers, body=None,
version=1.0, status=200, reason="Ok"):
"""
:param headers: dict representing HTTP response headers
:param body: file-like object
:param version: HTTP Version
:param status: Response status code
:param reason: Status code related message.
"""
self.headers = headers
self.body = body
self.status = status
self.reason = reason
self.version = version
self.headers = headers
def getheaders(self):
return copy.deepcopy(self.headers).items()