summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2018-06-27 14:50:03 -0400
committerTim Graham <timograham@gmail.com>2018-06-27 16:52:06 -0400
commit01b7fdfd67a87e8c881e34d7ed97767c506b189c (patch)
treeba45e6537ba0abe26c2508e2ca26a36929c7233d
parentda46599143408be58c1845b8c82cacbae0bb56c0 (diff)
[2.1.x] Fixed #29520 -- Fixed test client crash when posting bytes.
Regression in b8a41a2872624a6d9e61308932dd81d001e31eb9. Backport of 9294110a57ce0a6d14506969c950090045c622c8 from master
-rw-r--r--django/test/client.py2
-rw-r--r--tests/test_client_regress/tests.py4
2 files changed, 5 insertions, 1 deletions
diff --git a/django/test/client.py b/django/test/client.py
index 056060310a..776410cefa 100644
--- a/django/test/client.py
+++ b/django/test/client.py
@@ -310,7 +310,7 @@ class RequestFactory:
charset = match.group(1)
else:
charset = settings.DEFAULT_CHARSET
- return data.encode(charset)
+ return force_bytes(data, encoding=charset)
def _encode_json(self, data, content_type):
"""
diff --git a/tests/test_client_regress/tests.py b/tests/test_client_regress/tests.py
index 00e1b01766..36b28d3a92 100644
--- a/tests/test_client_regress/tests.py
+++ b/tests/test_client_regress/tests.py
@@ -1196,6 +1196,10 @@ class RequestMethodStringDataTests(SimpleTestCase):
response = self.client.head('/body/', data='', content_type='application/json')
self.assertEqual(response.content, b'')
+ def test_json_bytes(self):
+ response = self.client.post('/body/', data=b"{'value': 37}", content_type='application/json')
+ self.assertEqual(response.content, b"{'value': 37}")
+
def test_json(self):
response = self.client.get('/json_response/')
self.assertEqual(response.json(), {'key': 'value'})