Skip to content

Commit

Permalink
Fixed #16040 -- Preserved scheme, host and port in the test client wh…
Browse files Browse the repository at this point in the history
…en following a redirect.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17157 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
aaugustin committed Nov 27, 2011
1 parent 72085d0 commit bf47376
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
17 changes: 8 additions & 9 deletions django/test/client.py
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/regressiontests/test_client_regress/models.py
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/regressiontests/test_client_regress/urls.py
Expand Up @@ -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),
Expand Down

0 comments on commit bf47376

Please sign in to comment.