mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #29082 -- Allowed the test client to encode JSON request data.
This commit is contained in:
committed by
Tim Graham
parent
d968788b57
commit
47268242b0
@@ -21,6 +21,7 @@ rather than the HTML rendered to the end-user.
|
||||
"""
|
||||
import itertools
|
||||
import tempfile
|
||||
from unittest import mock
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.core import mail
|
||||
@@ -86,6 +87,31 @@ class ClientTest(TestCase):
|
||||
self.assertEqual(response.templates[0].name, 'POST Template')
|
||||
self.assertContains(response, 'Data received')
|
||||
|
||||
def test_json_serialization(self):
|
||||
"""The test client serializes JSON data."""
|
||||
methods = ('post', 'put', 'patch', 'delete')
|
||||
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)
|
||||
|
||||
def test_json_encoder_argument(self):
|
||||
"""The test Client accepts a json_encoder."""
|
||||
mock_encoder = mock.MagicMock()
|
||||
mock_encoding = mock.MagicMock()
|
||||
mock_encoder.return_value = mock_encoding
|
||||
mock_encoding.encode.return_value = '{"value": 37}'
|
||||
|
||||
client = self.client_class(json_encoder=mock_encoder)
|
||||
# Vendored tree JSON content types are accepted.
|
||||
client.post('/json_view/', {'value': 37}, content_type='application/vnd.api+json')
|
||||
self.assertTrue(mock_encoder.called)
|
||||
self.assertTrue(mock_encoding.encode.called)
|
||||
|
||||
def test_trace(self):
|
||||
"""TRACE a view"""
|
||||
response = self.client.trace('/trace_view/')
|
||||
|
||||
Reference in New Issue
Block a user