Django

Code

Changeset 6031

Show
Ignore:
Timestamp:
08/31/07 06:37:28 (1 year ago)
Author:
russellm
Message:

Fixed #4968 -- Added assertRedirects handling for paths with GET data. Thanks for the patch, Ivan Sagalaev.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/test/testcases.py

    r5898 r6031  
    11import re, unittest 
    2 from urlparse import urlparse 
     2from urlparse import urlsplit 
     3from django.http import QueryDict 
    34from django.db import transaction 
    45from django.core import mail 
     
    6162        super(TestCase, self).__call__(result) 
    6263 
    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): 
    6465        """Assert that a response redirected to a specific URL, and that the 
    6566        redirect URL can be loaded. 
    6667         
     68        Note that assertRedirects won't work for external links since it uses  
     69        TestClient to do a request. 
    6770        """ 
    6871        self.assertEqual(response.status_code, status_code,  
    6972            "Response didn't redirect as expected: Response code was %d (expected %d)" %  
    7073                (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)) 
    7584        self.assertEqual(redirect_response.status_code, target_status_code,  
    7685            "Couldn't retrieve redirection page '%s': response code was %d (expected %d)" %  
  • django/trunk/docs/testing.txt

    r5989 r6031  
    827827    the response. 
    828828 
    829 ``assertRedirects(response, expected_path, status_code=302, target_status_code=200)`` 
     829``assertRedirects(response, expected_url, status_code=302, target_status_code=200)`` 
    830830    Asserts that the response return a ``status_code`` redirect status, 
    831     it redirected to ``expected_path`` and the subsequent page was received with 
    832     ``target_status_code``. 
     831    it redirected to ``expected_url`` (including any GET data), and the subsequent  
     832    page was received with ``target_status_code``. 
    833833 
    834834``assertTemplateUsed(response, template_name)`` 
  • django/trunk/tests/modeltests/test_client/models.py

    r5917 r6031  
    8787        # Check that the response was a 302 (redirect) 
    8888        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') 
    8996 
    9097    def test_permanent_redirect(self): 
     
    225232        # Get the page without logging in. Should result in 302. 
    226233        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/') 
    228235         
    229236        # Log in 
     
    262269        # Request a page that requires a login 
    263270        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/') 
    265272 
    266273    def test_session_modifying_view(self): 
  • django/trunk/tests/modeltests/test_client/views.py

    r5876 r6031  
    4949def redirect_view(request): 
    5050    "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) 
    5257 
    5358def double_redirect_view(request): 
  • django/trunk/tests/regressiontests/test_client_regress/models.py

    r5876 r6031  
    113113        except AssertionError, e: 
    114114            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/'") 
    115123 
    116124    def test_incorrect_target(self):