Debian Codegraph @

tests/sendfile.py

raw
1 """Tests around :py:mod:`django_downloadview.sendfile`."""
2
3 from django.http import Http404
4 import django.test
5
6 from django_downloadview.response import DownloadResponse
7 from django_downloadview.shortcuts import sendfile
8
9
10 class SendfileTestCase(django.test.TestCase):
11 """Tests around :func:`django_downloadview.sendfile.sendfile`."""
12
13 def test_defaults(self):
14 """sendfile() takes at least request and filename."""
15 request = django.test.RequestFactory().get("/fake-url")
16 filename = __file__
17 response = sendfile(request, filename)
18 self.assertTrue(isinstance(response, DownloadResponse))
19 self.assertFalse(response.attachment)
20
21 def test_custom(self):
22 """sendfile() accepts various arguments for response tuning."""
23 request = django.test.RequestFactory().get("/fake-url")
24 filename = __file__
25 response = sendfile(
26 request,
27 filename,
28 attachment=True,
29 attachment_filename="toto.txt",
30 mimetype="test/octet-stream",
31 encoding="gzip",
32 )
33 self.assertTrue(isinstance(response, DownloadResponse))
34 self.assertTrue(response.attachment)
35 self.assertEqual(response.basename, "toto.txt")
36 self.assertEqual(response["Content-Type"], "test/octet-stream; charset=utf-8")
37 self.assertEqual(response.get_encoding(), "gzip")
38
39 def test_404(self):
40 """sendfile() raises Http404 if file does not exists."""
41 request = django.test.RequestFactory().get("/fake-url")
42 filename = "i-do-no-exist"
43 with self.assertRaises(Http404):
44 sendfile(request, filename)