Changeset 6031
- Timestamp:
- 08/31/07 06:37:28 (1 year ago)
- Files:
-
- django/trunk/django/test/testcases.py (modified) (2 diffs)
- django/trunk/docs/testing.txt (modified) (1 diff)
- django/trunk/tests/modeltests/test_client/models.py (modified) (3 diffs)
- django/trunk/tests/modeltests/test_client/views.py (modified) (1 diff)
- django/trunk/tests/regressiontests/test_client_regress/models.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/test/testcases.py
r5898 r6031 1 1 import re, unittest 2 from urlparse import urlparse 2 from urlparse import urlsplit 3 from django.http import QueryDict 3 4 from django.db import transaction 4 5 from django.core import mail … … 61 62 super(TestCase, self).__call__(result) 62 63 63 def assertRedirects(self, response, expected_ path, status_code=302, target_status_code=200):64 def assertRedirects(self, response, expected_url, status_code=302, target_status_code=200): 64 65 """Assert that a response redirected to a specific URL, and that the 65 66 redirect URL can be loaded. 66 67 68 Note that assertRedirects won't work for external links since it uses 69 TestClient to do a request. 67 70 """ 68 71 self.assertEqual(response.status_code, status_code, 69 72 "Response didn't redirect as expected: Response code was %d (expected %d)" % 70 73 (response.status_code, status_code)) 71 scheme, netloc, path, params, query, fragment = urlparse(response['Location']) 72 self.assertEqual(path, expected_path, 73 "Response redirected to '%s', expected '%s'" % (path, expected_path)) 74 redirect_response = self.client.get(path) 74 scheme, netloc, path, query, fragment = urlsplit(response['Location']) 75 url = path 76 if query: 77 url += '?' + query 78 if fragment: 79 url += '#' + fragment 80 self.assertEqual(url, expected_url, 81 "Response redirected to '%s', expected '%s'" % (url, expected_url)) 82 83 redirect_response = self.client.get(path, QueryDict(query)) 75 84 self.assertEqual(redirect_response.status_code, target_status_code, 76 85 "Couldn't retrieve redirection page '%s': response code was %d (expected %d)" % django/trunk/docs/testing.txt
r5989 r6031 827 827 the response. 828 828 829 ``assertRedirects(response, expected_ path, status_code=302, target_status_code=200)``829 ``assertRedirects(response, expected_url, status_code=302, target_status_code=200)`` 830 830 Asserts that the response return a ``status_code`` redirect status, 831 it redirected to ``expected_ path`` and the subsequent page was received with832 ``target_status_code``.831 it redirected to ``expected_url`` (including any GET data), and the subsequent 832 page was received with ``target_status_code``. 833 833 834 834 ``assertTemplateUsed(response, template_name)`` django/trunk/tests/modeltests/test_client/models.py
r5917 r6031 87 87 # Check that the response was a 302 (redirect) 88 88 self.assertRedirects(response, '/test_client/get_view/') 89 90 def test_redirect_with_query(self): 91 "GET a URL that redirects with given GET parameters" 92 response = self.client.get('/test_client/redirect_view/', {'var': 'value'}) 93 94 # Check if parameters are intact 95 self.assertRedirects(response, '/test_client/get_view/?var=value') 89 96 90 97 def test_permanent_redirect(self): … … 225 232 # Get the page without logging in. Should result in 302. 226 233 response = self.client.get('/test_client/login_protected_view/') 227 self.assertRedirects(response, '/accounts/login/ ')234 self.assertRedirects(response, '/accounts/login/?next=/test_client/login_protected_view/') 228 235 229 236 # Log in … … 262 269 # Request a page that requires a login 263 270 response = self.client.get('/test_client/login_protected_view/') 264 self.assertRedirects(response, '/accounts/login/ ')271 self.assertRedirects(response, '/accounts/login/?next=/test_client/login_protected_view/') 265 272 266 273 def test_session_modifying_view(self): django/trunk/tests/modeltests/test_client/views.py
r5876 r6031 49 49 def redirect_view(request): 50 50 "A view that redirects all requests to the GET view" 51 return HttpResponseRedirect('/test_client/get_view/') 51 if request.GET: 52 from urllib import urlencode 53 query = '?' + urlencode(request.GET, True) 54 else: 55 query = '' 56 return HttpResponseRedirect('/test_client/get_view/' + query) 52 57 53 58 def double_redirect_view(request): django/trunk/tests/regressiontests/test_client_regress/models.py
r5876 r6031 113 113 except AssertionError, e: 114 114 self.assertEquals(str(e), "Response didn't redirect as expected: Response code was 301 (expected 302)") 115 116 def test_lost_query(self): 117 "An assertion is raised if the redirect location doesn't preserve GET parameters" 118 response = self.client.get('/test_client/redirect_view/', {'var': 'value'}) 119 try: 120 self.assertRedirects(response, '/test_client/get_view/') 121 except AssertionError, e: 122 self.assertEquals(str(e), "Response redirected to '/test_client/get_view/?var=value', expected '/test_client/get_view/'") 115 123 116 124 def test_incorrect_target(self):
