When using assertRedirect(), allow the caller to specify relative URLs and
automatically fill in the hostname and scheme (host can be passed in, if different from the default). git-svn-id: http://code.djangoproject.com/svn/django/trunk@6661 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
1eecc5a47e
commit
30848dfe34
|
@ -1,6 +1,6 @@
|
|||
import re
|
||||
import unittest
|
||||
from urlparse import urlsplit
|
||||
from urlparse import urlsplit, urlunsplit
|
||||
|
||||
from django.http import QueryDict
|
||||
from django.db import transaction
|
||||
|
@ -74,7 +74,7 @@ class TestCase(unittest.TestCase):
|
|||
super(TestCase, self).__call__(result)
|
||||
|
||||
def assertRedirects(self, response, expected_url, status_code=302,
|
||||
target_status_code=200):
|
||||
target_status_code=200, host=None):
|
||||
"""Asserts that a response redirected to a specific URL, and that the
|
||||
redirect URL can be loaded.
|
||||
|
||||
|
@ -86,6 +86,10 @@ class TestCase(unittest.TestCase):
|
|||
" (expected %d)" % (response.status_code, status_code)))
|
||||
url = response['Location']
|
||||
scheme, netloc, path, query, fragment = urlsplit(url)
|
||||
e_scheme, e_netloc, e_path, e_query, e_fragment = urlsplit(expected_url)
|
||||
if not (e_scheme or e_netloc):
|
||||
expected_url = urlunsplit(('http', host or 'testserver', e_path,
|
||||
e_query, e_fragment))
|
||||
self.assertEqual(url, expected_url,
|
||||
"Response redirected to '%s', expected '%s'" % (url, expected_url))
|
||||
|
||||
|
|
|
@ -83,13 +83,16 @@ class ClientTest(TestCase):
|
|||
def test_redirect(self):
|
||||
"GET a URL that redirects elsewhere"
|
||||
response = self.client.get('/test_client/redirect_view/')
|
||||
# Check that the response was a 302 (redirect)
|
||||
self.assertRedirects(response, 'http://testserver/test_client/get_view/')
|
||||
# Check that the response was a 302 (redirect) and that
|
||||
# assertRedirect() understands to put an implicit http://testserver/ in
|
||||
# front of non-absolute URLs.
|
||||
self.assertRedirects(response, '/test_client/get_view/')
|
||||
|
||||
client_providing_host = Client(HTTP_HOST='django.testserver')
|
||||
host = 'django.testserver'
|
||||
client_providing_host = Client(HTTP_HOST=host)
|
||||
response = client_providing_host.get('/test_client/redirect_view/')
|
||||
# Check that the response was a 302 (redirect) with absolute URI
|
||||
self.assertRedirects(response, 'http://django.testserver/test_client/get_view/')
|
||||
self.assertRedirects(response, '/test_client/get_view/', host=host)
|
||||
|
||||
def test_redirect_with_query(self):
|
||||
"GET a URL that redirects with given GET parameters"
|
||||
|
|
Loading…
Reference in New Issue