Note that there are some explanatory texts on larger screens.

plurals
  1. POdjango test file download - "ValueError: I/O operation on closed file"
    primarykey
    data
    text
    <p>I have code for a view which serves a file download, and it works fine in the browser. Now I am trying to write a test for it, using the internal django Client.get:</p> <pre><code> response = self.client.get("/compile-book/", {'id': book.id}) self.assertEqual(response.status_code, 200) self.assertEquals(response.get('Content-Disposition'), "attachment; filename=book.zip") </code></pre> <p>so far so good. Now I would like to test if the downloaded file is the one I expect it to download. So I start by saying: </p> <pre><code> f = cStringIO.StringIO(response.content) </code></pre> <p>Now my test runner responds with:</p> <pre><code>Traceback (most recent call last): File ".../tests.py", line 154, in test_download f = cStringIO.StringIO(response.content) File "/home/epub/projects/epub-env/lib/python2.7/site-packages/django/http/response.py", line 282, in content self._consume_content() File "/home/epub/projects/epub-env/lib/python2.7/site-packages/django/http/response.py", line 278, in _consume_content self.content = b''.join(self.make_bytes(e) for e in self._container) File "/home/epub/projects/epub-env/lib/python2.7/site-packages/django/http/response.py", line 278, in &lt;genexpr&gt; self.content = b''.join(self.make_bytes(e) for e in self._container) File "/usr/lib/python2.7/wsgiref/util.py", line 30, in next data = self.filelike.read(self.blksize) ValueError: I/O operation on closed file </code></pre> <p>Even when I do simply: self.assertIsNotNone(response.content) I get the same ValueError</p> <p>The <em>only</em> topic on the entire internet (including django docs) I could find about testing downloads was this stackoverflow topic: <a href="https://stackoverflow.com/questions/8244220/django-unit-test-for-testing-a-file-download">Django Unit Test for testing a file download</a>. Trying that solution led to these results. It is old and rare enough for me to open a new question.</p> <p>Anybody knows how the testing of downloads is supposed to be handled in Django? (btw, running django 1.5 on python 2.7)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload