Re: [Django] #32106: Redirect following in test client does not correctly set HTTP_HOST

9 views
Skip to first unread message

Django

unread,
Oct 11, 2023, 11:17:43 AM10/11/23
to django-...@googlegroups.com
#32106: Redirect following in test client does not correctly set HTTP_HOST
-----------------------------------+------------------------------------
Reporter: Brenton Partridge | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------

Comment (by bcail):

Would something like this work for the test case? Should I open a PR for
this?

{{{
diff --git a/tests/test_client/tests.py b/tests/test_client/tests.py
index 402f282588..dc25d52ee2 100644
--- a/tests/test_client/tests.py
+++ b/tests/test_client/tests.py
@@ -26,7 +26,7 @@ from unittest import mock

from django.contrib.auth.models import User
from django.core import mail
-from django.http import HttpResponse, HttpResponseNotAllowed
+from django.http import HttpResponse, HttpResponseNotAllowed,
HttpResponseRedirect
from django.test import (
AsyncRequestFactory,
Client,
@@ -856,6 +856,36 @@ class ClientTest(TestCase):
response, "https://www.djangoproject.com/",
fetch_redirect_response=False
)

+ def test_external_redirect_http_host(self):
+ response_1 =
HttpResponseRedirect(redirect_to='https://www.djangoproject.com')
+ response_2 = HttpResponse()
+
+ with mock.patch('django.test.Client.request') as m:
+ m.side_effect = [response_1, response_2]
+ client = self.client_class()
+ response = client.get("/django_project_redirect/",
follow=True)
+
+ call_1_params = {
+ 'PATH_INFO': '/django_project_redirect/',
+ 'REQUEST_METHOD': 'GET',
+ 'SERVER_PORT': '80',
+ 'wsgi.url_scheme': 'http',
+ 'QUERY_STRING': ''
+ }
+ call_2_params = {
+ 'PATH_INFO': '/',
+ 'REQUEST_METHOD': 'GET',
+ 'SERVER_PORT': '80',
+ 'wsgi.url_scheme': 'https',
+ 'QUERY_STRING': '',
+ 'SERVER_NAME': 'www.djangoproject.com',
+ 'HTTP_HOST': 'www.djangoproject.com'
+ }
+ calls = m.mock_calls
+
+ self.assertEqual(calls[0], mock.call(**call_1_params))
+ self.assertEqual(calls[1], mock.call(**call_2_params))
+
def test_external_redirect_without_trailing_slash(self):
"""
Client._handle_redirects() with an empty path.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32106#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 27, 2023, 9:59:04 AM10/27/23
to django-...@googlegroups.com
#32106: Redirect following in test client does not correctly set HTTP_HOST
-----------------------------------+------------------------------------
Reporter: Brenton Partridge | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------
Changes (by bcail):

* cc: bcail (added)
* has_patch: 0 => 1


Comment:

PR: https://github.com/django/django/pull/17414

--
Ticket URL: <https://code.djangoproject.com/ticket/32106#comment:3>

Django

unread,
Oct 30, 2023, 8:17:20 PM10/30/23
to django-...@googlegroups.com
#32106: Redirect following in test client does not correctly set HTTP_HOST
-----------------------------------+------------------------------------
Reporter: Brenton Partridge | Owner: bcail
Type: Bug | Status: assigned

Component: Testing framework | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------
Changes (by David Sanders):

* owner: nobody => bcail
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/32106#comment:4>

Django

unread,
Oct 31, 2023, 12:33:53 AM10/31/23
to django-...@googlegroups.com
#32106: Redirect following in test client does not correctly set HTTP_HOST
-------------------------------------+-------------------------------------

Reporter: Brenton Partridge | Owner: bcail
Type: Bug | Status: assigned
Component: Testing framework | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/32106#comment:5>

Django

unread,
Oct 31, 2023, 3:08:03 AM10/31/23
to django-...@googlegroups.com
#32106: Redirect following in test client does not correctly set HTTP_HOST
-------------------------------------+-------------------------------------
Reporter: Brenton Partridge | Owner: bcail
Type: Bug | Status: closed

Component: Testing framework | Version: 3.1
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"523fed1d2faafa6610f27dad9bbb7d7c4e122ea5" 523fed1]:
{{{
#!CommitTicketReference repository=""
revision="523fed1d2faafa6610f27dad9bbb7d7c4e122ea5"
Fixed #32106 -- Preserved HTTP_HOST in test Client when following
redirects.

Co-authored-by: David Sanders <shang.xia...@gmail.com>
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32106#comment:6>

Reply all
Reply to author
Forward
0 new messages