Last active
June 20, 2023 20:49
-
-
Save davydany/a47a229e23569a54e0f9855fa35a40f9 to your computer and use it in GitHub Desktop.
Generate a HTTP Request that you can use to testing purposes with a view.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from django.test import RequestFactory | |
from django.http import QueryDict | |
def generate_http_request(url, user, method='GET', query_params=None, data=None, headers=None, request_attrs=None): | |
""" | |
Generate an HTTP request for a given URL and user. | |
Args: | |
url (str): The URL for the request. | |
user (User): The user making the request. | |
method (str): The HTTP method ('GET' or 'POST'). Defaults to 'GET'. | |
query_params (dict): A dictionary of query parameters for GET requests. Defaults to None. | |
data (dict): A dictionary of data for POST requests. Defaults to None. | |
headers (dict): A dictionary of headers for the request. Defaults to None. | |
Returns: | |
request: A mock HTTP request object. | |
""" | |
# Create an instance of the RequestFactory | |
factory = RequestFactory() | |
# Generate the request based on the specified method | |
if method == 'GET': | |
# Add query parameters to the URL if provided | |
if query_params: | |
url += '?' + '&'.join([f'{key}={value}' for key, value in query_params.items()]) | |
# Create a GET request with headers | |
request = factory.get(url, **(headers or {})) | |
if query_params: | |
request.query_params = QueryDict('&'.join([f'{key}={value}' for key, value in query_params.items()])) | |
else: | |
request.query_params = QueryDict() | |
request.data = QueryDict() | |
elif method == 'POST': | |
# Create a POST request with data payload and headers | |
request = factory.post(url, data=data, **(headers or {})) | |
else: | |
raise ValueError('Invalid method. Only "GET" and "POST" are supported.') | |
# Set the user for the request | |
request.user = user | |
# add any attributes to the request that needs to be added | |
if request_attrs: | |
for attr, value in request_attrs.items(): | |
setattr(request, attr, value) | |
# add headers as 'META' attributes | |
if headers: | |
for header, value in headers.items(): | |
request.META[header] = value | |
return request | |
def create_view(ViewClass, url, tenant, user, method='GET', query_params=None, data=None, headers=None, request_attrs=None, request_args=None, request_kwargs=None): | |
""" | |
Creates a view instance from the ViewClass and preps it up with everything needed to make a request. | |
""" | |
request = generate_http_request(url, user, method, query_params, data, headers, request_attrs) | |
view = ViewClass() | |
view.request = request | |
if request_args: | |
view.args = request_args | |
if request_kwargs: | |
view.kwargs = request_kwargs | |
request.tenant = tenant | |
return view, request |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment