| 1 |
from django_downloadview.nginx.response import XAccelRedirectResponse |
| 2 |
|
| 3 |
|
| 4 |
class XAccelRedirectValidator: |
| 5 |
"""Utility class to validate XAccelRedirectResponse instances. |
| 6 |
|
| 7 |
See also :py:func:`assert_x_accel_redirect` shortcut function. |
| 8 |
|
| 9 |
""" |
| 10 |
|
| 11 |
def __call__(self, test_case, response, **assertions): |
| 12 |
"""Assert that ``response`` is a valid X-Accel-Redirect 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 |
* ``redirect_url``: the value of "X-Accel-Redirect" header. |
| 22 |
|
| 23 |
* ``charset``: the value of ``X-Accel-Charset`` header. |
| 24 |
|
| 25 |
* ``with_buffering``: the value of ``X-Accel-Buffering`` header. |
| 26 |
If ``False``, then makes sure that the header disables buffering. |
| 27 |
If ``None``, then makes sure that the header is not set. |
| 28 |
|
| 29 |
* ``expires``: the value of ``X-Accel-Expires`` header. |
| 30 |
If ``False``, then makes sure that the header disables expiration. |
| 31 |
If ``None``, then makes sure that the header is not set. |
| 32 |
|
| 33 |
* ``limit_rate``: the value of ``X-Accel-Limit-Rate`` header. |
| 34 |
If ``False``, then makes sure that the header disables limit rate. |
| 35 |
If ``None``, then makes sure that the header is not set. |
| 36 |
|
| 37 |
""" |
| 38 |
self.assert_x_accel_redirect_response(test_case, response) |
| 39 |
for key, value in assertions.items(): |
| 40 |
assert_func = getattr(self, "assert_%s" % key) |
| 41 |
assert_func(test_case, response, value) |
| 42 |
|
| 43 |
def assert_x_accel_redirect_response(self, test_case, response): |
| 44 |
test_case.assertTrue(isinstance(response, XAccelRedirectResponse)) |
| 45 |
|
| 46 |
def assert_basename(self, test_case, response, value): |
| 47 |
test_case.assertEqual(response.basename, value) |
| 48 |
|
| 49 |
def assert_content_type(self, test_case, response, value): |
| 50 |
test_case.assertEqual(response["Content-Type"], value) |
| 51 |
|
| 52 |
def assert_redirect_url(self, test_case, response, value): |
| 53 |
test_case.assertEqual(response["X-Accel-Redirect"], value) |
| 54 |
|
| 55 |
def assert_charset(self, test_case, response, value): |
| 56 |
test_case.assertEqual(response["X-Accel-Charset"], value) |
| 57 |
|
| 58 |
def assert_with_buffering(self, test_case, response, value): |
| 59 |
header = "X-Accel-Buffering" |
| 60 |
if value is None: |
| 61 |
test_case.assertFalse(header in response) |
| 62 |
elif value: |
| 63 |
test_case.assertEqual(header, "yes") |
| 64 |
else: |
| 65 |
test_case.assertEqual(header, "no") |
| 66 |
|
| 67 |
def assert_expires(self, test_case, response, value): |
| 68 |
header = "X-Accel-Expires" |
| 69 |
if value is None: |
| 70 |
test_case.assertFalse(header in response) |
| 71 |
elif not value: |
| 72 |
test_case.assertEqual(header, "off") |
| 73 |
else: |
| 74 |
test_case.assertEqual(header, value) |
| 75 |
|
| 76 |
def assert_limit_rate(self, test_case, response, value): |
| 77 |
header = "X-Accel-Limit-Rate" |
| 78 |
if value is None: |
| 79 |
test_case.assertFalse(header in response) |
| 80 |
elif not value: |
| 81 |
test_case.assertEqual(header, "off") |
| 82 |
else: |
| 83 |
test_case.assertEqual(header, value) |
| 84 |
|
| 85 |
def assert_attachment(self, test_case, response, value): |
| 86 |
header = "Content-Disposition" |
| 87 |
if value: |
| 88 |
test_case.assertTrue(response[header].startswith("attachment")) |
| 89 |
else: |
| 90 |
test_case.assertFalse(header in response) |
| 91 |
|
| 92 |
|
| 93 |
def assert_x_accel_redirect(test_case, response, **assertions): |
| 94 |
"""Make ``test_case`` assert that ``response`` is a XAccelRedirectResponse. |
| 95 |
|
| 96 |
Optional ``assertions`` dictionary can be used to check additional items: |
| 97 |
|
| 98 |
* ``basename``: the basename of the file in the response. |
| 99 |
|
| 100 |
* ``content_type``: the value of "Content-Type" header. |
| 101 |
|
| 102 |
* ``redirect_url``: the value of "X-Accel-Redirect" header. |
| 103 |
|
| 104 |
* ``charset``: the value of ``X-Accel-Charset`` header. |
| 105 |
|
| 106 |
* ``with_buffering``: the value of ``X-Accel-Buffering`` header. |
| 107 |
If ``False``, then makes sure that the header disables buffering. |
| 108 |
If ``None``, then makes sure that the header is not set. |
| 109 |
|
| 110 |
* ``expires``: the value of ``X-Accel-Expires`` header. |
| 111 |
If ``False``, then makes sure that the header disables expiration. |
| 112 |
If ``None``, then makes sure that the header is not set. |
| 113 |
|
| 114 |
* ``limit_rate``: the value of ``X-Accel-Limit-Rate`` header. |
| 115 |
If ``False``, then makes sure that the header disables limit rate. |
| 116 |
If ``None``, then makes sure that the header is not set. |
| 117 |
|
| 118 |
""" |
| 119 |
validator = XAccelRedirectValidator() |
| 120 |
return validator(test_case, response, **assertions) |