RequestFactory
Class that lets you create mock Request objects for use in testing.
Attributes
| Attribute | Type | Description |
|---|---|---|
| json_encoder | type = DjangoJSONEncoder | The JSON encoder class used to serialize dictionary, list, or tuple data when the content type is application/json. |
| defaults | dict | A dictionary of default WSGI environment variables that are merged into every request created by the factory. |
| cookies | SimpleCookie = SimpleCookie() | A SimpleCookie object used to store and provide cookie data for the HTTP_COOKIE header in generated requests. |
| errors | BytesIO = 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
| Name | Type | Description |
|---|---|---|
| json_encoder | class = DjangoJSONEncoder | The JSON encoder class to use when encoding JSON request data. |
| headers | dict = null | A dictionary of HTTP headers to be included in every request. |
| query_params | dict = null | A dictionary of query parameters to be included in every request. |
| defaults | kwargs = null | Additional 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
| Name | Type | Description |
|---|---|---|
| **request | dict | Environment variables used to populate the WSGI environment for the new request. |
Returns
| Type | Description |
|---|---|
[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
| Name | Type | Description |
|---|---|---|
| path | string | The URL path for the request. |
| data | dict | Dictionary of query parameters; raises ValueError if used alongside query_params. |
| secure | boolean | If True, the request will be made over HTTPS. |
| headers | dict | Additional HTTP headers to include in the request. |
| query_params | dict | Dictionary of query parameters to append to the URL. |
Returns
| Type | Description |
|---|---|
[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
| Name | Type | Description |
|---|---|---|
| path | string | The URL path for the request. |
| data | any | The payload to be sent in the request body. |
| content_type | string | The MIME type of the body data, defaulting to multipart/form-data. |
| secure | boolean | If True, the request will be made over HTTPS. |
| headers | dict | Additional HTTP headers to include in the request. |
| query_params | dict | Dictionary of query parameters to append to the URL. |
Returns
| Type | Description |
|---|---|
[WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest) | A mock POST request object containing the encoded body data. |
head()
@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
| Name | Type | Description |
|---|---|---|
| path | string | The URL path for the request. |
| data | dict | Dictionary of query parameters; raises ValueError if used alongside query_params. |
| secure | boolean | If True, the request will be made over HTTPS. |
| headers | dict | Additional HTTP headers to include in the request. |
| query_params | dict | Dictionary of query parameters to append to the URL. |
Returns
| Type | Description |
|---|---|
[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
| Name | Type | Description |
|---|---|---|
| path | string | The URL path for the request. |
| secure | boolean | If True, the request will be made over HTTPS. |
| headers | dict | Additional HTTP headers to include in the request. |
| query_params | dict | Dictionary of query parameters to append to the URL. |
Returns
| Type | Description |
|---|---|
[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
| Name | Type | Description |
|---|---|---|
| path | string | The URL path for the request. |
| data | any | The payload to be sent in the request body. |
| content_type | string | The MIME type of the body data. |
| secure | boolean | If True, the request will be made over HTTPS. |
| headers | dict | Additional HTTP headers to include in the request. |
| query_params | dict | Dictionary of query parameters to append to the URL. |
Returns
| Type | Description |
|---|---|
[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
| Name | Type | Description |
|---|---|---|
| path | string | The URL path for the request. |
| data | any | The payload to be sent in the request body. |
| content_type | string | The MIME type of the body data. |
| secure | boolean | If True, the request will be made over HTTPS. |
| headers | dict | Additional HTTP headers to include in the request. |
| query_params | dict | Dictionary of query parameters to append to the URL. |
Returns
| Type | Description |
|---|---|
[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
| Name | Type | Description |
|---|---|---|
| path | string | The URL path for the request. |
| data | any | The payload to be sent in the request body. |
| content_type | string | The MIME type of the body data. |
| secure | boolean | If True, the request will be made over HTTPS. |
| headers | dict | Additional HTTP headers to include in the request. |
| query_params | dict | Dictionary of query parameters to append to the URL. |
Returns
| Type | Description |
|---|---|
[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
| Name | Type | Description |
|---|---|---|
| path | string | The URL path for the request. |
| data | any | The payload to be sent in the request body. |
| content_type | string | The MIME type of the body data. |
| secure | boolean | If True, the request will be made over HTTPS. |
| headers | dict | Additional HTTP headers to include in the request. |
| query_params | dict | Dictionary of query parameters to append to the URL. |
Returns
| Type | Description |
|---|---|
[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
| Name | Type | Description |
|---|---|---|
| method | string | The HTTP verb to use for the request (e.g., 'PUT', 'DELETE'). |
| path | string | The URL path for the request. |
| data | any | The raw payload to be sent in the request body. |
| content_type | string | The MIME type of the body data. |
| secure | boolean | If True, the request will be made over HTTPS. |
| headers | dict | Additional HTTP headers to include in the request. |
| query_params | dict | Dictionary of query parameters to append to the URL. |
Returns
| Type | Description |
|---|---|
[WSGIRequest](../../core/handlers/wsgi/wsgirequest.md?sid=django_core_handlers_wsgi_wsgirequest) | A mock request object configured with the specified HTTP method and parameters. |