Ticket #4476: 4476_rev7412.diff
File 4476_rev7412.diff, 5.6 KB (added by , 17 years ago) |
---|
-
django/test/client.py
diff --git a/django/test/client.py b/django/test/client.py index bac28f7..f48560a 100644
a b class Client: 204 204 205 205 return response 206 206 207 def get(self, path, data={}, **extra):207 def get(self, path, data={}, follow=False, **extra): 208 208 "Request a response from the server using GET." 209 209 r = { 210 210 'CONTENT_LENGTH': None, … … class Client: 215 215 } 216 216 r.update(extra) 217 217 218 return self.request(**r) 218 response = self.request(**r) 219 if follow: 220 responses = [response] 221 if response.status_code in [301, 302, 303, 307]: 222 new_url = response['Location'][17:] # Strip the "http://testserver" 223 responses = responses + self.get(new_url, {}, True) 224 return responses 225 else: 226 return response 219 227 220 def post(self, path, data={}, content_type=MULTIPART_CONTENT, **extra): 228 def post(self, path, data={}, content_type=MULTIPART_CONTENT, 229 follow=False, **extra): 221 230 "Request a response from the server using POST." 222 231 223 232 if content_type is MULTIPART_CONTENT: … … class Client: 234 243 } 235 244 r.update(extra) 236 245 237 return self.request(**r) 246 response = self.request(**r) 247 if follow: 248 responses = [response] 249 if response.status_code in [301, 302, 303, 307]: 250 new_url = response['Location'][17:] # Strip the "http://testserver" 251 responses = responses + self.get(new_url, {}, True) 252 return responses 253 else: 254 return response 238 255 239 256 def login(self, **credentials): 240 257 """Set the Client to appear as if it has sucessfully logged into a site. -
docs/testing.txt
diff --git a/docs/testing.txt b/docs/testing.txt index 0ff3cce..e2cef77 100644
a b arguments at time of construction:: 460 460 461 461 Once you have a ``Client`` instance, you can call any of the following methods: 462 462 463 ``get(path, data={} )``463 ``get(path, data={}, follow=False)`` 464 464 Makes a GET request on the provided ``path`` and returns a ``Response`` 465 465 object, which is documented below. 466 466 … … Once you have a ``Client`` instance, you can call any of the following methods: 474 474 475 475 /customers/details/?name=fred&age=7 476 476 477 ``post(path, data={}, content_type=MULTIPART_CONTENT)`` 477 If you set ``follow`` to ``True`` the client will follow any redirects 478 and return an array containing all the responses obtained. 479 480 ``post(path, data={}, content_type=MULTIPART_CONTENT, follow=False)`` 478 481 Makes a POST request on the provided ``path`` and returns a ``Response`` 479 482 object, which is documented below. 480 483 … … Once you have a ``Client`` instance, you can call any of the following methods: 523 526 Note that you should manually close the file after it has been provided to 524 527 ``post()``. 525 528 529 If you set ``follow`` to ``True`` the client will follow any redirects 530 and return an array containing all the responses obtained. 531 526 532 ``login(**credentials)`` 527 533 **New in Django development version** 528 534 -
tests/regressiontests/test_client_regress/models.py
diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py index 305ccc9..45330c9 100644
a b class AssertRedirectsTests(TestCase): 139 139 except AssertionError, e: 140 140 self.assertEquals(str(e), "Couldn't retrieve redirection page '/test_client/permanent_redirect_view/': response code was 301 (expected 200)") 141 141 142 def test_redirect_chain(self): 143 # First with GET 144 responses = self.client.get('/test_client_regress/redirects/', {}, True) 145 self.assertEquals(len(responses), 4) 146 self.assertRedirects(responses[0], 147 '/test_client_regress/redirects/further/', 301, 301) 148 self.assertRedirects(responses[1], 149 '/test_client_regress/redirects/further/more/', 301, 301) 150 self.assertRedirects(responses[2], 151 '/test_client_regress/redirects/further/more/end/', 301, 404) 152 153 # Now with POST 154 responses = self.client.post('/test_client_regress/redirects/', 155 {'nothing': 'to_send'}, follow=True) 156 self.assertEquals(len(responses), 4) 157 self.assertRedirects(responses[0], 158 '/test_client_regress/redirects/further/', 301, 301) 159 142 160 class AssertFormErrorTests(TestCase): 143 161 def test_unknown_form(self): 144 162 "An assertion is raised if the form name is unknown" -
tests/regressiontests/test_client_regress/urls.py
diff --git a/tests/regressiontests/test_client_regress/urls.py b/tests/regressiontests/test_client_regress/urls.py index d3304ca..f70ca6a 100644
a b 1 1 from django.conf.urls.defaults import * 2 from django.views.generic.simple import redirect_to 2 3 import views 3 4 4 5 urlpatterns = patterns('', … … urlpatterns = patterns('', 6 7 (r'^file_upload/$', views.file_upload_view), 7 8 (r'^get_view/$', views.get_view), 8 9 url(r'^arg_view/(?P<name>.+)/$', views.view_with_argument, name='arg_view'), 9 (r'^login_protected_redirect_view/$', views.login_protected_redirect_view) 10 (r'^login_protected_redirect_view/$', views.login_protected_redirect_view), 11 (r'^redirects/$', redirect_to, {'url': 'further/'}), 12 (r'^redirects/further/$', redirect_to, {'url': 'more/'}), 13 (r'^redirects/further/more/$', redirect_to, {'url': 'end/'}), 10 14 )