From e1816669732c54c40122c8a22dcba42f6ee3c326 Mon Sep 17 00:00:00 2001 From: Dan Palmer Date: Sat, 18 Aug 2018 13:15:24 +0100 Subject: Fixed #29687 -- Allowed the test client to serialize list/tuple as JSON. --- docs/releases/2.2.txt | 4 ++++ docs/topics/testing/tools.txt | 14 ++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) (limited to 'docs') diff --git a/docs/releases/2.2.txt b/docs/releases/2.2.txt index 4ca3bb9662..ff692151ed 100644 --- a/docs/releases/2.2.txt +++ b/docs/releases/2.2.txt @@ -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 ~~~~ diff --git a/docs/topics/testing/tools.txt b/docs/topics/testing/tools.txt index 92e96e8c98..38b437e18c 100644 --- a/docs/topics/testing/tools.txt +++ b/docs/topics/testing/tools.txt @@ -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`` -- cgit v1.3