File: sendfile.py

package info (click to toggle)
django-downloadview 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 748 kB
  • sloc: python: 2,507; makefile: 187
file content (44 lines) | stat: -rw-r--r-- 1,678 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""Tests around :py:mod:`django_downloadview.sendfile`."""

from django.http import Http404
import django.test

from django_downloadview.response import DownloadResponse
from django_downloadview.shortcuts import sendfile


class SendfileTestCase(django.test.TestCase):
    """Tests around :func:`django_downloadview.sendfile.sendfile`."""

    def test_defaults(self):
        """sendfile() takes at least request and filename."""
        request = django.test.RequestFactory().get("/fake-url")
        filename = __file__
        response = sendfile(request, filename)
        self.assertTrue(isinstance(response, DownloadResponse))
        self.assertFalse(response.attachment)

    def test_custom(self):
        """sendfile() accepts various arguments for response tuning."""
        request = django.test.RequestFactory().get("/fake-url")
        filename = __file__
        response = sendfile(
            request,
            filename,
            attachment=True,
            attachment_filename="toto.txt",
            mimetype="test/octet-stream",
            encoding="gzip",
        )
        self.assertTrue(isinstance(response, DownloadResponse))
        self.assertTrue(response.attachment)
        self.assertEqual(response.basename, "toto.txt")
        self.assertEqual(response["Content-Type"], "test/octet-stream; charset=utf-8")
        self.assertEqual(response.get_encoding(), "gzip")

    def test_404(self):
        """sendfile() raises Http404 if file does not exists."""
        request = django.test.RequestFactory().get("/fake-url")
        filename = "i-do-no-exist"
        with self.assertRaises(Http404):
            sendfile(request, filename)