Fixed #18558 -- Added url property to HttpResponseRedirect*

Thanks coolRR for the report.
This commit is contained in:
Hiroki Kiyohara 2013-02-13 09:55:43 +01:00 committed by Claude Paroz
parent 3a002db6f1
commit e94f405d94
15 changed files with 82 additions and 66 deletions

View File

@ -306,6 +306,7 @@ answer newbie questions, and generally made Django that much better:
Garth Kidd <http://www.deadlybloodyserious.com/> Garth Kidd <http://www.deadlybloodyserious.com/>
kilian <kilian.cavalotti@lip6.fr> kilian <kilian.cavalotti@lip6.fr>
Sune Kirkeby <http://ibofobi.dk/> Sune Kirkeby <http://ibofobi.dk/>
Hiroki Kiyohara <hirokiky@gmail.com>
Bastian Kleineidam <calvin@debian.org> Bastian Kleineidam <calvin@debian.org>
Cameron Knight (ckknight) Cameron Knight (ckknight)
Nena Kojadin <nena@kiberpipa.org> Nena Kojadin <nena@kiberpipa.org>

View File

@ -34,7 +34,7 @@ class LoginRequiredTestCase(AuthViewsTestCase):
""" """
response = self.client.get(view_url) response = self.client.get(view_url)
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertTrue(login_url in response['Location']) self.assertTrue(login_url in response.url)
self.login() self.login()
response = self.client.get(view_url) response = self.client.get(view_url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)

View File

@ -46,7 +46,7 @@ class AuthViewsTestCase(TestCase):
'password': password, 'password': password,
}) })
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertTrue(response['Location'].endswith(settings.LOGIN_REDIRECT_URL)) self.assertTrue(response.url.endswith(settings.LOGIN_REDIRECT_URL))
self.assertTrue(SESSION_KEY in self.client.session) self.assertTrue(SESSION_KEY in self.client.session)
def assertContainsEscaped(self, response, text, **kwargs): def assertContainsEscaped(self, response, text, **kwargs):
@ -281,7 +281,7 @@ class ChangePasswordTest(AuthViewsTestCase):
'new_password2': 'password1', 'new_password2': 'password1',
}) })
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertTrue(response['Location'].endswith('/password_change/done/')) self.assertTrue(response.url.endswith('/password_change/done/'))
self.fail_login() self.fail_login()
self.login(password='password1') self.login(password='password1')
@ -293,13 +293,13 @@ class ChangePasswordTest(AuthViewsTestCase):
'new_password2': 'password1', 'new_password2': 'password1',
}) })
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertTrue(response['Location'].endswith('/password_change/done/')) self.assertTrue(response.url.endswith('/password_change/done/'))
def test_password_change_done_fails(self): def test_password_change_done_fails(self):
with self.settings(LOGIN_URL='/login/'): with self.settings(LOGIN_URL='/login/'):
response = self.client.get('/password_change/done/') response = self.client.get('/password_change/done/')
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertTrue(response['Location'].endswith('/login/?next=/password_change/done/')) self.assertTrue(response.url.endswith('/login/?next=/password_change/done/'))
@skipIfCustomUser @skipIfCustomUser
@ -336,7 +336,7 @@ class LoginTest(AuthViewsTestCase):
'password': password, 'password': password,
}) })
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertFalse(bad_url in response['Location'], self.assertFalse(bad_url in response.url,
"%s should be blocked" % bad_url) "%s should be blocked" % bad_url)
# These URLs *should* still pass the security check # These URLs *should* still pass the security check
@ -357,7 +357,7 @@ class LoginTest(AuthViewsTestCase):
'password': password, 'password': password,
}) })
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertTrue(good_url in response['Location'], self.assertTrue(good_url in response.url,
"%s should be allowed" % good_url) "%s should be allowed" % good_url)
@ -376,7 +376,7 @@ class LoginURLSettings(AuthViewsTestCase):
settings.LOGIN_URL = login_url settings.LOGIN_URL = login_url
response = self.client.get('/login_required/') response = self.client.get('/login_required/')
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
return response['Location'] return response.url
def test_standard_login_url(self): def test_standard_login_url(self):
login_url = '/login/' login_url = '/login/'
@ -444,11 +444,11 @@ class LogoutTest(AuthViewsTestCase):
self.login() self.login()
response = self.client.get('/logout/next_page/') response = self.client.get('/logout/next_page/')
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertTrue(response['Location'].endswith('/somewhere/')) self.assertTrue(response.url.endswith('/somewhere/'))
response = self.client.get('/logout/next_page/?next=/login/') response = self.client.get('/logout/next_page/?next=/login/')
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertTrue(response['Location'].endswith('/login/')) self.assertTrue(response.url.endswith('/login/'))
self.confirm_logged_out() self.confirm_logged_out()
@ -457,7 +457,7 @@ class LogoutTest(AuthViewsTestCase):
self.login() self.login()
response = self.client.get('/logout/next_page/') response = self.client.get('/logout/next_page/')
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertTrue(response['Location'].endswith('/somewhere/')) self.assertTrue(response.url.endswith('/somewhere/'))
self.confirm_logged_out() self.confirm_logged_out()
def test_logout_with_redirect_argument(self): def test_logout_with_redirect_argument(self):
@ -465,7 +465,7 @@ class LogoutTest(AuthViewsTestCase):
self.login() self.login()
response = self.client.get('/logout/?next=/login/') response = self.client.get('/logout/?next=/login/')
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertTrue(response['Location'].endswith('/login/')) self.assertTrue(response.url.endswith('/login/'))
self.confirm_logged_out() self.confirm_logged_out()
def test_logout_with_custom_redirect_argument(self): def test_logout_with_custom_redirect_argument(self):
@ -473,7 +473,7 @@ class LogoutTest(AuthViewsTestCase):
self.login() self.login()
response = self.client.get('/logout/custom_query/?follow=/somewhere/') response = self.client.get('/logout/custom_query/?follow=/somewhere/')
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertTrue(response['Location'].endswith('/somewhere/')) self.assertTrue(response.url.endswith('/somewhere/'))
self.confirm_logged_out() self.confirm_logged_out()
def test_security_check(self, password='password'): def test_security_check(self, password='password'):
@ -492,7 +492,7 @@ class LogoutTest(AuthViewsTestCase):
self.login() self.login()
response = self.client.get(nasty_url) response = self.client.get(nasty_url)
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertFalse(bad_url in response['Location'], self.assertFalse(bad_url in response.url,
"%s should be blocked" % bad_url) "%s should be blocked" % bad_url)
self.confirm_logged_out() self.confirm_logged_out()
@ -512,6 +512,6 @@ class LogoutTest(AuthViewsTestCase):
self.login() self.login()
response = self.client.get(safe_url) response = self.client.get(safe_url)
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertTrue(good_url in response['Location'], self.assertTrue(good_url in response.url,
"%s should be allowed" % good_url) "%s should be allowed" % good_url)
self.confirm_logged_out() self.confirm_logged_out()

