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.exc_info = None
self.extra = None
self.headers = None
def request(self, **request):
"""
@ -905,6 +906,7 @@ class Client(ClientMixin, RequestFactory):
):
"""Request a response from the server using GET."""
self.extra = extra
self.headers = headers
response = super().get(path, data=data, secure=secure, headers=headers, **extra)
if follow:
response = self._handle_redirects(
@ -925,6 +927,7 @@ class Client(ClientMixin, RequestFactory):
):
"""Request a response from the server using POST."""
self.extra = extra
self.headers = headers
response = super().post(
path,
data=data,
@ -951,6 +954,7 @@ class Client(ClientMixin, RequestFactory):
):
"""Request a response from the server using HEAD."""
self.extra = extra
self.headers = headers
response = super().head(
path, data=data, secure=secure, headers=headers, **extra
)
@ -973,6 +977,7 @@ class Client(ClientMixin, RequestFactory):
):
"""Request a response from the server using OPTIONS."""
self.extra = extra
self.headers = headers
response = super().options(
path,
data=data,
@ -1000,6 +1005,7 @@ class Client(ClientMixin, RequestFactory):
):
"""Send a resource to the server using PUT."""
self.extra = extra
self.headers = headers
response = super().put(
path,
data=data,
@ -1027,6 +1033,7 @@ class Client(ClientMixin, RequestFactory):
):
"""Send a resource to the server using PATCH."""
self.extra = extra
self.headers = headers
response = super().patch(
path,
data=data,
@ -1054,6 +1061,7 @@ class Client(ClientMixin, RequestFactory):
):
"""Send a DELETE request to the server."""
self.extra = extra
self.headers = headers
response = super().delete(
path,
data=data,
@ -1080,6 +1088,7 @@ class Client(ClientMixin, RequestFactory):
):
"""Send a TRACE request to the server."""
self.extra = extra
self.headers = headers
response = super().trace(
path, data=data, secure=secure, headers=headers, **extra
)
@ -1190,6 +1199,7 @@ class AsyncClient(ClientMixin, AsyncRequestFactory):
self.raise_request_exception = raise_request_exception
self.exc_info = None
self.extra = None
self.headers = None
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
# to obtain the original response.
extra = response.client.extra or {}
headers = response.client.headers or {}
redirect_response = response.client.get(
path,
QueryDict(query),
secure=(scheme == "https"),
headers=headers,
**extra,
)
self.assertEqual(

View File

@ -601,6 +601,7 @@ class AssertRedirectsTests(SimpleTestCase):
for method in methods:
with self.subTest(method=method):
req_method = getattr(self.client, method)
# HTTP_REDIRECT in "extra".
response = req_method(
"/redirect_based_on_extra_headers_1/",
follow=False,
@ -613,6 +614,19 @@ class AssertRedirectsTests(SimpleTestCase):
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")