mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-04-28 13:03:45 +03:00
Fixed regression that tests using format still work (#9615)
* Fixed regression that tests using format still work Error only occurred on tests which return no content and use a renderer without charset (e.g. JSONRenderer) * Fixed linting * Used early return as before * Move ret str check back to where it was
This commit is contained in:
parent
a4f6059d50
commit
4a1d773b8f
|
@ -150,6 +150,8 @@ class APIRequestFactory(DjangoRequestFactory):
|
||||||
"""
|
"""
|
||||||
Encode the data returning a two tuple of (bytes, content_type)
|
Encode the data returning a two tuple of (bytes, content_type)
|
||||||
"""
|
"""
|
||||||
|
if data is None:
|
||||||
|
return (b'', content_type)
|
||||||
|
|
||||||
assert format is None or content_type is None, (
|
assert format is None or content_type is None, (
|
||||||
'You may not set both `format` and `content_type`.'
|
'You may not set both `format` and `content_type`.'
|
||||||
|
@ -161,9 +163,6 @@ class APIRequestFactory(DjangoRequestFactory):
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if data is None:
|
|
||||||
data = ''
|
|
||||||
|
|
||||||
# Content type specified explicitly, treat data as a raw bytestring
|
# Content type specified explicitly, treat data as a raw bytestring
|
||||||
ret = force_bytes(data, settings.DEFAULT_CHARSET)
|
ret = force_bytes(data, settings.DEFAULT_CHARSET)
|
||||||
|
|
||||||
|
@ -181,6 +180,7 @@ class APIRequestFactory(DjangoRequestFactory):
|
||||||
|
|
||||||
# Use format and render the data into a bytestring
|
# Use format and render the data into a bytestring
|
||||||
renderer = self.renderer_classes[format]()
|
renderer = self.renderer_classes[format]()
|
||||||
|
ret = renderer.render(data)
|
||||||
|
|
||||||
# Determine the content-type header from the renderer
|
# Determine the content-type header from the renderer
|
||||||
content_type = renderer.media_type
|
content_type = renderer.media_type
|
||||||
|
@ -189,11 +189,6 @@ class APIRequestFactory(DjangoRequestFactory):
|
||||||
content_type, renderer.charset
|
content_type, renderer.charset
|
||||||
)
|
)
|
||||||
|
|
||||||
if data is None:
|
|
||||||
ret = ''
|
|
||||||
else:
|
|
||||||
ret = renderer.render(data)
|
|
||||||
|
|
||||||
# Coerce text to bytes if required.
|
# Coerce text to bytes if required.
|
||||||
if isinstance(ret, str):
|
if isinstance(ret, str):
|
||||||
ret = ret.encode(renderer.charset)
|
ret = ret.encode(renderer.charset)
|
||||||
|
|
|
@ -8,9 +8,11 @@ from django.shortcuts import redirect
|
||||||
from django.test import TestCase, override_settings
|
from django.test import TestCase, override_settings
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
|
||||||
from rest_framework import fields, parsers, serializers
|
from rest_framework import fields, parsers, renderers, serializers, status
|
||||||
from rest_framework.authtoken.models import Token
|
from rest_framework.authtoken.models import Token
|
||||||
from rest_framework.decorators import api_view, parser_classes
|
from rest_framework.decorators import (
|
||||||
|
api_view, parser_classes, renderer_classes
|
||||||
|
)
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.test import (
|
from rest_framework.test import (
|
||||||
APIClient, APIRequestFactory, URLPatternsTestCase, force_authenticate
|
APIClient, APIRequestFactory, URLPatternsTestCase, force_authenticate
|
||||||
|
@ -56,6 +58,12 @@ def post_json_view(request):
|
||||||
return Response(request.data)
|
return Response(request.data)
|
||||||
|
|
||||||
|
|
||||||
|
@api_view(['DELETE'])
|
||||||
|
@renderer_classes((renderers.JSONRenderer, ))
|
||||||
|
def delete_json_view(request):
|
||||||
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||||
|
|
||||||
|
|
||||||
@api_view(['POST'])
|
@api_view(['POST'])
|
||||||
def post_view(request):
|
def post_view(request):
|
||||||
serializer = BasicSerializer(data=request.data)
|
serializer = BasicSerializer(data=request.data)
|
||||||
|
@ -69,6 +77,7 @@ urlpatterns = [
|
||||||
path('redirect-view/', redirect_view),
|
path('redirect-view/', redirect_view),
|
||||||
path('redirect-view/<int:code>/', redirect_307_308_view),
|
path('redirect-view/<int:code>/', redirect_307_308_view),
|
||||||
path('post-json-view/', post_json_view),
|
path('post-json-view/', post_json_view),
|
||||||
|
path('delete-json-view/', delete_json_view),
|
||||||
path('post-view/', post_view),
|
path('post-view/', post_view),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -254,6 +263,11 @@ class TestAPITestClient(TestCase):
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert response.data == data
|
assert response.data == data
|
||||||
|
|
||||||
|
def test_delete_based_on_format(self):
|
||||||
|
response = self.client.delete('/delete-json-view/', format='json')
|
||||||
|
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||||
|
assert response.data is None
|
||||||
|
|
||||||
|
|
||||||
class TestAPIRequestFactory(TestCase):
|
class TestAPIRequestFactory(TestCase):
|
||||||
def test_csrf_exempt_by_default(self):
|
def test_csrf_exempt_by_default(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user