在 APITestCase 中请求 headers
Request headers in APITestCase
我有一个测试(APITestCase
),我需要指定自定义headers:
class ListAppsAPITest(APITestCase):
def test_list_apps_versions(self):
response = self.client.get(reverse('api:applications:list'), None, **{'Device-Id': 'deadbeef'})
我尝试了不同的参数组合,但没有用。
如何在测试中指定自定义 headers?
因为 Django 有自己的方式来定义 header,您可以查看 here 以了解为什么以及如何实现。
With the exception of CONTENT_LENGTH and CONTENT_TYPE, as given above, any HTTP headers in the request are converted to META keys by converting all characters to uppercase, replacing any hyphens with underscores and adding an HTTP_ prefix to the name. So, for example, a header called X-Bender would be mapped to the META key HTTP_X_BENDER.
在你的情况下,像这样:
class ListAppsAPITest(APITestCase):
def test_list_apps_versions(self):
response = self.client.get(reverse('api:applications:list'), None, **{'HTTP_DEVICE_ID': 'deadbeef'})
希望对您有所帮助!
我有一个测试(APITestCase
),我需要指定自定义headers:
class ListAppsAPITest(APITestCase):
def test_list_apps_versions(self):
response = self.client.get(reverse('api:applications:list'), None, **{'Device-Id': 'deadbeef'})
我尝试了不同的参数组合,但没有用。
如何在测试中指定自定义 headers?
因为 Django 有自己的方式来定义 header,您可以查看 here 以了解为什么以及如何实现。
With the exception of CONTENT_LENGTH and CONTENT_TYPE, as given above, any HTTP headers in the request are converted to META keys by converting all characters to uppercase, replacing any hyphens with underscores and adding an HTTP_ prefix to the name. So, for example, a header called X-Bender would be mapped to the META key HTTP_X_BENDER.
在你的情况下,像这样:
class ListAppsAPITest(APITestCase):
def test_list_apps_versions(self):
response = self.client.get(reverse('api:applications:list'), None, **{'HTTP_DEVICE_ID': 'deadbeef'})
希望对您有所帮助!