Skip to main content

RequestFactory

Class that lets you create mock Request objects for use in testing.

Attributes

AttributeTypeDescription
json_encodertype = DjangoJSONEncoderThe JSON encoder class used to serialize dictionary, list, or tuple data when the content type is application/json.
defaultsdictA dictionary of default WSGI environment variables that are merged into every request created by the factory.
cookiesSimpleCookie = SimpleCookie()A SimpleCookie object used to store and provide cookie data for the HTTP_COOKIE header in generated requests.
errorsBytesIO = BytesIO()A byte stream used to capture WSGI errors during the request construction process.

Constructor

Signature

def RequestFactory(
json_encoder: class = DjangoJSONEncoder,
headers: dict = null,
query_params: dict = null,
defaults: kwargs = null
) - > null

Parameters

NameTypeDescription
json_encoderclass = DjangoJSONEncoderThe JSON encoder class to use when encoding JSON request data.
headersdict = nullA dictionary of HTTP headers to be included in every request.
query_paramsdict = nullA dictionary of query parameters to be included in every request.
defaultskwargs = nullAdditional keyword arguments to be used as default values in the WSGI environment.

Methods


request()

@classmethod
def request(
**request: dict
) - > [WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)

Construct a generic request object.

Parameters

NameTypeDescription
**requestdictEnvironment variables used to populate the WSGI environment for the new request.

Returns

TypeDescription
[WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)A fully initialized WSGIRequest instance based on the provided environment parameters.

get()

@classmethod
def get(
path: string,
data: dict,
secure: boolean,
headers: dict,
query_params: dict
) - > [WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)

Construct a GET request.

Parameters

NameTypeDescription
pathstringThe URL path for the request.
datadictDictionary of query parameters; raises ValueError if used alongside query_params.
securebooleanIf True, the request will be made over HTTPS.
headersdictAdditional HTTP headers to include in the request.
query_paramsdictDictionary of query parameters to append to the URL.

Returns

TypeDescription
[WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)A mock GET request object.

post()

@classmethod
def post(
path: string,
data: any,
content_type: string,
secure: boolean,
headers: dict,
query_params: dict
) - > [WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)

Construct a POST request.

Parameters

NameTypeDescription
pathstringThe URL path for the request.
dataanyThe payload to be sent in the request body.
content_typestringThe MIME type of the body data, defaulting to multipart/form-data.
securebooleanIf True, the request will be made over HTTPS.
headersdictAdditional HTTP headers to include in the request.
query_paramsdictDictionary of query parameters to append to the URL.

Returns

TypeDescription
[WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)A mock POST request object containing the encoded body data.

@classmethod
def head(
path: string,
data: dict,
secure: boolean,
headers: dict,
query_params: dict
) - > [WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)

Construct a HEAD request.

Parameters

NameTypeDescription
pathstringThe URL path for the request.
datadictDictionary of query parameters; raises ValueError if used alongside query_params.
securebooleanIf True, the request will be made over HTTPS.
headersdictAdditional HTTP headers to include in the request.
query_paramsdictDictionary of query parameters to append to the URL.

Returns

TypeDescription
[WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)A mock HEAD request object.

trace()

@classmethod
def trace(
path: string,
secure: boolean,
headers: dict,
query_params: dict
) - > [WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)

Construct a TRACE request.

Parameters

NameTypeDescription
pathstringThe URL path for the request.
securebooleanIf True, the request will be made over HTTPS.
headersdictAdditional HTTP headers to include in the request.
query_paramsdictDictionary of query parameters to append to the URL.

Returns

TypeDescription
[WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)A mock TRACE request object.

options()

@classmethod
def options(
path: string,
data: any,
content_type: string,
secure: boolean,
headers: dict,
query_params: dict
) - > [WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)

Construct an OPTIONS request.

Parameters

NameTypeDescription
pathstringThe URL path for the request.
dataanyThe payload to be sent in the request body.
content_typestringThe MIME type of the body data.
securebooleanIf True, the request will be made over HTTPS.
headersdictAdditional HTTP headers to include in the request.
query_paramsdictDictionary of query parameters to append to the URL.

Returns

TypeDescription
[WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)A mock OPTIONS request object.

put()

@classmethod
def put(
path: string,
data: any,
content_type: string,
secure: boolean,
headers: dict,
query_params: dict
) - > [WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)

Construct a PUT request.

Parameters

NameTypeDescription
pathstringThe URL path for the request.
dataanyThe payload to be sent in the request body.
content_typestringThe MIME type of the body data.
securebooleanIf True, the request will be made over HTTPS.
headersdictAdditional HTTP headers to include in the request.
query_paramsdictDictionary of query parameters to append to the URL.

Returns

TypeDescription
[WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)A mock PUT request object.

patch()

@classmethod
def patch(
path: string,
data: any,
content_type: string,
secure: boolean,
headers: dict,
query_params: dict
) - > [WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)

Construct a PATCH request.

Parameters

NameTypeDescription
pathstringThe URL path for the request.
dataanyThe payload to be sent in the request body.
content_typestringThe MIME type of the body data.
securebooleanIf True, the request will be made over HTTPS.
headersdictAdditional HTTP headers to include in the request.
query_paramsdictDictionary of query parameters to append to the URL.

Returns

TypeDescription
[WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)A mock PATCH request object.

delete()

@classmethod
def delete(
path: string,
data: any,
content_type: string,
secure: boolean,
headers: dict,
query_params: dict
) - > [WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)

Construct a DELETE request.

Parameters

NameTypeDescription
pathstringThe URL path for the request.
dataanyThe payload to be sent in the request body.
content_typestringThe MIME type of the body data.
securebooleanIf True, the request will be made over HTTPS.
headersdictAdditional HTTP headers to include in the request.
query_paramsdictDictionary of query parameters to append to the URL.

Returns

TypeDescription
[WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)A mock DELETE request object.

generic()

@classmethod
def generic(
method: string,
path: string,
data: any,
content_type: string,
secure: boolean,
headers: dict,
query_params: dict
) - > [WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)

Construct an arbitrary HTTP request.

Parameters

NameTypeDescription
methodstringThe HTTP verb to use for the request (e.g., 'PUT', 'DELETE').
pathstringThe URL path for the request.
dataanyThe raw payload to be sent in the request body.
content_typestringThe MIME type of the body data.
securebooleanIf True, the request will be made over HTTPS.
headersdictAdditional HTTP headers to include in the request.
query_paramsdictDictionary of query parameters to append to the URL.

Returns

TypeDescription
[WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest)A mock request object configured with the specified HTTP method and parameters.