From da37fd0364069e89509f81a00d6d57cf592fb653 Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 27 Jul 2012 19:52:44 +0100 Subject: [PATCH] Fix json_content helper on pythons with a unicode native str type --- testtools/content.py | 8 ++++++-- testtools/tests/test_content.py | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/testtools/content.py b/testtools/content.py index 8814f79..de60950 100644 --- a/testtools/content.py +++ b/testtools/content.py @@ -190,9 +190,13 @@ class TracebackContent(Content): return length -def json_content(data): +def json_content(json_data): """Create a JSON `Content` object from JSON-encodeable data.""" - return Content(JSON, lambda: [json.dumps(data)]) + data = json.dumps(json_data) + if str_is_unicode: + # The json module perversely returns native str not bytes + data = data.encode('utf8') + return Content(JSON, lambda: [data]) def text_content(text): diff --git a/testtools/tests/test_content.py b/testtools/tests/test_content.py index 2f12e46..3cb801a 100644 --- a/testtools/tests/test_content.py +++ b/testtools/tests/test_content.py @@ -155,7 +155,7 @@ class TestContent(TestCase): def test_json_content(self): data = {'foo': 'bar'} - expected = Content(JSON, lambda: [json.dumps(data)]) + expected = Content(JSON, lambda: [_b('{"foo": "bar"}')]) self.assertEqual(expected, json_content(data))