Fixed #34240 -- Preserved headers of requests made with django.test.Client in assertRedirects().

Bug in 67da22f08e.
This commit is contained in:
Mariusz Felisiak 2023-01-13 11:30:27 +01:00 committed by GitHub
parent 648005dee6
commit c2118d72d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 0 deletions

View File

@ -845,6 +845,7 @@ class Client(ClientMixin, RequestFactory):
self.raise_request_exception = raise_request_exception self.raise_request_exception = raise_request_exception
self.exc_info = None self.exc_info = None
self.extra = None self.extra = None
self.headers = None
def request(self, **request): def request(self, **request):
""" """
@ -905,6 +906,7 @@ class Client(ClientMixin, RequestFactory):
): ):
"""Request a response from the server using GET.""" """Request a response from the server using GET."""
self.extra = extra self.extra = extra
self.headers = headers
response = super().get(path, data=data, secure=secure, headers=headers, **extra) response = super().get(path, data=data, secure=secure, headers=headers, **extra)
if follow: if follow:
response = self._handle_redirects( response = self._handle_redirects(
@ -925,6 +927,7 @@ class Client(ClientMixin, RequestFactory):
): ):
"""Request a response from the server using POST.""" """Request a response from the server using POST."""
self.extra = extra self.extra = extra
self.headers = headers
response = super().post( response = super().post(
path, path,
data=data, data=data,
@ -951,6 +954,7 @@ class Client(ClientMixin, RequestFactory):
): ):
"""Request a response from the server using HEAD.""" """Request a response from the server using HEAD."""
self.extra = extra self.extra = extra
self.headers = headers
response = super().head( response = super().head(
path, data=data, secure=secure, headers=headers, **extra path, data=data, secure=secure, headers=headers, **extra
) )
@ -973,6 +977,7 @@ class Client(ClientMixin, RequestFactory):
): ):
"""Request a response from the server using OPTIONS.""" """Request a response from the server using OPTIONS."""
self.extra = extra self.extra = extra
self.headers = headers
response = super().options( response = super().options(
path, path,
data=data, data=data,
@ -1000,6 +1005,7 @@ class Client(ClientMixin, RequestFactory):
): ):
"""Send a resource to the server using PUT.""" """Send a resource to the server using PUT."""
self.extra = extra self.extra = extra
self.headers = headers
response = super().put( response = super().put(
path, path,
data=data, data=data,
@ -1027,6 +1033,7 @@ class Client(ClientMixin, RequestFactory):
): ):
"""Send a resource to the server using PATCH.""" """Send a resource to the server using PATCH."""
self.extra = extra self.extra = extra
self.headers = headers
response = super().patch( response = super().patch(
path, path,
data=data, data=data,
@ -1054,6 +1061,7 @@ class Client(ClientMixin, RequestFactory):
): ):
"""Send a DELETE request to the server.""" """Send a DELETE request to the server."""
self.extra = extra self.extra = extra
self.headers = headers
response = super().delete( response = super().delete(
path, path,
data=data, data=data,
@ -1080,6 +1088,7 @@ class Client(ClientMixin, RequestFactory):
): ):
"""Send a TRACE request to the server.""" """Send a TRACE request to the server."""
self.extra = extra self.extra = extra
self.headers = headers
response = super().trace( response = super().trace(
path, data=data, secure=secure, headers=headers, **extra path, data=data, secure=secure, headers=headers, **extra
) )
@ -1190,6 +1199,7 @@ class AsyncClient(ClientMixin, AsyncRequestFactory):
self.raise_request_exception = raise_request_exception self.raise_request_exception = raise_request_exception
self.exc_info = None self.exc_info = None
self.extra = None self.extra = None
self.headers = None
async def request(self, **request): async def request(self, **request):
""" """

View File

@ -545,10 +545,12 @@ class SimpleTestCase(unittest.TestCase):
# Get the redirection page, using the same client that was used # Get the redirection page, using the same client that was used
# to obtain the original response. # to obtain the original response.
extra = response.client.extra or {} extra = response.client.extra or {}
headers = response.client.headers or {}
redirect_response = response.client.get( redirect_response = response.client.get(
path, path,
QueryDict(query), QueryDict(query),
secure=(scheme == "https"), secure=(scheme == "https"),
headers=headers,
**extra, **extra,
) )
self.assertEqual( self.assertEqual(

View File

@ -601,6 +601,7 @@ class AssertRedirectsTests(SimpleTestCase):
for method in methods: for method in methods:
with self.subTest(method=method): with self.subTest(method=method):
req_method = getattr(self.client, method) req_method = getattr(self.client, method)
# HTTP_REDIRECT in "extra".
response = req_method( response = req_method(
"/redirect_based_on_extra_headers_1/", "/redirect_based_on_extra_headers_1/",
follow=False, follow=False,
@ -613,6 +614,19 @@ class AssertRedirectsTests(SimpleTestCase):
status_code=302, status_code=302,
target_status_code=302, target_status_code=302,
) )
# HTTP_REDIRECT in "headers".
response = req_method(
"/redirect_based_on_extra_headers_1/",
follow=False,
headers={"redirect": "val"},
)
self.assertRedirects(
response,
"/redirect_based_on_extra_headers_2/",
fetch_redirect_response=True,
status_code=302,
target_status_code=302,
)
@override_settings(ROOT_URLCONF="test_client_regress.urls") @override_settings(ROOT_URLCONF="test_client_regress.urls")