Django Unit Test for testing a file download

If the url is meant to produce a file rather than a “normal” http response, then its content-type and/or content-disposition will be different.

the response object is basically a dictionary, so you could so something like

self.assertEquals(
    response.get('Content-Disposition'),
    "attachment; filename=mypic.jpg"
)

more info:
https://docs.djangoproject.com/en/dev/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment

UPD:
If you want to read the actual contents of the attached file, you can use response.content. Example for a zip file:

try:
    f = io.BytesIO(response.content)
    zipped_file = zipfile.ZipFile(f, 'r')

    self.assertIsNone(zipped_file.testzip())        
    self.assertIn('my_file.txt', zipped_file.namelist())
finally:
    zipped_file.close()
    f.close()

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)