Refs #31494 -- Added test for query strings for GET/HEAD requests when following HTTP 307/308 redirects in test client.

This commit is contained in:
Ahmad A. Hussein 2020-05-27 09:16:01 +02:00 committed by Mariusz Felisiak
parent 9d55ae00d3
commit 6425fd3124
3 changed files with 30 additions and 0 deletions

View File

@ -284,6 +284,20 @@ class ClientTest(TestCase):
self.assertEqual(response.request['PATH_INFO'], '/post_view/') self.assertEqual(response.request['PATH_INFO'], '/post_view/')
self.assertEqual(response.request['REQUEST_METHOD'], method.upper()) self.assertEqual(response.request['REQUEST_METHOD'], method.upper())
def test_follow_307_and_308_get_head_query_string(self):
methods = ('get', 'head')
codes = (307, 308)
for method, code in itertools.product(methods, codes):
with self.subTest(method=method, code=code):
req_method = getattr(self.client, method)
response = req_method(
'/redirect_view_%s_query_string/' % code,
data={'value': 'test'},
follow=True,
)
self.assertRedirects(response, '/post_view/?hello=world', status_code=code)
self.assertEqual(response.request['QUERY_STRING'], 'value=test')
def test_follow_307_and_308_preserves_post_data(self): def test_follow_307_and_308_preserves_post_data(self):
for code in (307, 308): for code in (307, 308):
with self.subTest(code=code): with self.subTest(code=code):

View File

@ -14,7 +14,15 @@ urlpatterns = [
path('raw_post_view/', views.raw_post_view), path('raw_post_view/', views.raw_post_view),
path('redirect_view/', views.redirect_view), path('redirect_view/', views.redirect_view),
path('redirect_view_307/', views.method_saving_307_redirect_view), path('redirect_view_307/', views.method_saving_307_redirect_view),
path(
'redirect_view_307_query_string/',
views.method_saving_307_redirect_query_string_view,
),
path('redirect_view_308/', views.method_saving_308_redirect_view), path('redirect_view_308/', views.method_saving_308_redirect_view),
path(
'redirect_view_308_query_string/',
views.method_saving_308_redirect_query_string_view,
),
path('secure_view/', views.view_with_secure), path('secure_view/', views.view_with_secure),
path('permanent_redirect_view/', RedirectView.as_view(url='/get_view/', permanent=True)), path('permanent_redirect_view/', RedirectView.as_view(url='/get_view/', permanent=True)),
path('temporary_redirect_view/', RedirectView.as_view(url='/get_view/', permanent=False)), path('temporary_redirect_view/', RedirectView.as_view(url='/get_view/', permanent=False)),

View File

@ -131,6 +131,14 @@ def redirect_view(request):
return HttpResponseRedirect('/get_view/' + query) return HttpResponseRedirect('/get_view/' + query)
def method_saving_307_redirect_query_string_view(request):
return HttpResponseRedirect('/post_view/?hello=world', status=307)
def method_saving_308_redirect_query_string_view(request):
return HttpResponseRedirect('/post_view/?hello=world', status=308)
def _post_view_redirect(request, status_code): def _post_view_redirect(request, status_code):
"""Redirect to /post_view/ using the status code.""" """Redirect to /post_view/ using the status code."""
redirect_to = request.GET.get('to', '/post_view/') redirect_to = request.GET.get('to', '/post_view/')