async def test_get_range_file_getter_raises_for_invalid(): getter = get_range_file_getter(FilesHandler(), files2_index_path, 100, Range("bytes", [RangePart(None, None)])) with pytest.raises(BadRequest): async for chunk in getter(): ...
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 _validate_range(requested_range: Range, file_size: int) -> None: if not requested_range.can_satisfy(file_size): raise RangeNotSatisfiable()
def test_parse_range(value, expected_range): parsed_range = Range.parse(value) assert parsed_range == expected_range
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_raise_for_invalid_value(invalid_value): with pytest.raises(InvalidRangeValue): Range.parse(invalid_value)
import pytest from blacksheep.ranges import Range, RangePart, InvalidRangeValue @pytest.mark.parametrize( "range_a,range_b,equals", [ [ Range("bytes", [RangePart(100, None)]), Range("bytes", [RangePart(100, None)]), True, ], [ Range("bytes", [RangePart(100, None)]), Range("bytes", [RangePart(None, 100)]), False, ], [ Range("bytes", [RangePart(100, 200), RangePart(400, 600)]), Range("bytes", [RangePart(100, 200), RangePart(400, 600)]), True, ], [ Range("bytes", [RangePart(100, 200), RangePart(400, 600)]), Range("bytes", [RangePart(100, 200), RangePart(400, 650)]), False, ],
import pytest from blacksheep.ranges import Range, RangePart, InvalidRangeValue @pytest.mark.parametrize('range_a,range_b,equals', [ [ Range('bytes', [RangePart(100, None)]), Range('bytes', [RangePart(100, None)]), True ], [ Range('bytes', [RangePart(100, None)]), Range('bytes', [RangePart(None, 100)]), False ], [ Range('bytes', [RangePart(100, 200), RangePart(400, 600)]), Range('bytes', [RangePart(100, 200), RangePart(400, 600)]), True ], [ Range('bytes', [RangePart(100, 200), RangePart(400, 600)]), Range('bytes', [RangePart(100, 200), RangePart(400, 650)]), False ], [ Range('bytes', [RangePart(100, 200), RangePart(400, 600), RangePart(300, 500)]), Range('bytes',