Skip to main content

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

NameTypeDescription
**requestdictKeyword arguments containing scope data and an optional '_body_file' to be used as the request payload.

Returns

TypeDescription
[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

NameTypeDescription
methodstrThe HTTP verb to use for the request (e.g., 'GET', 'POST', 'PUT').
pathstrThe URL path for the request; can be a string or a lazy object.
data`strbytes`
content_typestrThe MIME type of the request body, used to set the 'Content-Type' header.
secureboolIf True, the request will use HTTPS and port 443; otherwise, it uses HTTP and port 80.
headersdictA dictionary of extra HTTP headers to include in the request.
query_paramsdictA dictionary of query string parameters to be URL-encoded and appended to the path.
**extradictAdditional keyword arguments to be merged into the ASGI scope or converted to headers.

Returns

TypeDescription
[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.