View File

@ -21,7 +21,7 @@ class NamedWizardTests(object):
def test_initial_call(self): def test_initial_call(self):
response = self.client.get(reverse('%s_start' % self.wizard_urlname)) response = self.client.get(reverse('%s_start' % self.wizard_urlname))
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
response = self.client.get(response['Location']) response = self.client.get(response.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
wizard = response.context['wizard'] wizard = response.context['wizard']
self.assertEqual(wizard['steps'].current, 'form1') self.assertEqual(wizard['steps'].current, 'form1')
@ -40,7 +40,7 @@ class NamedWizardTests(object):
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
# Test for proper redirect GET parameters # Test for proper redirect GET parameters
location = response['Location'] location = response.url
self.assertNotEqual(location.find('?'), -1) self.assertNotEqual(location.find('?'), -1)
querydict = QueryDict(location[location.find('?') + 1:]) querydict = QueryDict(location[location.find('?') + 1:])
self.assertEqual(dict(querydict.items()), get_params) self.assertEqual(dict(querydict.items()), get_params)
@ -60,7 +60,7 @@ class NamedWizardTests(object):
response = self.client.post( response = self.client.post(
reverse(self.wizard_urlname, kwargs={'step': 'form1'}), reverse(self.wizard_urlname, kwargs={'step': 'form1'}),
self.wizard_step_data[0]) self.wizard_step_data[0])
response = self.client.get(response['Location']) response = self.client.get(response.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
wizard = response.context['wizard'] wizard = response.context['wizard']
@ -79,7 +79,7 @@ class NamedWizardTests(object):
response = self.client.post( response = self.client.post(
reverse(self.wizard_urlname, kwargs={'step': 'form1'}), reverse(self.wizard_urlname, kwargs={'step': 'form1'}),
self.wizard_step_data[0]) self.wizard_step_data[0])
response = self.client.get(response['Location']) response = self.client.get(response.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['wizard']['steps'].current, 'form2') self.assertEqual(response.context['wizard']['steps'].current, 'form2')
@ -88,7 +88,7 @@ class NamedWizardTests(object):
reverse(self.wizard_urlname, kwargs={ reverse(self.wizard_urlname, kwargs={
'step': response.context['wizard']['steps'].current 'step': response.context['wizard']['steps'].current
}), {'wizard_goto_step': response.context['wizard']['steps'].prev}) }), {'wizard_goto_step': response.context['wizard']['steps'].prev})
response = self.client.get(response['Location']) response = self.client.get(response.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['wizard']['steps'].current, 'form1') self.assertEqual(response.context['wizard']['steps'].current, 'form1')
@ -116,7 +116,7 @@ class NamedWizardTests(object):
reverse(self.wizard_urlname, reverse(self.wizard_urlname,
kwargs={'step': response.context['wizard']['steps'].current}), kwargs={'step': response.context['wizard']['steps'].current}),
self.wizard_step_data[0]) self.wizard_step_data[0])
response = self.client.get(response['Location']) response = self.client.get(response.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['wizard']['steps'].current, 'form2') self.assertEqual(response.context['wizard']['steps'].current, 'form2')
@ -128,7 +128,7 @@ class NamedWizardTests(object):
reverse(self.wizard_urlname, reverse(self.wizard_urlname,
kwargs={'step': response.context['wizard']['steps'].current}), kwargs={'step': response.context['wizard']['steps'].current}),
post_data) post_data)
response = self.client.get(response['Location']) response = self.client.get(response.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['wizard']['steps'].current, 'form3') self.assertEqual(response.context['wizard']['steps'].current, 'form3')
@ -137,7 +137,7 @@ class NamedWizardTests(object):
reverse(self.wizard_urlname, reverse(self.wizard_urlname,
kwargs={'step': response.context['wizard']['steps'].current}), kwargs={'step': response.context['wizard']['steps'].current}),
self.wizard_step_data[2]) self.wizard_step_data[2])
response = self.client.get(response['Location']) response = self.client.get(response.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['wizard']['steps'].current, 'form4') self.assertEqual(response.context['wizard']['steps'].current, 'form4')
@ -146,7 +146,7 @@ class NamedWizardTests(object):
reverse(self.wizard_urlname, reverse(self.wizard_urlname,
kwargs={'step': response.context['wizard']['steps'].current}), kwargs={'step': response.context['wizard']['steps'].current}),
self.wizard_step_data[3]) self.wizard_step_data[3])
response = self.client.get(response['Location']) response = self.client.get(response.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
all_data = response.context['form_list'] all_data = response.context['form_list']
@ -169,7 +169,7 @@ class NamedWizardTests(object):
reverse(self.wizard_urlname, reverse(self.wizard_urlname,
kwargs={'step': response.context['wizard']['steps'].current}), kwargs={'step': response.context['wizard']['steps'].current}),
self.wizard_step_data[0]) self.wizard_step_data[0])
response = self.client.get(response['Location']) response = self.client.get(response.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
post_data = self.wizard_step_data[1] post_data = self.wizard_step_data[1]
@ -178,7 +178,7 @@ class NamedWizardTests(object):
reverse(self.wizard_urlname, reverse(self.wizard_urlname,
kwargs={'step': response.context['wizard']['steps'].current}), kwargs={'step': response.context['wizard']['steps'].current}),
post_data) post_data)
response = self.client.get(response['Location']) response = self.client.get(response.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
step2_url = reverse(self.wizard_urlname, kwargs={'step': 'form2'}) step2_url = reverse(self.wizard_urlname, kwargs={'step': 'form2'})
@ -194,14 +194,14 @@ class NamedWizardTests(object):
reverse(self.wizard_urlname, reverse(self.wizard_urlname,
kwargs={'step': response.context['wizard']['steps'].current}), kwargs={'step': response.context['wizard']['steps'].current}),
self.wizard_step_data[2]) self.wizard_step_data[2])
response = self.client.get(response['Location']) response = self.client.get(response.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.post( response = self.client.post(
reverse(self.wizard_urlname, reverse(self.wizard_urlname,
kwargs={'step': response.context['wizard']['steps'].current}), kwargs={'step': response.context['wizard']['steps'].current}),
self.wizard_step_data[3]) self.wizard_step_data[3])
response = self.client.get(response['Location']) response = self.client.get(response.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
all_data = response.context['all_cleaned_data'] all_data = response.context['all_cleaned_data']
@ -227,7 +227,7 @@ class NamedWizardTests(object):
reverse(self.wizard_urlname, reverse(self.wizard_urlname,
kwargs={'step': response.context['wizard']['steps'].current}), kwargs={'step': response.context['wizard']['steps'].current}),
self.wizard_step_data[0]) self.wizard_step_data[0])
response = self.client.get(response['Location']) response = self.client.get(response.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
post_data = self.wizard_step_data[1] post_data = self.wizard_step_data[1]
@ -237,14 +237,14 @@ class NamedWizardTests(object):
reverse(self.wizard_urlname, reverse(self.wizard_urlname,
kwargs={'step': response.context['wizard']['steps'].current}), kwargs={'step': response.context['wizard']['steps'].current}),
post_data) post_data)
response = self.client.get(response['Location']) response = self.client.get(response.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.post( response = self.client.post(
reverse(self.wizard_urlname, reverse(self.wizard_urlname,
kwargs={'step': response.context['wizard']['steps'].current}), kwargs={'step': response.context['wizard']['steps'].current}),
self.wizard_step_data[2]) self.wizard_step_data[2])
loc = response['Location'] loc = response.url
response = self.client.get(loc) response = self.client.get(loc)
self.assertEqual(response.status_code, 200, loc) self.assertEqual(response.status_code, 200, loc)
@ -263,7 +263,7 @@ class NamedWizardTests(object):
response = self.client.post( response = self.client.post(
reverse(self.wizard_urlname, kwargs={'step': 'form1'}), reverse(self.wizard_urlname, kwargs={'step': 'form1'}),
self.wizard_step_data[0]) self.wizard_step_data[0])
response = self.client.get(response['Location']) response = self.client.get(response.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['wizard']['steps'].current, 'form2') self.assertEqual(response.context['wizard']['steps'].current, 'form2')
@ -271,7 +271,7 @@ class NamedWizardTests(object):
'%s?reset=1' % reverse('%s_start' % self.wizard_urlname)) '%s?reset=1' % reverse('%s_start' % self.wizard_urlname))
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
response = self.client.get(response['Location']) response = self.client.get(response.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['wizard']['steps'].current, 'form1') self.assertEqual(response.context['wizard']['steps'].current, 'form1')

