From 9294110a57ce0a6d14506969c950090045c622c8 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Wed, 27 Jun 2018 14:50:03 -0400 Subject: [PATCH] Fixed #29520 -- Fixed test client crash when posting bytes. Regression in b8a41a2872624a6d9e61308932dd81d001e31eb9. --- django/test/client.py | 2 +- tests/test_client_regress/tests.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/django/test/client.py b/django/test/client.py index 056060310ac..776410cefa0 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 00e1b017668..36b28d3a92c 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'})