From c89dc9cfa0c8f59da93de05bb41c923f9397595e Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 14 May 2007 11:07:14 +0000 Subject: [PATCH] Fixed some incorrect reporting of error messages in assertRedirects, and added test cases to validate. git-svn-id: http://code.djangoproject.com/svn/django/trunk@5228 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/test/testcases.py | 4 +-- .../test_client_regress/models.py | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/django/test/testcases.py b/django/test/testcases.py index d07e38e436..dd1f73befd 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -62,7 +62,7 @@ class TestCase(unittest.TestCase): """ self.assertEqual(response.status_code, status_code, - "Response didn't redirect: Reponse code was %d (expected %d)" % + "Response didn't redirect as expected: Reponse code was %d (expected %d)" % (response.status_code, status_code)) scheme, netloc, path, params, query, fragment = urlparse(response['Location']) self.assertEqual(path, expected_path, @@ -70,7 +70,7 @@ class TestCase(unittest.TestCase): redirect_response = self.client.get(path) self.assertEqual(redirect_response.status_code, target_status_code, "Couldn't retrieve redirection page '%s': response code was %d (expected %d)" % - (path, response.status_code, status_code)) + (path, redirect_response.status_code, target_status_code)) def assertContains(self, response, text, count=1, status_code=200): """Assert that a response indicates that a page was retreived successfully, diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py index c39fafe314..40d022a47a 100644 --- a/tests/regressiontests/test_client_regress/models.py +++ b/tests/regressiontests/test_client_regress/models.py @@ -60,7 +60,35 @@ class AssertTemplateUsedTests(TestCase): self.assertTemplateUsed(response, "Valid POST Template") except AssertionError, e: 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']") + +class AssertRedirectsTests(TestCase): + def test_redirect_page(self): + "An assertion is raised if the original page couldn't be retrieved as expected" + # This page will redirect with code 301, not 302 + response = self.client.get('/test_client/permanent_redirect_view/') + try: + self.assertRedirects(response, '/test_client/get_view/') + except AssertionError, e: + self.assertEquals(str(e), "Response didn't redirect as expected: Reponse code was 301 (expected 302)") + + def test_incorrect_target(self): + "An assertion is raised if the response redirects to another target" + response = self.client.get('/test_client/permanent_redirect_view/') + try: + # Should redirect to get_view + self.assertRedirects(response, '/test_client/some_view/') + except AssertionError, e: + self.assertEquals(str(e), "Response didn't redirect as expected: Reponse code was 301 (expected 302)") + def test_target_page(self): + "An assertion is raised if the reponse redirect target cannot be retrieved as expected" + response = self.client.get('/test_client/double_redirect_view/') + try: + # The redirect target responds with a 301 code, not 200 + self.assertRedirects(response, '/test_client/permanent_redirect_view/') + except AssertionError, e: + self.assertEquals(str(e), "Couldn't retrieve redirection page '/test_client/permanent_redirect_view/': response code was 301 (expected 200)") + class AssertFormErrorTests(TestCase): def test_unknown_form(self): "An assertion is raised if the form name is unknown"