View File

@ -392,6 +392,8 @@ class HttpResponseRedirectBase(HttpResponse):
super(HttpResponseRedirectBase, self).__init__(*args, **kwargs) super(HttpResponseRedirectBase, self).__init__(*args, **kwargs)
self['Location'] = iri_to_uri(redirect_to) self['Location'] = iri_to_uri(redirect_to)
url = property(lambda self: self['Location'])
class HttpResponseRedirect(HttpResponseRedirectBase): class HttpResponseRedirect(HttpResponseRedirectBase):
status_code = 302 status_code = 302

View File

@ -580,7 +580,7 @@ class Client(RequestFactory):
response.redirect_chain = [] response.redirect_chain = []
while response.status_code in (301, 302, 303, 307): while response.status_code in (301, 302, 303, 307):
url = response['Location'] url = response.url
redirect_chain = response.redirect_chain redirect_chain = response.redirect_chain
redirect_chain.append((url, response.status_code)) redirect_chain.append((url, response.status_code))

View File

@ -601,7 +601,7 @@ class TransactionTestCase(SimpleTestCase):
" code was %d (expected %d)" % " code was %d (expected %d)" %
(response.status_code, status_code)) (response.status_code, status_code))
url = response['Location'] url = response.url
scheme, netloc, path, query, fragment = urlsplit(url) scheme, netloc, path, query, fragment = urlsplit(url)
redirect_response = response.client.get(path, QueryDict(query)) redirect_response = response.client.get(path, QueryDict(query))

