Skip to content

Commit

Permalink
Fixes to unit test errors.
Browse files Browse the repository at this point in the history
Addresses #98

Change set should make all unit tests pass under TravisCI
  • Loading branch information
avelis committed Jan 20, 2016
1 parent a96be0c commit d4c88b0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
15 changes: 8 additions & 7 deletions project/tests/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
DJANGO_META_CONTENT_TYPE = 'CONTENT_TYPE'
HTTP_CONTENT_TYPE = 'Content-Type'


class TestEncodingForRequests(TestCase):
"""
Check that the RequestModelFactory deals with encodings correctly via charset
Expand Down Expand Up @@ -121,32 +122,32 @@ def test_utf_json_encoded(self):
mock = Mock()
mock._headers = {HTTP_CONTENT_TYPE: 'application/json; charset=UTF-8'}
d = {'x': u'语'}
mock.content = json.dumps(d).encode('UTF-8')
mock.content = json.dumps(d)
mock.get = mock._headers.get
factory = ResponseModelFactory(mock)
body, content = factory.body()
self.assertDictEqual(json.loads(body), d)
self.assertEqual(content, mock.content.decode('UTF-8'))
self.assertEqual(content, mock.content)

def test_utf_json_encoded_no_charset(self):
"""default to UTF-8"""
mock = Mock()
mock._headers = {HTTP_CONTENT_TYPE: 'application/json'}
d = {'x': u'语'}
mock.content = json.dumps(d).encode('UTF-8')
mock.content = json.dumps(d)
mock.get = mock._headers.get
factory = ResponseModelFactory(mock)
body, content = factory.body()
self.assertDictEqual(json.loads(body), d)
self.assertEqual(content, mock.content.decode('UTF-8'))
self.assertEqual(content, mock.content)

def test_invalid_encoding_json(self):
mock = Mock()
mock._headers = {HTTP_CONTENT_TYPE: 'application/json; charset=asdas-8'}
d = {'x': u'语'}
mock.content = json.dumps(d).encode('UTF-8')
mock.content = json.dumps(d)
mock.get = mock._headers.get
factory = ResponseModelFactory(mock)
body, content = factory.body()
self.assertDictEqual(json.loads(body, encoding='UTF-8'), d)
self.assertEqual(mock.content.decode('UTF-8'), content)
self.assertDictEqual(json.loads(body), d)
self.assertEqual(mock.content, content)
4 changes: 4 additions & 0 deletions silk/model_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ def construct_response_model(self):
status_code=self.response.status_code,
encoded_headers=json.dumps(headers),
body=b)

# Python3 requires a byte-sting
if isinstance(content, str):
content = content.encode('utf-8')
silky_response.raw_body = base64.b64encode(content)

return silky_response

0 comments on commit d4c88b0

Please sign in to comment.