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))