Take the 2-minute tour ×

Right now I'm just checking the response of the link like so:

self.client = Client()
response = self.client.get(url)
self.assertEqual(response.status_code, 200)

Is there a Django-ic way to test a link to see if a file download event actually takes place? Can't seem to find much resource on this topic.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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

    "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:

    f = StringIO.StringIO(response.content)
    zipped_file = zipfile.ZipFile(f, 'r')

    self.assertIn('my_file.txt', zipped_file.namelist())
share|improve this answer
Yes of course! Thanks. –  super9 Nov 23 '11 at 15:29
yes but you don't control the file downloaded... –  francois Oct 8 '12 at 20:25
do you mean you want to check on the actual contents of the file? you can use response.content -- docs.djangoproject.com/en/dev/ref/request-response/#id4 –  hwjp Oct 9 '12 at 8:44
add comment

Your Answer


