【发布时间】:2021-08-10 14:56:17
【问题描述】:
要手动发送带有标头的 GET 请求,我使用 curl:
curl http://example.com/endpoint -H "Key: Value"
我想编写一个单元测试,它发出一个包含标头的请求(使用django.test.RequestFactory)。如何在请求中添加标头?
为清楚起见,下面是我希望 能够做的一个示例,尽管我在下面写的是 不是 有效代码,因为 RequestFactory.get()没有headers 参数:
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)
【问题讨论】:
标签: django unit-testing http-headers