Author: russellm
Date: 2007-08-31 06:37:28 -0500 (Fri, 31 Aug 2007)
New Revision: 6031

Modified:
   django/trunk/django/test/testcases.py
   django/trunk/docs/testing.txt
   django/trunk/tests/modeltests/test_client/models.py
   django/trunk/tests/modeltests/test_client/views.py
   django/trunk/tests/regressiontests/test_client_regress/models.py
Log:
Fixed #4968 -- Added assertRedirects handling for paths with GET data. Thanks 
for the patch, Ivan Sagalaev.


Modified: django/trunk/django/test/testcases.py
===================================================================
--- django/trunk/django/test/testcases.py       2007-08-31 05:53:47 UTC (rev 
6030)
+++ django/trunk/django/test/testcases.py       2007-08-31 11:37:28 UTC (rev 
6031)
@@ -1,5 +1,6 @@
 import re, unittest
-from urlparse import urlparse
+from urlparse import urlsplit
+from django.http import QueryDict
 from django.db import transaction
 from django.core import mail
 from django.core.management import call_command
@@ -60,18 +61,26 @@
         self._pre_setup()
         super(TestCase, self).__call__(result)
 
-    def assertRedirects(self, response, expected_path, status_code=302, 
target_status_code=200):
+    def assertRedirects(self, response, expected_url, status_code=302, 
target_status_code=200):
         """Assert that a response redirected to a specific URL, and that the
         redirect URL can be loaded.
         
+        Note that assertRedirects won't work for external links since it uses 
+        TestClient to do a request.
         """
         self.assertEqual(response.status_code, status_code, 
             "Response didn't redirect as expected: Response code was %d 
(expected %d)" % 
                 (response.status_code, status_code))
-        scheme, netloc, path, params, query, fragment = 
urlparse(response['Location'])
-        self.assertEqual(path, expected_path, 
-            "Response redirected to '%s', expected '%s'" % (path, 
expected_path))
-        redirect_response = self.client.get(path)
+        scheme, netloc, path, query, fragment = urlsplit(response['Location'])
+        url = path
+        if query:
+            url += '?' + query
+        if fragment:
+            url += '#' + fragment
+        self.assertEqual(url, expected_url, 
+            "Response redirected to '%s', expected '%s'" % (url, expected_url))
+        
+        redirect_response = self.client.get(path, QueryDict(query))
         self.assertEqual(redirect_response.status_code, target_status_code, 
             "Couldn't retrieve redirection page '%s': response code was %d 
(expected %d)" % 
                 (path, redirect_response.status_code, target_status_code))

Modified: django/trunk/docs/testing.txt
===================================================================
--- django/trunk/docs/testing.txt       2007-08-31 05:53:47 UTC (rev 6030)
+++ django/trunk/docs/testing.txt       2007-08-31 11:37:28 UTC (rev 6031)
@@ -826,10 +826,10 @@
     Asserts that the template with the given name was *not* used in rendering
     the response.
 
-``assertRedirects(response, expected_path, status_code=302, 
target_status_code=200)``
+``assertRedirects(response, expected_url, status_code=302, 
target_status_code=200)``
     Asserts that the response return a ``status_code`` redirect status,
-    it redirected to ``expected_path`` and the subsequent page was received 
with
-    ``target_status_code``.
+    it redirected to ``expected_url`` (including any GET data), and the 
subsequent 
+    page was received with ``target_status_code``.
 
 ``assertTemplateUsed(response, template_name)``
     Asserts that the template with the given name was used in rendering the

Modified: django/trunk/tests/modeltests/test_client/models.py
===================================================================
--- django/trunk/tests/modeltests/test_client/models.py 2007-08-31 05:53:47 UTC 
(rev 6030)
+++ django/trunk/tests/modeltests/test_client/models.py 2007-08-31 11:37:28 UTC 
(rev 6031)
@@ -86,6 +86,13 @@
         
         # Check that the response was a 302 (redirect)
         self.assertRedirects(response, '/test_client/get_view/')
+    
+    def test_redirect_with_query(self):
+        "GET a URL that redirects with given GET parameters"
+        response = self.client.get('/test_client/redirect_view/', {'var': 
'value'})
+        
+        # Check if parameters are intact
+        self.assertRedirects(response, '/test_client/get_view/?var=value')
 
     def test_permanent_redirect(self):
         "GET a URL that redirects permanently elsewhere"
@@ -224,7 +231,7 @@
         
         # Get the page without logging in. Should result in 302.
         response = self.client.get('/test_client/login_protected_view/')
-        self.assertRedirects(response, '/accounts/login/')
+        self.assertRedirects(response, 
'/accounts/login/?next=/test_client/login_protected_view/')
         
         # Log in
         self.client.login(username='testclient', password='password')
@@ -261,7 +268,7 @@
 
         # Request a page that requires a login
         response = self.client.get('/test_client/login_protected_view/')
-        self.assertRedirects(response, '/accounts/login/')
+        self.assertRedirects(response, 
'/accounts/login/?next=/test_client/login_protected_view/')
 
     def test_session_modifying_view(self):
         "Request a page that modifies the session"

Modified: django/trunk/tests/modeltests/test_client/views.py
===================================================================
--- django/trunk/tests/modeltests/test_client/views.py  2007-08-31 05:53:47 UTC 
(rev 6030)
+++ django/trunk/tests/modeltests/test_client/views.py  2007-08-31 11:37:28 UTC 
(rev 6031)
@@ -48,7 +48,12 @@
 
 def redirect_view(request):
     "A view that redirects all requests to the GET view"
-    return HttpResponseRedirect('/test_client/get_view/')
+    if request.GET:
+        from urllib import urlencode
+        query = '?' + urlencode(request.GET, True)
+    else:
+        query = ''
+    return HttpResponseRedirect('/test_client/get_view/' + query)
 
 def double_redirect_view(request):
     "A view that redirects all requests to a redirection view"

Modified: django/trunk/tests/regressiontests/test_client_regress/models.py
===================================================================
--- django/trunk/tests/regressiontests/test_client_regress/models.py    
2007-08-31 05:53:47 UTC (rev 6030)
+++ django/trunk/tests/regressiontests/test_client_regress/models.py    
2007-08-31 11:37:28 UTC (rev 6031)
@@ -112,6 +112,14 @@
             self.assertRedirects(response, '/test_client/get_view/')
         except AssertionError, e:
             self.assertEquals(str(e), "Response didn't redirect as expected: 
Response code was 301 (expected 302)")
+    
+    def test_lost_query(self):
+        "An assertion is raised if the redirect location doesn't preserve GET 
parameters"
+        response = self.client.get('/test_client/redirect_view/', {'var': 
'value'})
+        try:
+            self.assertRedirects(response, '/test_client/get_view/')
+        except AssertionError, e:
+            self.assertEquals(str(e), "Response redirected to 
'/test_client/get_view/?var=value', expected '/test_client/get_view/'")
 
     def test_incorrect_target(self):
         "An assertion is raised if the response redirects to another target"


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to