Fixed #29687 -- Allowed the test client to serialize list/tuple as JSON.

This commit is contained in:
Dan Palmer 2018-08-18 13:15:24 +01:00 committed by Tim Graham
parent 08f788b169
commit e181666973
6 changed files with 33 additions and 15 deletions

View File

@ -206,6 +206,7 @@ answer newbie questions, and generally made Django that much better:
Daniel Wiesmann <daniel.wiesmann@gmail.com>
Danilo Bargen
Dan Johnson <danj.py@gmail.com>
Dan Palmer <dan@danpalmer.me>
Dan Poirier <poirier@pobox.com>
Dan Stephenson <http://dan.io/>
Dan Watson <http://danwatson.net/>

View File

@ -317,10 +317,10 @@ class RequestFactory:
def _encode_json(self, data, content_type):
"""
Return encoded JSON if data is a dict and content_type is
application/json.
Return encoded JSON if data is a dict, list, or tuple and content_type
is application/json.
"""
should_encode = JSON_CONTENT_TYPE_RE.match(content_type) and isinstance(data, dict)
should_encode = JSON_CONTENT_TYPE_RE.match(content_type) and isinstance(data, (dict, list, tuple))
return json.dumps(data, cls=self.json_encoder) if should_encode else data
def _get_path(self, parsed):

View File

@ -224,6 +224,10 @@ Tests
URL, ignoring the ordering of the query string.
:meth:`~.SimpleTestCase.assertRedirects` uses the new assertion.
* The test :class:`~.django.test.Client` now supports automatic JSON
serialization of list and tuple ``data`` when
``content_type='application/json'``.
URLs
~~~~

View File

@ -213,10 +213,11 @@ Use the ``django.test.Client`` class to make requests.
name=fred&passwd=secret
If you provide ``content_type`` as :mimetype:`application/json`, a
``data`` dictionary is serialized using :func:`json.dumps` with
:class:`~django.core.serializers.json.DjangoJSONEncoder`. You can
change the encoder by providing a ``json_encoder`` argument to
If you provide ``content_type`` as :mimetype:`application/json`, the
``data`` is serialized using :func:`json.dumps` if it's a dict, list,
or tuple. Serialization is performed with
:class:`~django.core.serializers.json.DjangoJSONEncoder` by default,
and can be overridden by providing a ``json_encoder`` argument to
:class:`Client`. This serialization also happens for :meth:`put`,
:meth:`patch`, and :meth:`delete` requests.
@ -226,6 +227,11 @@ Use the ``django.test.Client`` class to make requests.
you can call :func:`json.dumps` on ``data`` before passing it to
``post()`` to achieve the same thing.
.. versionchanged:: 2.2
The JSON serialization was extended to support lists and tuples. In
older versions, only dicts are serialized.
If you provide any other ``content_type`` (e.g. :mimetype:`text/xml`
for an XML payload), the contents of ``data`` are sent as-is in the
POST request, using ``content_type`` in the HTTP ``Content-Type``

View File

@ -95,13 +95,20 @@ 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):
for data, expected in tests:
with self.subTest(data):
client_method = getattr(self.client, method)
method_name = method.upper()
response = client_method('/json_view/', {'value': 37}, content_type='application/json')
response = client_method('/json_view/', data, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['data'], 37)
self.assertEqual(response.context['data'], expected)
self.assertContains(response, 'Viewing %s page.' % method_name)
def test_json_encoder_argument(self):

View File

@ -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))