support django 2.1 test client json data automatically serialized (#6511)

This commit is contained in:
Terence Honles 2024-12-28 11:22:21 +01:00 committed by GitHub
parent a8595a8eae
commit 089f6a6974
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 9 deletions

View File

@ -25,9 +25,12 @@ The `APIRequestFactory` class supports an almost identical API to Django's stand
factory = APIRequestFactory()
request = factory.post('/notes/', {'title': 'new idea'})
# Using the standard RequestFactory API to encode JSON data
request = factory.post('/notes/', {'title': 'new idea'}, content_type='application/json')
#### Using the `format` argument
Methods which create a request body, such as `post`, `put` and `patch`, include a `format` argument, which make it easy to generate requests using a content type other than multipart form data. For example:
Methods which create a request body, such as `post`, `put` and `patch`, include a `format` argument, which make it easy to generate requests using a wide set of request formats. When using this argument, the factory will select an appropriate renderer and its configured `content_type`. For example:
# Create a JSON POST request
factory = APIRequestFactory()
@ -41,7 +44,7 @@ To support a wider set of request formats, or change the default format, [see th
If you need to explicitly encode the request body, you can do so by setting the `content_type` flag. For example:
request = factory.post('/notes/', json.dumps({'title': 'new idea'}), content_type='application/json')
request = factory.post('/notes/', yaml.dump({'title': 'new idea'}), content_type='application/yaml')
#### PUT and PATCH with form data

View File

@ -151,14 +151,19 @@ class APIRequestFactory(DjangoRequestFactory):
Encode the data returning a two tuple of (bytes, content_type)
"""
if data is None:
return ('', content_type)
assert format is None or content_type is None, (
'You may not set both `format` and `content_type`.'
)
if content_type:
try:
data = self._encode_json(data, content_type)
except AttributeError:
pass
if data is None:
data = ''
# Content type specified explicitly, treat data as a raw bytestring
ret = force_bytes(data, settings.DEFAULT_CHARSET)
@ -176,7 +181,6 @@ class APIRequestFactory(DjangoRequestFactory):
# Use format and render the data into a bytestring
renderer = self.renderer_classes[format]()
ret = renderer.render(data)
# Determine the content-type header from the renderer
content_type = renderer.media_type
@ -185,6 +189,11 @@ class APIRequestFactory(DjangoRequestFactory):
content_type, renderer.charset
)
if data is None:
ret = ''
else:
ret = renderer.render(data)
# Coerce text to bytes if required.
if isinstance(ret, str):
ret = ret.encode(renderer.charset)

View File

@ -8,9 +8,9 @@ from django.shortcuts import redirect
from django.test import TestCase, override_settings
from django.urls import path
from rest_framework import fields, serializers
from rest_framework import fields, parsers, serializers
from rest_framework.authtoken.models import Token
from rest_framework.decorators import api_view
from rest_framework.decorators import api_view, parser_classes
from rest_framework.response import Response
from rest_framework.test import (
APIClient, APIRequestFactory, URLPatternsTestCase, force_authenticate
@ -50,6 +50,12 @@ class BasicSerializer(serializers.Serializer):
flag = fields.BooleanField(default=lambda: True)
@api_view(['POST'])
@parser_classes((parsers.JSONParser,))
def post_json_view(request):
return Response(request.data)
@api_view(['POST'])
def post_view(request):
serializer = BasicSerializer(data=request.data)
@ -62,7 +68,8 @@ urlpatterns = [
path('session-view/', session_view),
path('redirect-view/', redirect_view),
path('redirect-view/<int:code>/', redirect_307_308_view),
path('post-view/', post_view)
path('post-json-view/', post_json_view),
path('post-view/', post_view),
]
@ -236,6 +243,17 @@ class TestAPITestClient(TestCase):
assert response.status_code == 200
assert response.data == {"flag": True}
def test_post_encodes_data_based_on_json_content_type(self):
data = {'data': True}
response = self.client.post(
'/post-json-view/',
data=data,
content_type='application/json'
)
assert response.status_code == 200
assert response.data == data
class TestAPIRequestFactory(TestCase):
def test_csrf_exempt_by_default(self):