Skip to content

Commit

Permalink
Refs #26428 -- Added support for relative path redirects to the test …
Browse files Browse the repository at this point in the history
…client.

Thanks iktyrrell for the patch.
  • Loading branch information
timgraham committed Apr 29, 2016
1 parent ffb1c53 commit 2f698cd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
9 changes: 7 additions & 2 deletions django/test/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from django.utils.functional import SimpleLazyObject, curry
from django.utils.http import urlencode
from django.utils.itercompat import is_iterable
from django.utils.six.moves.urllib.parse import urlparse, urlsplit
from django.utils.six.moves.urllib.parse import urljoin, urlparse, urlsplit

__all__ = ('Client', 'RedirectCycleError', 'RequestFactory', 'encode_file', 'encode_multipart')

Expand Down Expand Up @@ -699,7 +699,12 @@ def _handle_redirects(self, response, **extra):
if url.port:
extra['SERVER_PORT'] = str(url.port)

response = self.get(url.path, QueryDict(url.query), follow=False, **extra)
# Prepend the request path to handle relative path redirects
path = url.path
if not path.startswith('/'):
path = urljoin(response.request['PATH_INFO'], path)

response = self.get(path, QueryDict(url.query), follow=False, **extra)
response.redirect_chain = redirect_chain

if redirect_chain[-1] in redirect_chain[:-1]:
Expand Down
2 changes: 1 addition & 1 deletion docs/releases/1.9.6.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Django 1.9.6 fixes several bugs in 1.9.5.
Bugfixes
========

* Added support for relative path redirects to
* Added support for relative path redirects to the test client and to
``SimpleTestCase.assertRedirects()`` because Django 1.9 no longer converts
redirects to absolute URIs (:ticket:`26428`).

Expand Down
12 changes: 12 additions & 0 deletions tests/test_client/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,18 @@ def test_follow_redirect(self):
self.assertRedirects(response, '/get_view/', status_code=302, target_status_code=200)
self.assertEqual(len(response.redirect_chain), 2)

def test_follow_relative_redirect(self):
"A URL with a relative redirect can be followed."
response = self.client.get('/accounts/', follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.request['PATH_INFO'], '/accounts/login/')

def test_follow_relative_redirect_no_trailing_slash(self):
"A URL with a relative redirect with no trailing slash can be followed."
response = self.client.get('/accounts/no_trailing_slash', follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.request['PATH_INFO'], '/accounts/login/')

def test_redirect_http(self):
"GET a URL that redirects to an http URI"
response = self.client.get('/http_redirect_view/', follow=True)
Expand Down

0 comments on commit 2f698cd

Please sign in to comment.