From dcb69043d0de45bb55998fc418d93c28bc7689ae Mon Sep 17 00:00:00 2001 From: Tom Carrick Date: Tue, 15 Sep 2020 12:43:37 +0200 Subject: Fixed #32002 -- Added headers parameter to HttpResponse and subclasses. --- tests/httpwrappers/tests.py | 23 ++++++++++++++++++++++- tests/template_tests/test_response.py | 17 +++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/httpwrappers/tests.py b/tests/httpwrappers/tests.py index fbc5019979..cbf8b7d586 100644 --- a/tests/httpwrappers/tests.py +++ b/tests/httpwrappers/tests.py @@ -286,7 +286,7 @@ class QueryDictTests(SimpleTestCase): QueryDict.fromkeys(0) -class HttpResponseTests(unittest.TestCase): +class HttpResponseTests(SimpleTestCase): def test_headers_type(self): r = HttpResponse() @@ -470,10 +470,31 @@ class HttpResponseTests(unittest.TestCase): # del doesn't raise a KeyError on nonexistent headers. del r.headers['X-Foo'] + def test_instantiate_with_headers(self): + r = HttpResponse('hello', headers={'X-Foo': 'foo'}) + self.assertEqual(r.headers['X-Foo'], 'foo') + self.assertEqual(r.headers['x-foo'], 'foo') + def test_content_type(self): r = HttpResponse('hello', content_type='application/json') self.assertEqual(r.headers['Content-Type'], 'application/json') + def test_content_type_headers(self): + r = HttpResponse('hello', headers={'Content-Type': 'application/json'}) + self.assertEqual(r.headers['Content-Type'], 'application/json') + + def test_content_type_mutually_exclusive(self): + msg = ( + "'headers' must not contain 'Content-Type' when the " + "'content_type' parameter is provided." + ) + with self.assertRaisesMessage(ValueError, msg): + HttpResponse( + 'hello', + content_type='application/json', + headers={'Content-Type': 'text/csv'}, + ) + class HttpResponseSubclassesTests(SimpleTestCase): def test_redirect(self): diff --git a/tests/template_tests/test_response.py b/tests/template_tests/test_response.py index 0a51d68f01..8ac629f3bd 100644 --- a/tests/template_tests/test_response.py +++ b/tests/template_tests/test_response.py @@ -216,6 +216,14 @@ class SimpleTemplateResponseTest(SimpleTestCase): self.assertEqual(unpickled_response.cookies['key'].value, 'value') + def test_headers(self): + response = SimpleTemplateResponse( + 'first/test.html', + {'value': 123, 'fn': datetime.now}, + headers={'X-Foo': 'foo'}, + ) + self.assertEqual(response.headers['X-Foo'], 'foo') + @override_settings(TEMPLATES=[{ 'BACKEND': 'django.template.backends.django.DjangoTemplates', @@ -319,6 +327,15 @@ class TemplateResponseTest(SimpleTestCase): unpickled_response = pickle.loads(pickled_response) pickle.dumps(unpickled_response) + def test_headers(self): + response = TemplateResponse( + self.factory.get('/'), + 'first/test.html', + {'value': 123, 'fn': datetime.now}, + headers={'X-Foo': 'foo'}, + ) + self.assertEqual(response.headers['X-Foo'], 'foo') + @modify_settings(MIDDLEWARE={'append': ['template_tests.test_response.custom_urlconf_middleware']}) @override_settings(ROOT_URLCONF='template_tests.urls') -- cgit v1.3