Django

Code

Changeset 5228

Show
Ignore:
Timestamp:
05/14/07 06:07:14 (2 years ago)
Author:
russellm
Message:

Fixed some incorrect reporting of error messages in assertRedirects, and added test cases to validate.

Files:

Legend:

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

    r5215 r5228  
    6363        """ 
    6464        self.assertEqual(response.status_code, status_code,  
    65             "Response didn't redirect: Reponse code was %d (expected %d)" %  
     65            "Response didn't redirect as expected: Reponse code was %d (expected %d)" %  
    6666                (response.status_code, status_code)) 
    6767        scheme, netloc, path, params, query, fragment = urlparse(response['Location']) 
     
    7171        self.assertEqual(redirect_response.status_code, target_status_code,  
    7272            "Couldn't retrieve redirection page '%s': response code was %d (expected %d)" %  
    73                 (path, response.status_code, status_code)) 
     73                (path, redirect_response.status_code, target_status_code)) 
    7474     
    7575    def assertContains(self, response, text, count=1, status_code=200): 
  • django/trunk/tests/regressiontests/test_client_regress/models.py

    r5181 r5228  
    6161        except AssertionError, e: 
    6262            self.assertEquals(str(e), "Template 'Valid POST Template' was not one of the templates used to render the response. Templates used: ['form_view.html', 'base.html']") 
     63 
     64class AssertRedirectsTests(TestCase): 
     65    def test_redirect_page(self): 
     66        "An assertion is raised if the original page couldn't be retrieved as expected"         
     67        # This page will redirect with code 301, not 302 
     68        response = self.client.get('/test_client/permanent_redirect_view/')         
     69        try: 
     70            self.assertRedirects(response, '/test_client/get_view/') 
     71        except AssertionError, e: 
     72            self.assertEquals(str(e), "Response didn't redirect as expected: Reponse code was 301 (expected 302)") 
     73 
     74    def test_incorrect_target(self): 
     75        "An assertion is raised if the response redirects to another target" 
     76        response = self.client.get('/test_client/permanent_redirect_view/')         
     77        try: 
     78            # Should redirect to get_view 
     79            self.assertRedirects(response, '/test_client/some_view/') 
     80        except AssertionError, e: 
     81            self.assertEquals(str(e), "Response didn't redirect as expected: Reponse code was 301 (expected 302)") 
    6382         
     83    def test_target_page(self): 
     84        "An assertion is raised if the reponse redirect target cannot be retrieved as expected" 
     85        response = self.client.get('/test_client/double_redirect_view/') 
     86        try: 
     87            # The redirect target responds with a 301 code, not 200 
     88            self.assertRedirects(response, '/test_client/permanent_redirect_view/') 
     89        except AssertionError, e: 
     90            self.assertEquals(str(e), "Couldn't retrieve redirection page '/test_client/permanent_redirect_view/': response code was 301 (expected 200)") 
     91             
    6492class AssertFormErrorTests(TestCase): 
    6593    def test_unknown_form(self):