Ticket #4476: 4476.diff
File 4476.diff, 5.5 KB (added by , 17 years ago) |
---|
-
django/test/client.py
diff --git a/django/test/client.py b/django/test/client.py index bbd8239..4e76335 100644
a b class Client: 205 205 206 206 return response 207 207 208 def get(self, path, data={}, **extra):208 def get(self, path, data={}, follow=False, **extra): 209 209 "Request a response from the server using GET." 210 210 r = { 211 211 'CONTENT_LENGTH': None, … … class Client: 216 216 } 217 217 r.update(extra) 218 218 219 return self.request(**r) 219 response = self.request(**r) 220 if follow: 221 responses = [response] 222 if response.status_code in [301, 302]: 223 new_url = response['Location'][17:] # Strip the "http://testserver" 224 responses = responses + self.get(new_url, {}, True) 225 return responses 226 else: 227 return response 220 228 221 def post(self, path, data={}, content_type=MULTIPART_CONTENT, **extra): 229 def post(self, path, data={}, content_type=MULTIPART_CONTENT, 230 follow=False, **extra): 222 231 "Request a response from the server using POST." 223 232 224 233 if content_type is MULTIPART_CONTENT: … … class Client: 235 244 } 236 245 r.update(extra) 237 246 238 return self.request(**r) 247 response = self.request(**r) 248 if follow: 249 responses = [response] 250 if response.status_code in [301, 302]: 251 new_url = response['Location'][17:] # Strip the "http://testserver" 252 responses = responses + self.get(new_url, {}, True) 253 return responses 254 else: 255 return response 239 256 240 257 def login(self, **credentials): 241 258 """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 7705380..0e941d9 100644
a b arguments at time of construction:: 453 453 454 454 Once you have a ``Client`` instance, you can call any of the following methods: 455 455 456 ``get(path, data={} )``456 ``get(path, data={}, follow=False)`` 457 457 Makes a GET request on the provided ``path`` and returns a ``Response`` 458 458 object, which is documented below. 459 459 … … Once you have a ``Client`` instance, you can call any of the following methods: 467 467 468 468 /customers/details/?name=fred&age=7 469 469 470 ``post(path, data={}, content_type=MULTIPART_CONTENT)`` 470 If you set ``follow`` to ``True`` the client will follow any redirects 471 and return an array containing all the responses obtained. 472 473 ``post(path, data={}, content_type=MULTIPART_CONTENT, follow=False)`` 471 474 Makes a POST request on the provided ``path`` and returns a ``Response`` 472 475 object, which is documented below. 473 476 … … Once you have a ``Client`` instance, you can call any of the following methods: 516 519 Note that you should manually close the file after it has been provided to 517 520 ``post()``. 518 521 522 If you set ``follow`` to ``True`` the client will follow any redirects 523 and return an array containing all the responses obtained. 524 519 525 ``login(**credentials)`` 520 526 **New in Django development version** 521 527 -
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 b5d9ae6..6f3f4ac 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 e771707..5d93b2c 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('', 5 6 (r'^no_template_view/$', views.no_template_view), 6 7 (r'^file_upload/$', views.file_upload_view), 7 8 (r'^get_view/$', views.get_view), 8 (r'^login_protected_redirect_view/$', views.login_protected_redirect_view) 9 (r'^login_protected_redirect_view/$', views.login_protected_redirect_view), 10 (r'^redirects/$', redirect_to, {'url': 'further/'}), 11 (r'^redirects/further/$', redirect_to, {'url': 'more/'}), 12 (r'^redirects/further/more/$', redirect_to, {'url': 'end/'}), 9 13 )