'How do I add a header to a Django RequestFactory request?
To manually send a GET request with a header, I do this with curl:
curl http://example.com/endpoint -H "Key: Value"
I want to write a unit test that makes a request (using django.test.RequestFactory) containing a header. How can I add a header to the request?
For clarity, below is an example of what I'd like to be able to do, though what I have written below is not valid code because RequestFactory.get() does not have a headers parameter:
from django.test import TestCase, RequestFactory
class TestClassForMyDjangoApp(TestCase):
def test_for_my_app(self):
factory = RequestFactory()
my_header = dict(key=value, another_key=another_value)
factory.get('/endpoint/', headers=my_header)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
