Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[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 freakboy3742 authored
15 django/test/client.py
View
@@ -293,7 +293,7 @@ def get(self, path, data={}, follow=False, **extra):
response = self.request(**r)
if follow:
- response = self._handle_redirects(response)
+ response = self._handle_redirects(response, **extra)
return response
def post(self, path, data={}, content_type=MULTIPART_CONTENT,
@@ -325,7 +325,7 @@ def post(self, path, data={}, content_type=MULTIPART_CONTENT,
response = self.request(**r)
if follow:
- response = self._handle_redirects(response)
+ response = self._handle_redirects(response, **extra)
return response
def head(self, path, data={}, follow=False, **extra):
@@ -344,7 +344,7 @@ def head(self, path, data={}, follow=False, **extra):
response = self.request(**r)
if follow:
- response = self._handle_redirects(response)
+ response = self._handle_redirects(response, **extra)
return response
def options(self, path, data={}, follow=False, **extra):
@@ -362,7 +362,7 @@ def options(self, path, data={}, follow=False, **extra):
response = self.request(**r)
if follow:
- response = self._handle_redirects(response)
+ response = self._handle_redirects(response, **extra)
return response
def put(self, path, data={}, content_type=MULTIPART_CONTENT,
@@ -394,7 +394,7 @@ def put(self, path, data={}, content_type=MULTIPART_CONTENT,
response = self.request(**r)
if follow:
- response = self._handle_redirects(response)
+ response = self._handle_redirects(response, **extra)
return response
def delete(self, path, data={}, follow=False, **extra):
@@ -412,7 +412,7 @@ def delete(self, path, data={}, follow=False, **extra):
response = self.request(**r)
if follow:
- response = self._handle_redirects(response)
+ response = self._handle_redirects(response, **extra)
return response
def login(self, **credentials):
@@ -467,7 +467,7 @@ def logout(self):
session.delete(session_key=session_cookie.value)
self.cookies = SimpleCookie()
- def _handle_redirects(self, response):
+ def _handle_redirects(self, response, **extra):
"Follows any redirects by requesting responses from the server using GET."
response.redirect_chain = []
@@ -478,7 +478,6 @@ def _handle_redirects(self, response):
redirect_chain = response.redirect_chain
redirect_chain.append((url, response.status_code))
- extra = {}
if scheme:
extra['wsgi.url_scheme'] = scheme
14 tests/regressiontests/test_client_regress/models.py
View
@@ -845,3 +845,17 @@ def test_guesses_content_type_on_file_encoding(self):
encode_file('IGNORE', 'IGNORE', DummyFile("file.zip"))[2])
self.assertEqual('Content-Type: application/octet-stream',
encode_file('IGNORE', 'IGNORE', DummyFile("file.unknown"))[2])
+
+class RequestHeadersTest(TestCase):
+ def test_client_headers(self):
+ "A test client can receive custom headers"
+ response = self.client.get("/test_client_regress/check_headers/", HTTP_X_ARG_CHECK='Testing 123')
+ self.assertEquals(response.content, "HTTP_X_ARG_CHECK: Testing 123")
+ self.assertEquals(response.status_code, 200)
+
+ def test_client_headers_redirect(self):
+ "Test client headers are preserved through redirects"
+ response = self.client.get("/test_client_regress/check_headers_redirect/", follow=True, HTTP_X_ARG_CHECK='Testing 123')
+ self.assertEquals(response.content, "HTTP_X_ARG_CHECK: Testing 123")
+ self.assertRedirects(response, '/test_client_regress/check_headers/',
+ status_code=301, target_status_code=200)
2  tests/regressiontests/test_client_regress/urls.py
View
@@ -24,4 +24,6 @@
(r'^request_methods/$', views.request_methods_view),
(r'^check_unicode/$', views.return_unicode),
(r'^parse_unicode_json/$', views.return_json_file),
+ (r'^check_headers/$', views.check_headers),
+ (r'^check_headers_redirect/$', redirect_to, {'url': '/test_client_regress/check_headers/'}),
)
5 tests/regressiontests/test_client_regress/views.py
View
@@ -86,3 +86,8 @@ def return_json_file(request):
mimetype='application/json; charset=' + charset)
response['Content-Disposition'] = 'attachment; filename=testfile.json'
return response
+
+def check_headers(request):
+ "A view that responds with value of the X-ARG-CHECK header"
+ return HttpResponse('HTTP_X_ARG_CHECK: %s' % request.META.get('HTTP_X_ARG_CHECK', 'Undefined'))
+
Please sign in to comment.
Something went wrong with that request. Please try again.