mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-27 00:19:53 +03:00
Add 307 308 follow redirect test
This commit is contained in:
parent
fbb29d03e1
commit
149eb33f7a
|
@ -1,7 +1,10 @@
|
||||||
|
import itertools
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
import django
|
import django
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
from django.http import HttpResponseRedirect
|
||||||
from django.shortcuts import redirect
|
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
|
||||||
|
@ -14,7 +17,7 @@ from rest_framework.test import (
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@api_view(['GET', 'POST'])
|
@api_view(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'])
|
||||||
def view(request):
|
def view(request):
|
||||||
return Response({
|
return Response({
|
||||||
'auth': request.META.get('HTTP_AUTHORIZATION', b''),
|
'auth': request.META.get('HTTP_AUTHORIZATION', b''),
|
||||||
|
@ -36,6 +39,11 @@ def redirect_view(request):
|
||||||
return redirect('/view/')
|
return redirect('/view/')
|
||||||
|
|
||||||
|
|
||||||
|
@api_view(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'])
|
||||||
|
def redirect_307_308_view(request, code):
|
||||||
|
return HttpResponseRedirect('/view/', status=code)
|
||||||
|
|
||||||
|
|
||||||
class BasicSerializer(serializers.Serializer):
|
class BasicSerializer(serializers.Serializer):
|
||||||
flag = fields.BooleanField(default=lambda: True)
|
flag = fields.BooleanField(default=lambda: True)
|
||||||
|
|
||||||
|
@ -51,6 +59,7 @@ urlpatterns = [
|
||||||
path('view/', view),
|
path('view/', view),
|
||||||
path('session-view/', session_view),
|
path('session-view/', session_view),
|
||||||
path('redirect-view/', redirect_view),
|
path('redirect-view/', redirect_view),
|
||||||
|
path('redirect-view/<int:code>/', redirect_307_308_view),
|
||||||
path('post-view/', post_view)
|
path('post-view/', post_view)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -155,6 +164,24 @@ class TestAPITestClient(TestCase):
|
||||||
assert response.redirect_chain is not None
|
assert response.redirect_chain is not None
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
def test_follow_307_308_preserve_kwargs(self, *mocked_methods):
|
||||||
|
"""
|
||||||
|
Follow redirect by setting follow argument, and make sure the following
|
||||||
|
method called with appropriate kwargs.
|
||||||
|
"""
|
||||||
|
methods = ('get', 'post', 'put', 'patch', 'delete', 'options')
|
||||||
|
codes = (307, 308)
|
||||||
|
for method, code in itertools.product(methods, codes):
|
||||||
|
subtest_ctx = self.subTest(method=method, code=code)
|
||||||
|
patch_ctx = patch.object(self.client, method, side_effect=getattr(self.client, method))
|
||||||
|
with subtest_ctx, patch_ctx as req_method:
|
||||||
|
kwargs = {'data': {'example': 'test'}, 'format': 'json'}
|
||||||
|
response = req_method('/redirect-view/%s/' % code, follow=True, **kwargs)
|
||||||
|
assert response.redirect_chain is not None
|
||||||
|
assert response.status_code == 200
|
||||||
|
for _, call_args, call_kwargs in req_method.mock_calls:
|
||||||
|
assert all(call_kwargs[k] == kwargs[k] for k in kwargs if k in call_kwargs)
|
||||||
|
|
||||||
def test_invalid_multipart_data(self):
|
def test_invalid_multipart_data(self):
|
||||||
"""
|
"""
|
||||||
MultiPart encoding cannot support nested data, so raise a helpful
|
MultiPart encoding cannot support nested data, so raise a helpful
|
||||||
|
|
Loading…
Reference in New Issue
Block a user