AsyncRequestFactory
Class that lets you create mock ASGI-like Request objects for use in testing. Usage:
rf = AsyncRequestFactory() get_request = rf.get("/hello/") post_request = rf.post("/submit/", {"foo": "bar"})
Once you have a request object you can pass it to any view function, including synchronous ones. The reason we have a separate class here is: a) this makes ASGIRequest subclasses, and b) AsyncClient can subclass it.
Constructor
Signature
def AsyncRequestFactory()
Methods
request()
@classmethod
def request(
**request: dict
) - > [ASGIRequest](../../core/handlers/asgi/asgirequest.md?sid=django_core_handlers_asgi_asgirequest)
Construct a generic request object.
Parameters
| Name | Type | Description |
|---|---|---|
| **request | dict | Keyword arguments containing scope data and an optional '_body_file' to be used as the request payload. |
Returns
| Type | Description |
|---|---|
[ASGIRequest](../../core/handlers/asgi/asgirequest.md?sid=django_core_handlers_asgi_asgirequest) | An ASGIRequest instance initialized with the provided scope and a streamable body payload. |
generic()
@classmethod
def generic(
method: str,
path: str,
data: str|bytes,
content_type: str,
secure: bool,
headers: dict,
query_params: dict,
**extra: dict
) - > [ASGIRequest](../../core/handlers/asgi/asgirequest.md?sid=django_core_handlers_asgi_asgirequest)
Construct an arbitrary HTTP request.
Parameters
| Name | Type | Description |
|---|---|---|
| method | str | The HTTP verb to use for the request (e.g., 'GET', 'POST', 'PUT'). |
| path | str | The URL path for the request; can be a string or a lazy object. |
| data | `str | bytes` |
| content_type | str | The MIME type of the request body, used to set the 'Content-Type' header. |
| secure | bool | If True, the request will use HTTPS and port 443; otherwise, it uses HTTP and port 80. |
| headers | dict | A dictionary of extra HTTP headers to include in the request. |
| query_params | dict | A dictionary of query string parameters to be URL-encoded and appended to the path. |
| **extra | dict | Additional keyword arguments to be merged into the ASGI scope or converted to headers. |
Returns
| Type | Description |
|---|---|
[ASGIRequest](../../core/handlers/asgi/asgirequest.md?sid=django_core_handlers_asgi_asgirequest) | A fully initialized ASGIRequest object configured with the specified HTTP method, path, and headers. |