View File

@ -746,6 +746,13 @@ types of HTTP responses. Like ``HttpResponse``, these subclasses live in
domain (e.g. ``'/search/'``). See :class:`HttpResponse` for other optional domain (e.g. ``'/search/'``). See :class:`HttpResponse` for other optional
constructor arguments. Note that this returns an HTTP status code 302. constructor arguments. Note that this returns an HTTP status code 302.
.. attribute:: HttpResponseRedirect.url
.. versionadded:: 1.6
This read-only attribute represents the URL the response will redirect
to (equivalent to the ``Location`` response header).
.. class:: HttpResponsePermanentRedirect .. class:: HttpResponsePermanentRedirect
Like :class:`HttpResponseRedirect`, but it returns a permanent redirect Like :class:`HttpResponseRedirect`, but it returns a permanent redirect

View File

@ -68,6 +68,10 @@ Minor features
:class:`~django.views.generic.edit.DeletionMixin` is now interpolated with :class:`~django.views.generic.edit.DeletionMixin` is now interpolated with
its ``object``\'s ``__dict__``. its ``object``\'s ``__dict__``.
* :class:`~django.http.HttpResponseRedirect` and
:class:`~django.http.HttpResponsePermanentRedirect` now provide an ``url``
attribute (equivalent to the URL the response will redirect to).
Backwards incompatible changes in 1.6 Backwards incompatible changes in 1.6
===================================== =====================================

