Skip to content

Commit

Permalink
support django 2.1 test client json data automatically serialized
Browse files Browse the repository at this point in the history
  • Loading branch information
terencehonles committed Mar 15, 2019
1 parent d2d1888 commit 4607f28
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions rest_framework/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,19 @@ def _encode_data(self, data, format=None, content_type=None):
Encode the data returning a two tuple of (bytes, content_type)
"""

if data is None:
return ('', content_type)

assert format is None or content_type is None, (
'You may not set both `format` and `content_type`.'
)

if content_type:
try:
data = self._encode_json(data, content_type)
except AttributeError:
pass

if data is None:
data = ''

# Content type specified explicitly, treat data as a raw bytestring
ret = force_bytes(data, settings.DEFAULT_CHARSET)

Expand All @@ -181,13 +186,17 @@ def _encode_data(self, data, format=None, content_type=None):

# Use format and render the data into a bytestring
renderer = self.renderer_classes[format]()
ret = renderer.render(data)

# Determine the content-type header from the renderer
content_type = "{0}; charset={1}".format(
renderer.media_type, renderer.charset
)

if data is None:
ret = ''
else:
ret = renderer.render(data)

# Coerce text to bytes if required.
if isinstance(ret, six.text_type):
ret = bytes(ret.encode(renderer.charset))
Expand Down

0 comments on commit 4607f28

Please sign in to comment.