Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Fixed #13140 -- Ensure that request headers are preserved thr…

…ough redirect chains in the test client. Thanks to David Novakovic for the report.

Backport of r13620 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@13621 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 49c5c075686a4afd831f5d7d8dad6fe019c49091 1 parent 5c6fe32
Russell Keith-Magee authored August 21, 2010
15  django/test/client.py
@@ -293,7 +293,7 @@ def get(self, path, data={}, follow=False, **extra):
293 293
 
294 294
         response = self.request(**r)
295 295
         if follow:
296  
-            response = self._handle_redirects(response)
  296
+            response = self._handle_redirects(response, **extra)
297 297
         return response
298 298
 
299 299
     def post(self, path, data={}, content_type=MULTIPART_CONTENT,
@@ -325,7 +325,7 @@ def post(self, path, data={}, content_type=MULTIPART_CONTENT,
325 325
 
326 326
         response = self.request(**r)
327 327
         if follow:
328  
-            response = self._handle_redirects(response)
  328
+            response = self._handle_redirects(response, **extra)
329 329
         return response
330 330
 
331 331
     def head(self, path, data={}, follow=False, **extra):
@@ -344,7 +344,7 @@ def head(self, path, data={}, follow=False, **extra):
344 344
 
345 345
         response = self.request(**r)
346 346
         if follow:
347  
-            response = self._handle_redirects(response)
  347
+            response = self._handle_redirects(response, **extra)
348 348
         return response
349 349
 
350 350
     def options(self, path, data={}, follow=False, **extra):
@@ -362,7 +362,7 @@ def options(self, path, data={}, follow=False, **extra):
362 362
 
363 363
         response = self.request(**r)
364 364
         if follow:
365  
-            response = self._handle_redirects(response)
  365
+            response = self._handle_redirects(response, **extra)
366 366
         return response
367 367
 
368 368
     def put(self, path, data={}, content_type=MULTIPART_CONTENT,
@@ -394,7 +394,7 @@ def put(self, path, data={}, content_type=MULTIPART_CONTENT,
394 394
 
395 395
         response = self.request(**r)
396 396
         if follow:
397  
-            response = self._handle_redirects(response)
  397
+            response = self._handle_redirects(response, **extra)
398 398
         return response
399 399
 
400 400
     def delete(self, path, data={}, follow=False, **extra):
@@ -412,7 +412,7 @@ def delete(self, path, data={}, follow=False, **extra):
412 412
 
413 413
         response = self.request(**r)
414 414
         if follow:
415  
-            response = self._handle_redirects(response)
  415
+            response = self._handle_redirects(response, **extra)
416 416
         return response
417 417
 
418 418
     def login(self, **credentials):
@@ -467,7 +467,7 @@ def logout(self):
467 467
             session.delete(session_key=session_cookie.value)
468 468
         self.cookies = SimpleCookie()
469 469
 
470  
-    def _handle_redirects(self, response):
  470
+    def _handle_redirects(self, response, **extra):
471 471
         "Follows any redirects by requesting responses from the server using GET."
472 472
 
473 473
         response.redirect_chain = []
@@ -478,7 +478,6 @@ def _handle_redirects(self, response):
478 478
             redirect_chain = response.redirect_chain
479 479
             redirect_chain.append((url, response.status_code))
480 480
 
481  
-            extra = {}
482 481
             if scheme:
483 482
                 extra['wsgi.url_scheme'] = scheme
484 483
 
14  tests/regressiontests/test_client_regress/models.py
@@ -845,3 +845,17 @@ def test_guesses_content_type_on_file_encoding(self):
845 845
                          encode_file('IGNORE', 'IGNORE', DummyFile("file.zip"))[2])
846 846
         self.assertEqual('Content-Type: application/octet-stream',
847 847
                          encode_file('IGNORE', 'IGNORE', DummyFile("file.unknown"))[2])
  848
+
  849
+class RequestHeadersTest(TestCase):
  850
+    def test_client_headers(self):
  851
+        "A test client can receive custom headers"
  852
+        response = self.client.get("/test_client_regress/check_headers/", HTTP_X_ARG_CHECK='Testing 123')
  853
+        self.assertEquals(response.content, "HTTP_X_ARG_CHECK: Testing 123")
  854
+        self.assertEquals(response.status_code, 200)
  855
+
  856
+    def test_client_headers_redirect(self):
  857
+        "Test client headers are preserved through redirects"
  858
+        response = self.client.get("/test_client_regress/check_headers_redirect/", follow=True, HTTP_X_ARG_CHECK='Testing 123')
  859
+        self.assertEquals(response.content, "HTTP_X_ARG_CHECK: Testing 123")
  860
+        self.assertRedirects(response, '/test_client_regress/check_headers/',
  861
+            status_code=301, target_status_code=200)
2  tests/regressiontests/test_client_regress/urls.py
@@ -24,4 +24,6 @@
24 24
     (r'^request_methods/$', views.request_methods_view),
25 25
     (r'^check_unicode/$', views.return_unicode),
26 26
     (r'^parse_unicode_json/$', views.return_json_file),
  27
+    (r'^check_headers/$', views.check_headers),
  28
+    (r'^check_headers_redirect/$', redirect_to, {'url': '/test_client_regress/check_headers/'}),
27 29
 )
5  tests/regressiontests/test_client_regress/views.py
@@ -86,3 +86,8 @@ def return_json_file(request):
86 86
                             mimetype='application/json; charset=' + charset)
87 87
     response['Content-Disposition'] = 'attachment; filename=testfile.json'
88 88
     return response
  89
+
  90
+def check_headers(request):
  91
+    "A view that responds with value of the X-ARG-CHECK header"
  92
+    return HttpResponse('HTTP_X_ARG_CHECK: %s' % request.META.get('HTTP_X_ARG_CHECK', 'Undefined'))
  93
+

0 notes on commit 49c5c07

Please sign in to comment.
Something went wrong with that request. Please try again.