Ticket #4968: 4968.diff
File 4968.diff, 5.0 KB (added by , 17 years ago) |
---|
-
django/test/testcases.py
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 management, mail 5 6 from django.db.models import get_apps … … 57 58 self._pre_setup() 58 59 super(TestCase, self).__call__(result) 59 60 60 def assertRedirects(self, response, expected_ path, status_code=302, target_status_code=200):61 def assertRedirects(self, response, expected_url, status_code=302, target_status_code=200): 61 62 """Assert that a response redirected to a specific URL, and that the 62 63 redirect URL can be loaded. 63 64 65 Note that assertRedirects won't work for external links since it uses 66 TestClient to do a request. 64 67 """ 65 68 self.assertEqual(response.status_code, status_code, 66 69 "Response didn't redirect as expected: Response code was %d (expected %d)" % 67 70 (response.status_code, status_code)) 68 scheme, netloc, path, params, query, fragment = urlparse(response['Location']) 69 self.assertEqual(path, expected_path, 70 "Response redirected to '%s', expected '%s'" % (path, expected_path)) 71 redirect_response = self.client.get(path) 71 scheme, netloc, path, query, fragment = urlsplit(response['Location']) 72 url = path 73 if query: 74 url += '?' + query 75 if fragment: 76 url += '#' + fragment 77 self.assertEqual(url, expected_url, 78 "Response redirected to '%s', expected '%s'" % (url, expected_url)) 79 80 redirect_response = self.client.get(path, QueryDict(query)) 72 81 self.assertEqual(redirect_response.status_code, target_status_code, 73 82 "Couldn't retrieve redirection page '%s': response code was %d (expected %d)" % 74 83 (path, redirect_response.status_code, target_status_code)) -
tests/modeltests/test_client/views.py
48 48 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): 54 59 "A view that redirects all requests to a redirection view" -
tests/modeltests/test_client/models.py
86 86 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): 91 98 "GET a URL that redirects permanently elsewhere" … … 224 231 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 230 237 self.client.login(username='testclient', password='password') -
tests/regressiontests/test_client_regress/models.py
112 112 self.assertRedirects(response, '/test_client/get_view/') 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): 117 125 "An assertion is raised if the response redirects to another target"