mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
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:
@@ -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))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user