Fixed #13140 -- Ensure that request headers are preserved through redirect chains in the test client. Thanks to David Novakovic for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13620 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2010-08-21 03:01:46 +00:00
parent 8cd5d28369
commit 26b3fb1e82
4 changed files with 28 additions and 8 deletions

View File

@ -293,7 +293,7 @@ class Client(object):
response = self.request(**r) response = self.request(**r)
if follow: if follow:
response = self._handle_redirects(response) response = self._handle_redirects(response, **extra)
return response return response
def post(self, path, data={}, content_type=MULTIPART_CONTENT, def post(self, path, data={}, content_type=MULTIPART_CONTENT,
@ -325,7 +325,7 @@ class Client(object):
response = self.request(**r) response = self.request(**r)
if follow: if follow:
response = self._handle_redirects(response) response = self._handle_redirects(response, **extra)
return response return response
def head(self, path, data={}, follow=False, **extra): def head(self, path, data={}, follow=False, **extra):
@ -344,7 +344,7 @@ class Client(object):
response = self.request(**r) response = self.request(**r)
if follow: if follow:
response = self._handle_redirects(response) response = self._handle_redirects(response, **extra)
return response return response
def options(self, path, data={}, follow=False, **extra): def options(self, path, data={}, follow=False, **extra):
@ -362,7 +362,7 @@ class Client(object):
response = self.request(**r) response = self.request(**r)
if follow: if follow:
response = self._handle_redirects(response) response = self._handle_redirects(response, **extra)
return response return response
def put(self, path, data={}, content_type=MULTIPART_CONTENT, def put(self, path, data={}, content_type=MULTIPART_CONTENT,
@ -394,7 +394,7 @@ class Client(object):
response = self.request(**r) response = self.request(**r)
if follow: if follow:
response = self._handle_redirects(response) response = self._handle_redirects(response, **extra)
return response return response
def delete(self, path, data={}, follow=False, **extra): def delete(self, path, data={}, follow=False, **extra):
@ -412,7 +412,7 @@ class Client(object):
response = self.request(**r) response = self.request(**r)
if follow: if follow:
response = self._handle_redirects(response) response = self._handle_redirects(response, **extra)
return response return response
def login(self, **credentials): def login(self, **credentials):
@ -467,7 +467,7 @@ class Client(object):
session.delete(session_key=session_cookie.value) session.delete(session_key=session_cookie.value)
self.cookies = SimpleCookie() 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." "Follows any redirects by requesting responses from the server using GET."
response.redirect_chain = [] response.redirect_chain = []
@ -478,7 +478,6 @@ class Client(object):
redirect_chain = response.redirect_chain redirect_chain = response.redirect_chain
redirect_chain.append((url, response.status_code)) redirect_chain.append((url, response.status_code))
extra = {}
if scheme: if scheme:
extra['wsgi.url_scheme'] = scheme extra['wsgi.url_scheme'] = scheme

View File

@ -847,3 +847,17 @@ class UploadedFileEncodingTest(TestCase):
encode_file('IGNORE', 'IGNORE', DummyFile("file.zip"))[2]) encode_file('IGNORE', 'IGNORE', DummyFile("file.zip"))[2])
self.assertEqual('Content-Type: application/octet-stream', self.assertEqual('Content-Type: application/octet-stream',
encode_file('IGNORE', 'IGNORE', DummyFile("file.unknown"))[2]) 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)

View File

@ -24,4 +24,6 @@ urlpatterns = patterns('',
(r'^request_methods/$', views.request_methods_view), (r'^request_methods/$', views.request_methods_view),
(r'^check_unicode/$', views.return_unicode), (r'^check_unicode/$', views.return_unicode),
(r'^parse_unicode_json/$', views.return_json_file), (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/'}),
) )

View File

@ -86,3 +86,8 @@ def return_json_file(request):
mimetype='application/json; charset=' + charset) mimetype='application/json; charset=' + charset)
response['Content-Disposition'] = 'attachment; filename=testfile.json' response['Content-Disposition'] = 'attachment; filename=testfile.json'
return response 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'))