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.
https://docs.djangoproject.com/en/1.11/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With