From d4c88b08d429f04019f75e262036797e3b9fd808 Mon Sep 17 00:00:00 2001 From: Andrew Velis Date: Wed, 20 Jan 2016 01:03:53 -0600 Subject: [PATCH] Fixes to unit test errors. Addresses #98 Change set should make all unit tests pass under TravisCI --- project/tests/test_encoding.py | 15 ++++++++------- silk/model_factory.py | 4 ++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/project/tests/test_encoding.py b/project/tests/test_encoding.py index 50691a5f..10d53b3e 100644 --- a/project/tests/test_encoding.py +++ b/project/tests/test_encoding.py @@ -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 @@ -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) \ No newline at end of file + self.assertDictEqual(json.loads(body), d) + self.assertEqual(mock.content, content) diff --git a/silk/model_factory.py b/silk/model_factory.py index 594b2be6..38fccde6 100644 --- a/silk/model_factory.py +++ b/silk/model_factory.py @@ -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