#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018b1f509c18-2f1a19ca-a1ad-4b3c-a7a6-469f3f3305f5-000000%40eu-central-1.amazonses.com.

Reply via email to