From a7ae37a09002d951a944a78dcce273234319cdaf Mon Sep 17 00:00:00 2001 From: Kurt Griffiths Date: Thu, 22 Sep 2016 20:09:19 -0600 Subject: [PATCH] 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 --- falcon/response.py | 18 ++++++++++++++++++ tests/test_headers.py | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/falcon/response.py b/falcon/response.py index 6e73436..eabced5 100644 --- a/falcon/response.py +++ b/falcon/response.py @@ -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): diff --git a/tests/test_headers.py b/tests/test_headers.py index 3244f22..387690f 100644 --- a/tests/test_headers.py +++ b/tests/test_headers.py @@ -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')