1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #14611 -- Added query_params argument to RequestFactory and Client classes.

This commit is contained in:
Tom Carrick
2023-11-05 16:41:16 +01:00
committed by Mariusz Felisiak
parent e76cc93b01
commit a03593967f
6 changed files with 434 additions and 82 deletions

View File

@@ -1002,6 +1002,36 @@ class ClientTest(TestCase):
)
self.assertEqual(response.content, b"named_temp_file")
def test_query_params(self):
tests = (
"get",
"post",
"put",
"patch",
"delete",
"head",
"options",
"trace",
)
for method in tests:
with self.subTest(method=method):
client_method = getattr(self.client, method)
response = client_method("/get_view/", query_params={"example": "data"})
self.assertEqual(response.wsgi_request.GET["example"], "data")
def test_cannot_use_data_and_query_params_together(self):
tests = ["get", "head"]
msg = "query_params and data arguments are mutually exclusive."
for method in tests:
with self.subTest(method=method):
client_method = getattr(self.client, method)
with self.assertRaisesMessage(ValueError, msg):
client_method(
"/get_view/",
data={"example": "data"},
query_params={"q": "terms"},
)
@override_settings(
MIDDLEWARE=["django.middleware.csrf.CsrfViewMiddleware"],
@@ -1127,6 +1157,23 @@ class RequestFactoryTest(SimpleTestCase):
self.assertEqual(request.headers["x-another-header"], "some other value")
self.assertIn("HTTP_X_ANOTHER_HEADER", request.META)
def test_request_factory_query_params(self):
tests = (
"get",
"post",
"put",
"patch",
"delete",
"head",
"options",
"trace",
)
for method in tests:
with self.subTest(method=method):
factory = getattr(self.request_factory, method)
request = factory("/somewhere", query_params={"example": "data"})
self.assertEqual(request.GET["example"], "data")
@override_settings(ROOT_URLCONF="test_client.urls")
class AsyncClientTest(TestCase):
@@ -1183,6 +1230,25 @@ class AsyncClientTest(TestCase):
response = await self.async_client.get("/post_view/")
self.assertContains(response, "Viewing GET page.")
async def test_query_params(self):
tests = (
"get",
"post",
"put",
"patch",
"delete",
"head",
"options",
"trace",
)
for method in tests:
with self.subTest(method=method):
client_method = getattr(self.async_client, method)
response = await client_method(
"/async_get_view/", query_params={"example": "data"}
)
self.assertEqual(response.asgi_request.GET["example"], "data")
@override_settings(ROOT_URLCONF="test_client.urls")
class AsyncRequestFactoryTest(SimpleTestCase):
@@ -1264,3 +1330,33 @@ class AsyncRequestFactoryTest(SimpleTestCase):
request = self.request_factory.get("/somewhere/", {"example": "data"})
self.assertNotIn("Query-String", request.headers)
self.assertEqual(request.GET["example"], "data")
def test_request_factory_query_params(self):
tests = (
"get",
"post",
"put",
"patch",
"delete",
"head",
"options",
"trace",
)
for method in tests:
with self.subTest(method=method):
factory = getattr(self.request_factory, method)
request = factory("/somewhere", query_params={"example": "data"})
self.assertEqual(request.GET["example"], "data")
def test_cannot_use_data_and_query_params_together(self):
tests = ["get", "head"]
msg = "query_params and data arguments are mutually exclusive."
for method in tests:
with self.subTest(method=method):
factory = getattr(self.request_factory, method)
with self.assertRaisesMessage(ValueError, msg):
factory(
"/somewhere",
data={"example": "data"},
query_params={"q": "terms"},
)