Reputation: 1412
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.
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)
Upvotes: 8
Views: 4910
Reputation: 1412
Found the answer!
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),
}
https://github.com/encode/django-rest-framework/blob/master/rest_framework/parsers.py#L206
Upvotes: 16