diff --git a/django/test/client.py b/django/test/client.py index 85d91b0c44..c733372130 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -752,6 +752,8 @@ class AsyncRequestFactory(RequestFactory): "scheme": "https" if secure else "http", "headers": [(b"host", b"testserver")], } + if self.defaults: + extra = {**self.defaults, **extra} if data: s["headers"].extend( [ diff --git a/tests/test_client/tests.py b/tests/test_client/tests.py index 18b7aaef6c..652563d269 100644 --- a/tests/test_client/tests.py +++ b/tests/test_client/tests.py @@ -1327,6 +1327,19 @@ class AsyncRequestFactoryTest(SimpleTestCase): self.assertEqual(request.headers["x-another-header"], "some other value") self.assertIn("HTTP_X_ANOTHER_HEADER", request.META) + def test_async_request_factory_default_headers(self): + request_factory_with_headers = AsyncRequestFactory( + **{ + "Authorization": "Bearer faketoken", + "X-Another-Header": "some other value", + } + ) + request = request_factory_with_headers.get("/somewhere/") + self.assertEqual(request.headers["authorization"], "Bearer faketoken") + self.assertIn("HTTP_AUTHORIZATION", request.META) + self.assertEqual(request.headers["x-another-header"], "some other value") + self.assertIn("HTTP_X_ANOTHER_HEADER", request.META) + def test_request_factory_query_string(self): request = self.request_factory.get("/somewhere/", {"example": "data"}) self.assertNotIn("Query-String", request.headers)