View File

@ -1641,7 +1641,7 @@ class SecureViewTests(TestCase):
response = self.client.get(shortcut_url, follow=False) response = self.client.get(shortcut_url, follow=False)
# Can't use self.assertRedirects() because User.get_absolute_url() is silly. # Can't use self.assertRedirects() because User.get_absolute_url() is silly.
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response['Location'], 'http://example.com/users/super/') self.assertEqual(response.url, 'http://example.com/users/super/')
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',)) @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))

View File

@ -329,66 +329,66 @@ class RedirectViewTest(unittest.TestCase):
"Default is a permanent redirect" "Default is a permanent redirect"
response = RedirectView.as_view(url='/bar/')(self.rf.get('/foo/')) response = RedirectView.as_view(url='/bar/')(self.rf.get('/foo/'))
self.assertEqual(response.status_code, 301) self.assertEqual(response.status_code, 301)
self.assertEqual(response['Location'], '/bar/') self.assertEqual(response.url, '/bar/')
def test_temporary_redirect(self): def test_temporary_redirect(self):
"Permanent redirects are an option" "Permanent redirects are an option"
response = RedirectView.as_view(url='/bar/', permanent=False)(self.rf.get('/foo/')) response = RedirectView.as_view(url='/bar/', permanent=False)(self.rf.get('/foo/'))
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response['Location'], '/bar/') self.assertEqual(response.url, '/bar/')
def test_include_args(self): def test_include_args(self):
"GET arguments can be included in the redirected URL" "GET arguments can be included in the redirected URL"
response = RedirectView.as_view(url='/bar/')(self.rf.get('/foo/')) response = RedirectView.as_view(url='/bar/')(self.rf.get('/foo/'))
self.assertEqual(response.status_code, 301) self.assertEqual(response.status_code, 301)
self.assertEqual(response['Location'], '/bar/') self.assertEqual(response.url, '/bar/')
response = RedirectView.as_view(url='/bar/', query_string=True)(self.rf.get('/foo/?pork=spam')) response = RedirectView.as_view(url='/bar/', query_string=True)(self.rf.get('/foo/?pork=spam'))
self.assertEqual(response.status_code, 301) self.assertEqual(response.status_code, 301)
self.assertEqual(response['Location'], '/bar/?pork=spam') self.assertEqual(response.url, '/bar/?pork=spam')
def test_include_urlencoded_args(self): def test_include_urlencoded_args(self):
"GET arguments can be URL-encoded when included in the redirected URL" "GET arguments can be URL-encoded when included in the redirected URL"
response = RedirectView.as_view(url='/bar/', query_string=True)( response = RedirectView.as_view(url='/bar/', query_string=True)(
self.rf.get('/foo/?unicode=%E2%9C%93')) self.rf.get('/foo/?unicode=%E2%9C%93'))
self.assertEqual(response.status_code, 301) self.assertEqual(response.status_code, 301)
self.assertEqual(response['Location'], '/bar/?unicode=%E2%9C%93') self.assertEqual(response.url, '/bar/?unicode=%E2%9C%93')
def test_parameter_substitution(self): def test_parameter_substitution(self):
"Redirection URLs can be parameterized" "Redirection URLs can be parameterized"
response = RedirectView.as_view(url='/bar/%(object_id)d/')(self.rf.get('/foo/42/'), object_id=42) response = RedirectView.as_view(url='/bar/%(object_id)d/')(self.rf.get('/foo/42/'), object_id=42)
self.assertEqual(response.status_code, 301) self.assertEqual(response.status_code, 301)
self.assertEqual(response['Location'], '/bar/42/') self.assertEqual(response.url, '/bar/42/')
def test_redirect_POST(self): def test_redirect_POST(self):
"Default is a permanent redirect" "Default is a permanent redirect"
response = RedirectView.as_view(url='/bar/')(self.rf.post('/foo/')) response = RedirectView.as_view(url='/bar/')(self.rf.post('/foo/'))
self.assertEqual(response.status_code, 301) self.assertEqual(response.status_code, 301)
self.assertEqual(response['Location'], '/bar/') self.assertEqual(response.url, '/bar/')
def test_redirect_HEAD(self): def test_redirect_HEAD(self):
"Default is a permanent redirect" "Default is a permanent redirect"
response = RedirectView.as_view(url='/bar/')(self.rf.head('/foo/')) response = RedirectView.as_view(url='/bar/')(self.rf.head('/foo/'))
self.assertEqual(response.status_code, 301) self.assertEqual(response.status_code, 301)
self.assertEqual(response['Location'], '/bar/') self.assertEqual(response.url, '/bar/')
def test_redirect_OPTIONS(self): def test_redirect_OPTIONS(self):
"Default is a permanent redirect" "Default is a permanent redirect"
response = RedirectView.as_view(url='/bar/')(self.rf.options('/foo/')) response = RedirectView.as_view(url='/bar/')(self.rf.options('/foo/'))
self.assertEqual(response.status_code, 301) self.assertEqual(response.status_code, 301)
self.assertEqual(response['Location'], '/bar/') self.assertEqual(response.url, '/bar/')
def test_redirect_PUT(self): def test_redirect_PUT(self):
"Default is a permanent redirect" "Default is a permanent redirect"
response = RedirectView.as_view(url='/bar/')(self.rf.put('/foo/')) response = RedirectView.as_view(url='/bar/')(self.rf.put('/foo/'))
self.assertEqual(response.status_code, 301) self.assertEqual(response.status_code, 301)
self.assertEqual(response['Location'], '/bar/') self.assertEqual(response.url, '/bar/')
def test_redirect_DELETE(self): def test_redirect_DELETE(self):
"Default is a permanent redirect" "Default is a permanent redirect"
response = RedirectView.as_view(url='/bar/')(self.rf.delete('/foo/')) response = RedirectView.as_view(url='/bar/')(self.rf.delete('/foo/'))
self.assertEqual(response.status_code, 301) self.assertEqual(response.status_code, 301)
self.assertEqual(response['Location'], '/bar/') self.assertEqual(response.url, '/bar/')
def test_redirect_when_meta_contains_no_query_string(self): def test_redirect_when_meta_contains_no_query_string(self):
"regression for #16705" "regression for #16705"

