summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorNick Sarbicki <nick.a.sarbicki@gmail.com>2018-02-05 10:22:24 +0000
committerTim Graham <timograham@gmail.com>2018-02-06 18:29:04 -0500
commit47268242b071895dd269d97540e45dce646f675c (patch)
treedd03c4fc91098c58507b11e7db78b36c99e581b3 /docs
parentd968788b57f41b7def88046d1178fd2932a32a4e (diff)
Fixed #29082 -- Allowed the test client to encode JSON request data.
Diffstat (limited to 'docs')
-rw-r--r--docs/releases/2.1.txt4
-rw-r--r--docs/topics/testing/tools.txt29
2 files changed, 29 insertions, 4 deletions
diff --git a/docs/releases/2.1.txt b/docs/releases/2.1.txt
index 54666e3d44..d62371f555 100644
--- a/docs/releases/2.1.txt
+++ b/docs/releases/2.1.txt
@@ -208,6 +208,10 @@ Tests
* Added test :class:`~django.test.Client` support for 307 and 308 redirects.
+* The test :class:`~django.test.Client` now serializes a request data
+ dictionary as JSON if ``content_type='application/json'``. You can customize
+ the JSON encoder with test client's ``json_encoder`` parameter.
+
URLs
~~~~
diff --git a/docs/topics/testing/tools.txt b/docs/topics/testing/tools.txt
index d3be9e2124..3adacffb35 100644
--- a/docs/topics/testing/tools.txt
+++ b/docs/topics/testing/tools.txt
@@ -109,7 +109,7 @@ Making requests
Use the ``django.test.Client`` class to make requests.
-.. class:: Client(enforce_csrf_checks=False, **defaults)
+.. class:: Client(enforce_csrf_checks=False, json_encoder=DjangoJSONEncoder, **defaults)
It requires no arguments at time of construction. However, you can use
keywords arguments to specify some default headers. For example, this will
@@ -125,6 +125,13 @@ Use the ``django.test.Client`` class to make requests.
The ``enforce_csrf_checks`` argument can be used to test CSRF
protection (see above).
+ The ``json_encoder`` argument allows setting a custom JSON encoder for
+ the JSON serialization that's described in :meth:`post`.
+
+ .. versionchanged:: 2.1
+
+ The ``json_encoder`` argument was added.
+
Once you have a ``Client`` instance, you can call any of the following
methods:
@@ -206,9 +213,23 @@ Use the ``django.test.Client`` class to make requests.
name=fred&passwd=secret
- If you provide ``content_type`` (e.g. :mimetype:`text/xml` for an XML
- payload), the contents of ``data`` will be sent as-is in the POST
- request, using ``content_type`` in the HTTP ``Content-Type`` header.
+ 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
+ :class:`Client`. This serialization also happens for :meth:`put`,
+ :meth:`patch`, and :meth:`delete` requests.
+
+ .. versionchanged:: 2.1
+
+ The JSON serialization described above was added. In older versions,
+ you can call :func:`json.dumps` on ``data`` before passing it to
+ ``post()`` to achieve the same thing.
+
+ 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``
+ header.
If you don't provide a value for ``content_type``, the values in
``data`` will be transmitted with a content type of