mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
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:
parent
9b18af4f6f
commit
523fed1d2f
@ -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)
|
||||
|
||||
|
@ -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.
|
||||
|
@ -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/",
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user