View File

@ -410,6 +410,8 @@ class HttpResponseSubclassesTests(TestCase):
content='The resource has temporarily moved', content='The resource has temporarily moved',
content_type='text/html') content_type='text/html')
self.assertContains(response, 'The resource has temporarily moved', status_code=302) self.assertContains(response, 'The resource has temporarily moved', status_code=302)
# Test that url attribute is right
self.assertEqual(response.url, response['Location'])
def test_not_modified(self): def test_not_modified(self):
response = HttpResponseNotModified() response = HttpResponseNotModified()

View File

@ -69,7 +69,7 @@ class CommonMiddlewareTest(TestCase):
request = self._get_request('slash') request = self._get_request('slash')
r = CommonMiddleware().process_request(request) r = CommonMiddleware().process_request(request)
self.assertEqual(r.status_code, 301) self.assertEqual(r.status_code, 301)
self.assertEqual(r['Location'], 'http://testserver/middleware/slash/') self.assertEqual(r.url, 'http://testserver/middleware/slash/')
@override_settings(APPEND_SLASH=True, DEBUG=True) @override_settings(APPEND_SLASH=True, DEBUG=True)
def test_append_slash_no_redirect_on_POST_in_DEBUG(self): def test_append_slash_no_redirect_on_POST_in_DEBUG(self):
@ -101,7 +101,7 @@ class CommonMiddlewareTest(TestCase):
r = CommonMiddleware().process_request(request) r = CommonMiddleware().process_request(request)
self.assertEqual(r.status_code, 301) self.assertEqual(r.status_code, 301)
self.assertEqual( self.assertEqual(
r['Location'], r.url,
'http://testserver/middleware/needsquoting%23/') 'http://testserver/middleware/needsquoting%23/')
@override_settings(APPEND_SLASH=False, PREPEND_WWW=True) @override_settings(APPEND_SLASH=False, PREPEND_WWW=True)
@ -110,7 +110,7 @@ class CommonMiddlewareTest(TestCase):
r = CommonMiddleware().process_request(request) r = CommonMiddleware().process_request(request)
self.assertEqual(r.status_code, 301) self.assertEqual(r.status_code, 301)
self.assertEqual( self.assertEqual(
r['Location'], r.url,
'http://www.testserver/middleware/path/') 'http://www.testserver/middleware/path/')
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True) @override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
@ -118,7 +118,7 @@ class CommonMiddlewareTest(TestCase):
request = self._get_request('slash/') request = self._get_request('slash/')
r = CommonMiddleware().process_request(request) r = CommonMiddleware().process_request(request)
self.assertEqual(r.status_code, 301) self.assertEqual(r.status_code, 301)
self.assertEqual(r['Location'], self.assertEqual(r.url,
'http://www.testserver/middleware/slash/') 'http://www.testserver/middleware/slash/')
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True) @override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
@ -126,7 +126,7 @@ class CommonMiddlewareTest(TestCase):
request = self._get_request('slash') request = self._get_request('slash')
r = CommonMiddleware().process_request(request) r = CommonMiddleware().process_request(request)
self.assertEqual(r.status_code, 301) self.assertEqual(r.status_code, 301)
self.assertEqual(r['Location'], self.assertEqual(r.url,
'http://www.testserver/middleware/slash/') 'http://www.testserver/middleware/slash/')
@ -171,7 +171,7 @@ class CommonMiddlewareTest(TestCase):
self.assertFalse(r is None, self.assertFalse(r is None,
"CommonMiddlware failed to return APPEND_SLASH redirect using request.urlconf") "CommonMiddlware failed to return APPEND_SLASH redirect using request.urlconf")
self.assertEqual(r.status_code, 301) self.assertEqual(r.status_code, 301)
self.assertEqual(r['Location'], 'http://testserver/middleware/customurlconf/slash/') self.assertEqual(r.url, 'http://testserver/middleware/customurlconf/slash/')
@override_settings(APPEND_SLASH=True, DEBUG=True) @override_settings(APPEND_SLASH=True, DEBUG=True)
def test_append_slash_no_redirect_on_POST_in_DEBUG_custom_urlconf(self): def test_append_slash_no_redirect_on_POST_in_DEBUG_custom_urlconf(self):
@ -208,7 +208,7 @@ class CommonMiddlewareTest(TestCase):
"CommonMiddlware failed to return APPEND_SLASH redirect using request.urlconf") "CommonMiddlware failed to return APPEND_SLASH redirect using request.urlconf")
self.assertEqual(r.status_code, 301) self.assertEqual(r.status_code, 301)
self.assertEqual( self.assertEqual(
r['Location'], r.url,
'http://testserver/middleware/customurlconf/needsquoting%23/') 'http://testserver/middleware/customurlconf/needsquoting%23/')
@override_settings(APPEND_SLASH=False, PREPEND_WWW=True) @override_settings(APPEND_SLASH=False, PREPEND_WWW=True)
@ -218,7 +218,7 @@ class CommonMiddlewareTest(TestCase):
r = CommonMiddleware().process_request(request) r = CommonMiddleware().process_request(request)
self.assertEqual(r.status_code, 301) self.assertEqual(r.status_code, 301)
self.assertEqual( self.assertEqual(
r['Location'], r.url,
'http://www.testserver/middleware/customurlconf/path/') 'http://www.testserver/middleware/customurlconf/path/')
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True) @override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
@ -227,7 +227,7 @@ class CommonMiddlewareTest(TestCase):
request.urlconf = 'regressiontests.middleware.extra_urls' request.urlconf = 'regressiontests.middleware.extra_urls'
r = CommonMiddleware().process_request(request) r = CommonMiddleware().process_request(request)
self.assertEqual(r.status_code, 301) self.assertEqual(r.status_code, 301)
self.assertEqual(r['Location'], self.assertEqual(r.url,
'http://www.testserver/middleware/customurlconf/slash/') 'http://www.testserver/middleware/customurlconf/slash/')
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True) @override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
@ -236,7 +236,7 @@ class CommonMiddlewareTest(TestCase):
request.urlconf = 'regressiontests.middleware.extra_urls' request.urlconf = 'regressiontests.middleware.extra_urls'
r = CommonMiddleware().process_request(request) r = CommonMiddleware().process_request(request)
self.assertEqual(r.status_code, 301) self.assertEqual(r.status_code, 301)
self.assertEqual(r['Location'], self.assertEqual(r.url,
'http://www.testserver/middleware/customurlconf/slash/') 'http://www.testserver/middleware/customurlconf/slash/')
# Legacy tests for the 404 error reporting via email (to be removed in 1.8) # Legacy tests for the 404 error reporting via email (to be removed in 1.8)

