Beispiel #1
0
def test_range() -> None:
    range_ = Range.from_header('bytes=500-600,601-999')
    assert range_.units == 'bytes'
    assert range_.ranges == [RangeSet(500, 600), RangeSet(601, 999)]
    assert range_.to_header() == 'bytes=500-600,601-999'
    range_ = Range.from_header('bytes=-999')
    assert range_.units == 'bytes'
    assert range_.ranges == [RangeSet(-999, None)]
    assert range_.to_header() == 'bytes=-999'
Beispiel #2
0
async def test_response_make_conditional() -> None:
    response = Response(b"abcdef")
    await response.make_conditional(Range("bytes", [RangeSet(0, 3)]))
    assert b"abc" == (await response.get_data())  # type: ignore
    assert response.status_code == 206
    assert response.accept_ranges == "bytes"
    assert response.content_range == ContentRange("bytes", 0, 2, 6)
Beispiel #3
0
    csp.img_src = "'none'"
    assert csp.to_header() == "default-src * 'self' quart.com; img-src 'none'"


def test_etags() -> None:
    etags = ETags.from_header('W/"67ab43", "54ed21"')
    assert etags.weak == {'67ab43'}
    assert etags.strong == {'54ed21'}
    assert '54ed21' in etags
    assert etags.to_header() == 'W/"67ab43","54ed21"'


@pytest.mark.parametrize(
    "header, expected_ranges",
    [
        ("bytes=500-600,601-999", [RangeSet(500, 600), RangeSet(601, 999)]),
        ("bytes=-999", [RangeSet(-999, None)]),
        ("bytes=0-", [RangeSet(0, None)]),
    ],
)
def test_range(header: str, expected_ranges: List[RangeSet]) -> None:
    range_ = Range.from_header(header)
    assert range_.units == 'bytes'
    assert range_.ranges == expected_ranges
    assert range_.to_header() == header


def test_header_set() -> None:
    updated = False

    def on_update(_: HeaderSet) -> None:
Beispiel #4
0
    assert b"Body" == (await response.get_data())  # type: ignore


@pytest.mark.asyncio
async def test_response_make_conditional() -> None:
    response = Response(b"abcdef")
    await response.make_conditional(Range("bytes", [RangeSet(0, 3)]))
    assert b"abc" == (await response.get_data())  # type: ignore
    assert response.status_code == 206
    assert response.accept_ranges == "bytes"
    assert response.content_range == ContentRange("bytes", 0, 2, 6)


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "range_", [Range("", {}), Range("bytes", [RangeSet(0, 6)])])
async def test_response_make_conditional_no_condition(range_: Range) -> None:
    response = Response(b"abcdef")
    await response.make_conditional(range_)
    assert b"abcdef" == (await response.get_data())  # type: ignore
    assert response.status_code == 200
    assert response.accept_ranges == "bytes"


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "range_",
    [
        Range("seconds", [RangeSet(0, 3)]),
        Range("bytes", [RangeSet(0, 2), RangeSet(3, 5)]),
        Range("bytes", [RangeSet(0, 8)]),
Beispiel #5
0
    # Fetch again to ensure it isn't exhausted
    assert b'Body' == (await response.get_data())  # type: ignore


@pytest.mark.asyncio
async def test_response_make_conditional() -> None:
    response = Response(b"abcdef")
    await response.make_conditional(Range("bytes", [RangeSet(0, 3)]))
    assert b"abc" == (await response.get_data())  # type: ignore
    assert response.status_code == 206
    assert response.accept_ranges == "bytes"
    assert response.content_range == ContentRange("bytes", 0, 2, 6)


@pytest.mark.asyncio
@pytest.mark.parametrize("range_", [Range("", {}), Range("bytes", [RangeSet(0, 6)])])
async def test_response_make_conditional_no_condition(range_: Range) -> None:
    response = Response(b"abcdef")
    await response.make_conditional(range_)
    assert b"abcdef" == (await response.get_data())  # type: ignore
    assert response.status_code == 200
    assert response.accept_ranges == "bytes"


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "range_",
    [
        Range("seconds", [RangeSet(0, 3)]),
        Range("bytes", [RangeSet(0, 2), RangeSet(3, 5)]),
        Range("bytes", [RangeSet(0, 8)]),