diff --git a/django/test/client.py b/django/test/client.py index 498af5c344..cceed8d9f6 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -293,7 +293,7 @@ class Client(object): 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 @@ class Client(object): 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 @@ class Client(object): 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 @@ class Client(object): 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 @@ class Client(object): 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 @@ class Client(object): 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 @@ class Client(object): 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 @@ class Client(object): redirect_chain = response.redirect_chain redirect_chain.append((url, response.status_code)) - extra = {} if scheme: extra['wsgi.url_scheme'] = scheme diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py index 692e0f63d3..5f23c8ee13 100644 --- a/tests/regressiontests/test_client_regress/models.py +++ b/tests/regressiontests/test_client_regress/models.py @@ -845,3 +845,17 @@ class UploadedFileEncodingTest(TestCase): 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) diff --git a/tests/regressiontests/test_client_regress/urls.py b/tests/regressiontests/test_client_regress/urls.py index 99eb7b70be..650d80b909 100644 --- a/tests/regressiontests/test_client_regress/urls.py +++ b/tests/regressiontests/test_client_regress/urls.py @@ -24,4 +24,6 @@ urlpatterns = patterns('', (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/'}), ) diff --git a/tests/regressiontests/test_client_regress/views.py b/tests/regressiontests/test_client_regress/views.py index 5ba7cc30a9..40aa61fca7 100644 --- a/tests/regressiontests/test_client_regress/views.py +++ b/tests/regressiontests/test_client_regress/views.py @@ -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')) +