View File

@ -270,31 +270,31 @@ class ReverseShortcutTests(TestCase):
res = redirect(FakeObj()) res = redirect(FakeObj())
self.assertTrue(isinstance(res, HttpResponseRedirect)) self.assertTrue(isinstance(res, HttpResponseRedirect))
self.assertEqual(res['Location'], '/hi-there/') self.assertEqual(res.url, '/hi-there/')
res = redirect(FakeObj(), permanent=True) res = redirect(FakeObj(), permanent=True)
self.assertTrue(isinstance(res, HttpResponsePermanentRedirect)) self.assertTrue(isinstance(res, HttpResponsePermanentRedirect))
self.assertEqual(res['Location'], '/hi-there/') self.assertEqual(res.url, '/hi-there/')
def test_redirect_to_view_name(self): def test_redirect_to_view_name(self):
res = redirect('hardcoded2') res = redirect('hardcoded2')
self.assertEqual(res['Location'], '/hardcoded/doc.pdf') self.assertEqual(res.url, '/hardcoded/doc.pdf')
res = redirect('places', 1) res = redirect('places', 1)
self.assertEqual(res['Location'], '/places/1/') self.assertEqual(res.url, '/places/1/')
res = redirect('headlines', year='2008', month='02', day='17') res = redirect('headlines', year='2008', month='02', day='17')
self.assertEqual(res['Location'], '/headlines/2008.02.17/') self.assertEqual(res.url, '/headlines/2008.02.17/')
self.assertRaises(NoReverseMatch, redirect, 'not-a-view') self.assertRaises(NoReverseMatch, redirect, 'not-a-view')
def test_redirect_to_url(self): def test_redirect_to_url(self):
res = redirect('/foo/') res = redirect('/foo/')
self.assertEqual(res['Location'], '/foo/') self.assertEqual(res.url, '/foo/')
res = redirect('http://example.com/') res = redirect('http://example.com/')
self.assertEqual(res['Location'], 'http://example.com/') self.assertEqual(res.url, 'http://example.com/')
def test_redirect_view_object(self): def test_redirect_view_object(self):
from .views import absolute_kwargs_view from .views import absolute_kwargs_view
res = redirect(absolute_kwargs_view) res = redirect(absolute_kwargs_view)
self.assertEqual(res['Location'], '/absolute_arg_view/') self.assertEqual(res.url, '/absolute_arg_view/')
self.assertRaises(NoReverseMatch, redirect, absolute_kwargs_view, wrong_argument=None) self.assertRaises(NoReverseMatch, redirect, absolute_kwargs_view, wrong_argument=None)

View File

@ -44,7 +44,7 @@ class I18NTests(TestCase):
lang_code, lang_name = settings.LANGUAGES[0] lang_code, lang_name = settings.LANGUAGES[0]
post_data = dict(language=lang_code, next='//unsafe/redirection/') post_data = dict(language=lang_code, next='//unsafe/redirection/')
response = self.client.post('/views/i18n/setlang/', data=post_data) response = self.client.post('/views/i18n/setlang/', data=post_data)
self.assertEqual(response['Location'], 'http://testserver/') self.assertEqual(response.url, 'http://testserver/')
self.assertEqual(self.client.session['django_language'], lang_code) self.assertEqual(self.client.session['django_language'], lang_code)
def test_setlang_reversal(self): def test_setlang_reversal(self):