Debian Codegraph @

django_downloadview/apache/tests.py

raw
1 from django_downloadview.apache.response import XSendfileResponse
2
3
4 class XSendfileValidator:
5 """Utility class to validate XSendfileResponse instances.
6
7 See also :py:func:`assert_x_sendfile` shortcut function.
8
9 """
10
11 def __call__(self, test_case, response, **assertions):
12 """Assert that ``response`` is a valid X-Sendfile response.
13
14 Optional ``assertions`` dictionary can be used to check additional
15 items:
16
17 * ``basename``: the basename of the file in the response.
18
19 * ``content_type``: the value of "Content-Type" header.
20
21 * ``file_path``: the value of "X-Sendfile" header.
22
23 """
24 self.assert_x_sendfile_response(test_case, response)
25 for key, value in assertions.items():
26 assert_func = getattr(self, "assert_%s" % key)
27 assert_func(test_case, response, value)
28
29 def assert_x_sendfile_response(self, test_case, response):
30 test_case.assertTrue(isinstance(response, XSendfileResponse))
31
32 def assert_basename(self, test_case, response, value):
33 test_case.assertEqual(response.basename, value)
34
35 def assert_content_type(self, test_case, response, value):
36 test_case.assertEqual(response["Content-Type"], value)
37
38 def assert_file_path(self, test_case, response, value):
39 test_case.assertEqual(response["X-Sendfile"], value)
40
41 def assert_attachment(self, test_case, response, value):
42 header = "Content-Disposition"
43 if value:
44 test_case.assertTrue(response[header].startswith("attachment"))
45 else:
46 test_case.assertFalse(header in response)
47
48
49 def assert_x_sendfile(test_case, response, **assertions):
50 """Make ``test_case`` assert that ``response`` is a XSendfileResponse.
51
52 Optional ``assertions`` dictionary can be used to check additional items:
53
54 * ``basename``: the basename of the file in the response.
55
56 * ``content_type``: the value of "Content-Type" header.
57
58 * ``file_path``: the value of "X-Sendfile" header.
59
60 """
61 validator = XSendfileValidator()
62 return validator(test_case, response, **assertions)