Fix json_content helper on pythons with a unicode native str type
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user