summaryrefslogtreecommitdiff
path: root/tests/test_client
diff options
context:
space:
mode:
authorDan Palmer <dan@danpalmer.me>2018-08-18 13:15:24 +0100
committerTim Graham <timograham@gmail.com>2018-08-25 10:57:05 -0400
commite1816669732c54c40122c8a22dcba42f6ee3c326 (patch)
tree80d66304077c63580144f72ba744d40a63d04c68 /tests/test_client
parent08f788b169a30d26f50433083aca253a4e4031b2 (diff)
downloaddjango-e1816669732c54c40122c8a22dcba42f6ee3c326.tar.gz
Fixed #29687 -- Allowed the test client to serialize list/tuple as JSON.
Diffstat (limited to 'tests/test_client')
-rw-r--r--tests/test_client/tests.py19
-rw-r--r--tests/test_client/views.py4
2 files changed, 15 insertions, 8 deletions
diff --git a/tests/test_client/tests.py b/tests/test_client/tests.py
index b39d5f5e09..22e28be198 100644
--- a/tests/test_client/tests.py
+++ b/tests/test_client/tests.py
@@ -95,14 +95,21 @@ class ClientTest(TestCase):
def test_json_serialization(self):
"""The test client serializes JSON data."""
methods = ('post', 'put', 'patch', 'delete')
+ tests = (
+ ({'value': 37}, {'value': 37}),
+ ([37, True], [37, True]),
+ ((37, False), [37, False]),
+ )
for method in methods:
with self.subTest(method=method):
- client_method = getattr(self.client, method)
- method_name = method.upper()
- response = client_method('/json_view/', {'value': 37}, content_type='application/json')
- self.assertEqual(response.status_code, 200)
- self.assertEqual(response.context['data'], 37)
- self.assertContains(response, 'Viewing %s page.' % method_name)
+ for data, expected in tests:
+ with self.subTest(data):
+ client_method = getattr(self.client, method)
+ method_name = method.upper()
+ response = client_method('/json_view/', data, content_type='application/json')
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.context['data'], expected)
+ self.assertContains(response, 'Viewing %s page.' % method_name)
def test_json_encoder_argument(self):
"""The test Client accepts a json_encoder."""
diff --git a/tests/test_client/views.py b/tests/test_client/views.py
index 60a0b765d4..2d076fafaf 100644
--- a/tests/test_client/views.py
+++ b/tests/test_client/views.py
@@ -83,14 +83,14 @@ def post_view(request):
def json_view(request):
"""
A view that expects a request with the header 'application/json' and JSON
- data with a key named 'value'.
+ data, which is deserialized and included in the context.
"""
if request.META.get('CONTENT_TYPE') != 'application/json':
return HttpResponse()
t = Template('Viewing {} page. With data {{ data }}.'.format(request.method))
data = json.loads(request.body.decode('utf-8'))
- c = Context({'data': data['value']})
+ c = Context({'data': data})
return HttpResponse(t.render(c))