diff options
author | Tim Graham <timograham@gmail.com> | 2016-04-02 10:35:33 -0400 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2016-04-02 10:35:55 -0400 |
commit | 192b06594958b701a4b47dd51148a15357ac8f76 (patch) | |
tree | 8b7d2419093c28700a149e294e07ea3a02080d40 | |
parent | be1ac003794e19796489f0d6b6959c4703053b91 (diff) | |
download | django-192b06594958b701a4b47dd51148a15357ac8f76.tar.gz |
[1.9.x] Fixed #26428 -- Added support for relative path redirects in assertRedirects().
Thanks Trac alias master for the report and review.
Backport of d2569f89f28883d07ede0e44a0a69ae678d3b35f from master
-rw-r--r-- | django/test/testcases.py | 7 | ||||
-rw-r--r-- | docs/releases/1.9.6.txt | 4 | ||||
-rw-r--r-- | tests/test_client/tests.py | 8 | ||||
-rw-r--r-- | tests/test_client/urls.py | 2 |
4 files changed, 19 insertions, 2 deletions
diff --git a/django/test/testcases.py b/django/test/testcases.py index 6b31e974e3..7bcc048ee1 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -44,7 +44,7 @@ from django.utils.deprecation import ( ) from django.utils.encoding import force_text from django.utils.six.moves.urllib.parse import ( - unquote, urlparse, urlsplit, urlunsplit, + unquote, urljoin, urlparse, urlsplit, urlunsplit, ) from django.utils.six.moves.urllib.request import url2pathname from django.views.static import serve @@ -317,6 +317,11 @@ class SimpleTestCase(unittest.TestCase): url = response.url scheme, netloc, path, query, fragment = urlsplit(url) + # Prepend the request path to handle relative path redirects. + if not path.startswith('/'): + url = urljoin(response.request['PATH_INFO'], url) + path = urljoin(response.request['PATH_INFO'], path) + if fetch_redirect_response: redirect_response = response.client.get(path, QueryDict(query), secure=(scheme == 'https')) diff --git a/docs/releases/1.9.6.txt b/docs/releases/1.9.6.txt index 32379496f4..bf2e61a5c1 100644 --- a/docs/releases/1.9.6.txt +++ b/docs/releases/1.9.6.txt @@ -9,4 +9,6 @@ Django 1.9.6 fixes several bugs in 1.9.5. Bugfixes ======== -* ... +* Added support for relative path redirects 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 4b48332085..1836dd49c5 100644 --- a/tests/test_client/tests.py +++ b/tests/test_client/tests.py @@ -632,6 +632,14 @@ class ClientTest(TestCase): # Check some response details self.assertContains(response, 'This is a test') + def test_relative_redirect(self): + response = self.client.get('/accounts/') + self.assertRedirects(response, '/accounts/login/') + + def test_relative_redirect_no_trailing_slash(self): + response = self.client.get('/accounts/no_trailing_slash') + self.assertRedirects(response, '/accounts/login/') + def test_mass_mail_sending(self): "Test that mass mail is redirected to a dummy outbox during test setup" diff --git a/tests/test_client/urls.py b/tests/test_client/urls.py index f605448b5b..a7b0ed310d 100644 --- a/tests/test_client/urls.py +++ b/tests/test_client/urls.py @@ -34,6 +34,8 @@ urlpatterns = [ url(r'^nesting_exception_view/$', views.nesting_exception_view), url(r'^django_project_redirect/$', views.django_project_redirect), + url(r'^accounts/$', RedirectView.as_view(url='login/')), + url(r'^accounts/no_trailing_slash$', RedirectView.as_view(url='login/')), url(r'^accounts/login/$', auth_views.login, {'template_name': 'login.html'}), url(r'^accounts/logout/$', auth_views.logout), ] |