Fixed #32106 -- Preserved HTTP_HOST in test Client when following redirects.

Co-authored-by: David Sanders <shang.xiao.sanders@gmail.com>
This commit is contained in:
Ben Cail 2023-10-27 09:23:07 -04:00 committed by Mariusz Felisiak
parent 9b18af4f6f
commit 523fed1d2f
4 changed files with 22 additions and 0 deletions

View File

@ -901,6 +901,7 @@ class ClientMixin:
extra["wsgi.url_scheme"] = url.scheme
if url.hostname:
extra["SERVER_NAME"] = url.hostname
extra["HTTP_HOST"] = url.hostname
if url.port:
extra["SERVER_PORT"] = str(url.port)

View File

@ -856,6 +856,13 @@ class ClientTest(TestCase):
response, "https://www.djangoproject.com/", fetch_redirect_response=False
)
@override_settings(ALLOWED_HOSTS=["hostname1", "hostname2"])
def test_redirect_with_http_host(self):
response = self.client.get(
"/redirect_to_different_hostname/", follow=True, HTTP_HOST="hostname1"
)
self.assertEqual(response.content, b"hostname2")
def test_external_redirect_without_trailing_slash(self):
"""
Client._handle_redirects() with an empty path.

View File

@ -25,6 +25,12 @@ urlpatterns = [
"redirect_view_308_query_string/",
views.method_saving_308_redirect_query_string_view,
),
path(
"redirect_to_different_hostname/",
views.redirect_to_different_hostname,
name="redirect_to_different_hostname",
),
path("get_host_view/", views.get_host_view, name="get_host_view"),
path("secure_view/", views.view_with_secure),
path(
"permanent_redirect_view/",

View File

@ -179,6 +179,14 @@ def method_saving_308_redirect_view(request):
return _post_view_redirect(request, 308)
def redirect_to_different_hostname(request):
return HttpResponseRedirect("https://hostname2/get_host_view/")
def get_host_view(request):
return HttpResponse(request.get_host())
def view_with_secure(request):
"A view that indicates if the request was secure"
response = HttpResponse()