CARVIEW |
Select Language
HTTP/2 200
date: Mon, 13 Oct 2025 10:34:45 GMT
content-type: text/html; charset=utf-8
cross-origin-opener-policy: same-origin
nel: {"report_to":"heroku-nel","response_headers":["Via"],"max_age":3600,"success_fraction":0.01,"failure_fraction":0.1}
referrer-policy: same-origin
report-to: {"group":"heroku-nel","endpoints":[{"url":"https://nel.heroku.com/reports?s=x6Euso%2B3ZddlR19gZ2uS9xO8Lb0fvvG%2FERqwCoYDNFA%3D\u0026sid=af571f24-03ee-46d1-9f90-ab9030c2c74c\u0026ts=1760351685"}],"max_age":3600}
reporting-endpoints: heroku-nel="https://nel.heroku.com/reports?s=x6Euso%2B3ZddlR19gZ2uS9xO8Lb0fvvG%2FERqwCoYDNFA%3D&sid=af571f24-03ee-46d1-9f90-ab9030c2c74c&ts=1760351685"
server: cloudflare
strict-transport-security: max-age=15552000; includeSubDomains; preload
vary: Cookie
via: 1.1 heroku-router
x-content-type-options: nosniff
cf-cache-status: DYNAMIC
content-encoding: gzip
set-cookie: csrftoken=D3iGY0Sh3CSaNrmREpHNpGcgu523PvH7; SameSite=Lax; Path=/; Max-Age=31449600; Expires=Mon, 12 Oct 2026 10:34:45 GMT
cf-ray: 98de2fab6f71afda-BOM
alt-svc: h3=":443"; ma=86400
djangosnippets: RequestFactory: Easily create mock request objects, for use in testing
RequestFactory: Easily create mock request objects, for use in testing
Django's testing framework assumes you will be running your tests against "live" views that have been plugged in to your site's URL configuration - but sometimes you might want to run a test against a view function without first wiring it in to the rest of the site. This class makes it easy to do that by providing a "factory" for creating mock request objects, re-using the existing test Client interface (and most of the code). Once you've created a request object in your test you can use it to call your view functions directly, then run assertions against the response object that gets returned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | from django.test import Client
from django.core.handlers.wsgi import WSGIRequest
class RequestFactory(Client):
"""
Class that lets you create mock Request objects for use in testing.
Usage:
rf = RequestFactory()
get_request = rf.get('/hello/')
post_request = rf.post('/submit/', {'foo': 'bar'})
This class re-uses the django.test.client.Client interface, docs here:
https://www.djangoproject.com/documentation/testing/#the-test-client
Once you have a request object you can pass it to any view function,
just as if that view had been hooked up using a URLconf.
"""
def request(self, **request):
"""
Similar to parent class, but returns the request object as soon as it
has created it.
"""
environ = {
'HTTP_COOKIE': self.cookies,
'PATH_INFO': '/',
'QUERY_STRING': '',
'REQUEST_METHOD': 'GET',
'SCRIPT_NAME': '',
'SERVER_NAME': 'testserver',
'SERVER_PORT': 80,
'SERVER_PROTOCOL': 'HTTP/1.1',
}
environ.update(self.defaults)
environ.update(request)
return WSGIRequest(environ)
|
More like this
- Add Toggle Switch Widget to Django Forms by OgliariNatan 1 month ago
- get_object_or_none by azwdevops 4 months, 4 weeks ago
- Mask sensitive data from logger by agusmakmun 6 months, 3 weeks ago
- Template tag - list punctuation for a list of items by shapiromatron 1 year, 9 months ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 9 months ago
Comments
What would be a good way to have request.user included? Just assign a User object to the created request? It would be nice to have AnonymousUser there by default.
#
Answering myself, replace the last line with the following:
#
Is this still sufficient? You cut away large chunks of Client.request I notice by checking the SVN version.
#
Wondering if there's any solution for sending this request to a view that has the @csrf_protect_m decorator.
#
@cooncesean: this looks like it works ->
#
Please login first before commenting.