Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #16040 -- Preserved scheme, host and port in the test client wh…

…en following a redirect.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17157 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 33a52cde08ef7ab8ba8990d40646d0dc2a8482df 1 parent c11f9c3
@aaugustin aaugustin authored
View
17 django/test/client.py
@@ -546,19 +546,18 @@ def _handle_redirects(self, response, **extra):
response.redirect_chain = []
while response.status_code in (301, 302, 303, 307):
url = response['Location']
- scheme, netloc, path, query, fragment = urlsplit(url)
-
redirect_chain = response.redirect_chain
redirect_chain.append((url, response.status_code))
- if scheme:
- extra['wsgi.url_scheme'] = scheme
+ url = urlsplit(url)
+ if url.scheme:
+ extra['wsgi.url_scheme'] = url.scheme
+ if url.hostname:
+ extra['SERVER_NAME'] = url.hostname
+ if url.port:
+ extra['SERVER_PORT'] = str(url.port)
- # The test client doesn't handle external links,
- # but since the situation is simulated in test_client,
- # we fake things here by ignoring the netloc portion of the
- # redirected URL.
- response = self.get(path, QueryDict(query), follow=False, **extra)
+ response = self.get(url.path, QueryDict(url.query), follow=False, **extra)
response.redirect_chain = redirect_chain
# Prevent loops
View
12 tests/regressiontests/test_client_regress/models.py
@@ -368,6 +368,18 @@ def test_redirect_chain_delete(self):
'/test_client_regress/no_template_view/', 301, 200)
self.assertEqual(len(response.redirect_chain), 3)
+ def test_redirect_to_different_host(self):
+ "The test client will preserve scheme, host and port changes"
+ response = self.client.get('/test_client_regress/redirect_other_host/', follow=True)
+ self.assertRedirects(response,
+ 'https://otherserver:8443/test_client_regress/no_template_view/',
+ status_code=301, target_status_code=200)
+ # We can't use is_secure() or get_host()
+ # because response.request is a dictionary, not an HttpRequest
+ self.assertEqual(response.request.get('wsgi.url_scheme'), 'https')
+ self.assertEqual(response.request.get('SERVER_NAME'), 'otherserver')
+ self.assertEqual(response.request.get('SERVER_PORT'), '8443')
+
def test_redirect_chain_on_non_redirect_page(self):
"An assertion is raised if the original page couldn't be retrieved as expected"
# This page will redirect with code 301, not 302
View
1  tests/regressiontests/test_client_regress/urls.py
@@ -23,6 +23,7 @@
(r'^circular_redirect_1/$', RedirectView.as_view(url='/test_client_regress/circular_redirect_2/')),
(r'^circular_redirect_2/$', RedirectView.as_view(url='/test_client_regress/circular_redirect_3/')),
(r'^circular_redirect_3/$', RedirectView.as_view(url='/test_client_regress/circular_redirect_1/')),
+ (r'^redirect_other_host/$', RedirectView.as_view(url='https://otherserver:8443/test_client_regress/no_template_view/')),
(r'^set_session/$', views.set_session_view),
(r'^check_session/$', views.check_session_view),
(r'^request_methods/$', views.request_methods_view),
Please sign in to comment.
Something went wrong with that request. Please try again.