def _get_requested_range(request: Request) -> Optional[Range]: # http://svn.tools.ietf.org/svn/wg/httpbis/specs/rfc7233.html#rfc.section.3.1 # A server must ignore a Range header field received with a request method # other than GET if request.method != "GET": return None # NB: only the first Range request header is taken into consideration; # if the HTTP contains several Range headers, only the first is used range_header = request.get_first_header(b"range") if not range_header: return None try: value = Range.parse(range_header) except InvalidRangeValue: raise BadRequest("Invalid Range header") else: # An origin server must ignore a Range header field that contains # a range unit it does not understand. if value.unit != "bytes": return None return value
def test_range_throws_value_error_for_invalid_value(): with pytest.raises(ValueError): Range.parse("bytes=100-200, 400-600, 300-500=50-150")
def test_parse_range(value, expected_range): parsed_range = Range.parse(value) assert parsed_range == expected_range
def test_raise_for_invalid_value(invalid_value): with pytest.raises(InvalidRangeValue): Range.parse(invalid_value)