我定义了一个接受文件的API端点(例如,使用Django REST Framework).在Django中,检查响应时可以使用内容处置标头.
I defined an API endpoint which accepts a file (e.g. using Django REST Framework). In Django, the content disposition header can be used when inspecting the response.
docs.djangoproject/en/1.11/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment
现在,如果要在测试端点时设置标头,如何使用REST-Framework的APITestCase包含此标头?
Now, if we want to set the header when testing the endpoint, how do I include this header using REST-Framework's APITestCase?
到目前为止,我尝试过,但是似乎不接受标头.
What I tried so far is, but it does not seem to accept the headers.
class TestSaleViews(APITestCase): def test_sale_detail_view(self): f = create_named_temporary_file() files = {'archive': f} basename = os.path.basename(f.name) headers = { 'content-disposition': 'attachment; filename={}'.format(basename), } response = self.client.post(url, files, format='multipart', **headers) 推荐答案找到了答案!
Django在其FileUploadParser中对此标头具有固定的关键字.它是: HTTP_CONTENT_DISPOSITION
Django has a fixed keyword for this header in its FileUploadParser. It is: HTTP_CONTENT_DISPOSITION
所以我需要替换它,瞧!有效!
So I needed to replace it et voila: worked!
headers = { 'HTTP_CONTENT_DISPOSITION': 'attachment; filename={}'.format(basename), }github/编码/django-rest-framework/blob/master/rest_framework/parsers.py#L206
更多推荐
包括Django FileUpload的Content
发布评论