feat(Response): Add support for setting the Accept-Ranges header (#909)

* feat(Response): Add support for setting the Accept-Ranges header

See also: https://tools.ietf.org/html/rfc7233#section-2.3

Fixes #646

* doc(Response): Edit the docstring for accept_ranges for clarity, style
This commit is contained in:
Kurt Griffiths
2016-09-22 20:09:19 -06:00
committed by John Vrbanac
parent 67d6102984
commit a7ae37a090
2 changed files with 23 additions and 0 deletions

View File

@@ -625,6 +625,24 @@ class Response(object):
""",
lambda v: ', '.join(v))
accept_ranges = header_property(
'Accept-Ranges',
"""Sets the Accept-Ranges header.
The Accept-Ranges header field indicates to the client which
range units are supported (e.g. "bytes") for the target
resource.
If range requests are not supported for the target resource,
the header may be set to "none" to advise the client not to
attempt any such requests.
Note:
"none" is the literal string, not Python's built-in ``None``
type.
""")
def _encode_header(self, name, value, py2=PY2):
if py2:
if isinstance(name, unicode):

View File

@@ -50,6 +50,8 @@ class HeaderHelpersResource(object):
else:
resp.content_range = (0, 25, 100, req.range_unit)
resp.accept_ranges = 'bytes'
self.resp = resp
def on_head(self, req, resp):
@@ -322,6 +324,9 @@ class TestHeaders(testing.TestCase):
resp.content_range = (1, 499, 10 * 1024, 'bytes')
self.assertEqual(resp.content_range, 'bytes 1-499/10240')
self.assertEqual(resp.accept_ranges, 'bytes')
self.assertEqual(result.headers['Accept-Ranges'], 'bytes')
req_headers = {'Range': 'items=0-25'}
result = self.simulate_get(headers=req_headers)
self.assertEqual(result.headers['Content-Range'], 'items 0-25/100')