summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--django/test/client.py9
-rw-r--r--docs/releases/1.9.6.txt2
-rw-r--r--tests/test_client/tests.py12
3 files changed, 20 insertions, 3 deletions
diff --git a/django/test/client.py b/django/test/client.py
index 6730ac7acd..8f2e6f3963 100644
--- a/django/test/client.py
+++ b/django/test/client.py
@@ -27,7 +27,7 @@ from django.utils.encoding import force_bytes, force_str, uri_to_iri
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')
@@ -678,7 +678,12 @@ class Client(RequestFactory):
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]:
diff --git a/docs/releases/1.9.6.txt b/docs/releases/1.9.6.txt
index 1387aa9c4e..7e3f6aed6a 100644
--- a/docs/releases/1.9.6.txt
+++ b/docs/releases/1.9.6.txt
@@ -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`).
diff --git a/tests/test_client/tests.py b/tests/test_client/tests.py
index 1836dd49c5..9b9c11d9c6 100644
--- a/tests/test_client/tests.py
+++ b/tests/test_client/tests.py
@@ -217,6 +217,18 @@ class ClientTest(TestCase):
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)