Exemple #1
0
async def test_invalid_range_request_range_not_satisfiable(range_value):
    file_path = _get_file_path('example.txt')
    with pytest.raises(RangeNotSatisfiable):
        get_response_for_file(Request('GET', b'/example',
                                      [(b'Range', range_value)]),
                              file_path,
                              1200)
Exemple #2
0
async def test_invalid_range_request_range_not_satisfiable(range_value):
    file_path = get_file_path("example.txt")
    with pytest.raises(RangeNotSatisfiable):
        get_response_for_file(
            FilesHandler(),
            Request("GET", b"/example", [(b"Range", range_value)]),
            file_path,
            1200,
        )
Exemple #3
0
async def test_text_file_range_request_single_part_if_range_handling(
        range_value, matches):
    file_path = get_file_path("example.txt")
    info = FileInfo.from_path(file_path)

    response = get_response_for_file(
        FilesHandler(),
        Request(
            "GET",
            b"/example",
            [
                (b"Range", range_value),
                (b"If-Range", info.etag.encode() +
                 (b"" if matches else b"xx")),
            ],
        ),
        file_path,
        1200,
    )

    expected_status = 206 if matches else 200

    assert response.status == expected_status

    if not matches:
        body = await response.read()

        with open(file_path, mode="rb") as actual_file:
            assert body == actual_file.read()
Exemple #4
0
async def test_get_response_for_file_with_head_method_returns_empty_body_with_info(file_path):
    response = get_response_for_file(Request('HEAD', b'/example', None),
                                     file_path,
                                     1200)

    assert response.status == 200
    data = await response.read()
    assert data is None
Exemple #5
0
async def test_get_response_for_file_with_head_method_returns_empty_body_with_info(
    file_path, ):
    response = get_response_for_file(FilesHandler(),
                                     Request("HEAD", b"/example", None),
                                     file_path, 1200)

    assert response.status == 200
    data = await response.read()
    assert data is None
Exemple #6
0
async def test_get_response_for_file_returns_cache_control_header(cache_time):
    response = get_response_for_file(FilesHandler(),
                                     Request("GET", b"/example", None),
                                     TEST_FILES[0], cache_time)

    assert response.status == 200
    header = response.get_single_header(b"cache-control")

    assert header == f"max-age={cache_time}".encode()
Exemple #7
0
async def test_get_response_for_file_returns_cache_control_header(cache_time):
    response = get_response_for_file(Request('GET', b'/example', None),
                                     TEST_FILES[0],
                                     cache_time)

    assert response.status == 200
    header = response.get_single_header(b'cache-control')

    assert header == f'max-age={cache_time}'.encode()
Exemple #8
0
async def test_text_file_range_request_single_part(range_value, expected_bytes, expected_content_range):
    file_path = _get_file_path('example.txt')
    response = get_response_for_file(Request('GET', b'/example',
                                             [(b'Range', range_value)]),
                                     file_path,
                                     1200)
    assert response.status == 206
    body = await response.read()
    assert body == expected_bytes

    assert response.get_single_header(b'content-range') == expected_content_range
Exemple #9
0
async def test_get_response_for_file_returns_not_modified_handling_if_none_match_header(file_path, method):
    info = FileInfo.from_path(file_path)

    response = get_response_for_file(Request(method, b'/example',
                                             [(b'If-None-Match', info.etag.encode())]),
                                     file_path,
                                     1200)

    assert response.status == 304
    data = await response.read()
    assert data is None
Exemple #10
0
async def test_get_response_for_file_returns_file_contents(file_path):
    response = get_response_for_file(Request('GET', b'/example', None),
                                     file_path,
                                     1200)

    assert response.status == 200
    data = await response.read()

    with open(file_path, mode='rb') as test_file:
        contents = test_file.read()

    assert data == contents
Exemple #11
0
async def test_get_response_for_file_returns_file_contents(file_path):
    response = get_response_for_file(FilesHandler(),
                                     Request("GET", b"/example", None),
                                     file_path, 1200)

    assert response.status == 200
    data = await response.read()

    with open(file_path, mode="rb") as test_file:
        contents = test_file.read()

    assert data == contents
Exemple #12
0
async def test_text_file_range_request_multi_part(range_value, expected_bytes_lines: bytes):
    file_path = _get_file_path('example.txt')
    response = get_response_for_file(Request('GET', b'/example',
                                             [(b'Range', range_value)]),
                                     file_path,
                                     1200)
    assert response.status == 206
    content_type = response.content.type
    boundary = content_type.split(b'=')[1]
    body = await response.read()

    expected_bytes_lines = [line.replace(b'##BOUNDARY##', boundary) for line in expected_bytes_lines]
    assert body.splitlines() == expected_bytes_lines
Exemple #13
0
async def test_text_file_range_request_single_part(range_value, expected_bytes,
                                                   expected_content_range):
    file_path = get_file_path("example.txt")
    response = get_response_for_file(
        FilesHandler(),
        Request("GET", b"/example", [(b"Range", range_value)]),
        file_path,
        1200,
    )
    assert response.status == 206
    body = await response.read()
    assert body == expected_bytes

    assert response.get_single_header(
        b"content-range") == expected_content_range
Exemple #14
0
async def test_text_file_range_request_multi_part(
        range_value: bytes, expected_bytes_lines: List[bytes]):
    file_path = get_file_path("example.txt")
    response = get_response_for_file(
        FilesHandler(),
        Request("GET", b"/example", [(b"Range", range_value)]),
        file_path,
        1200,
    )
    assert response.status == 206
    content_type = response.content.type
    boundary = content_type.split(b"=")[1]
    body = await response.read()

    expected_bytes_lines = [
        line.replace(b"##BOUNDARY##", boundary)
        for line in expected_bytes_lines
    ]
    assert body.splitlines() == expected_bytes_lines
Exemple #15
0
async def test_get_response_for_file_returns_headers_with_information(file_path, method):
    response = get_response_for_file(Request(method, b'/example', None),
                                     file_path,
                                     1200)

    assert response.status == 200

    info = FileInfo.from_path(file_path)
    expected_headers = {
        b'etag': info.etag.encode(),
        b'last-modified': str(info.modified_time).encode(),
        b'accept-ranges': b'bytes',
        b'cache-control': b'max-age=1200'
    }

    for expected_header_name, expected_header_value in expected_headers.items():
        value = response.get_single_header(expected_header_name)

        assert value is not None
        assert value == expected_header_value
Exemple #16
0
async def test_text_file_range_request_single_part_if_range_handling(range_value, matches):
    file_path = _get_file_path('example.txt')
    info = FileInfo.from_path(file_path)

    response = get_response_for_file(Request('GET', b'/example',
                                             [(b'Range', range_value),
                                              (b'If-Range', info.etag.encode()
                                               + (b'' if matches else b'xx'))]),
                                     file_path,
                                     1200)

    expected_status = 206 if matches else 200

    assert response.status == expected_status

    if not matches:
        body = await response.read()

        with open(file_path, mode='rb') as actual_file:
            assert body == actual_file.read()
Exemple #17
0
async def test_get_response_for_file_returns_headers(file_path, method):
    response = get_response_for_file(
        FilesHandler(), Request(method, b"/example", None), file_path, 1200
    )

    assert response.status == 200

    info = FileInfo.from_path(file_path)
    expected_headers = {
        b"etag": info.etag.encode(),
        b"last-modified": str(info.modified_time).encode(),
        b"accept-ranges": b"bytes",
        b"cache-control": b"max-age=1200",
    }

    for expected_header_name, expected_header_value in expected_headers.items():
        value = response.get_single_header(expected_header_name)

        assert value is not None
        assert value == expected_header_value
Exemple #18
0
def test_get_response_for_file_raise_for_file_not_found():
    with pytest.raises(FileNotFoundError):
        get_response_for_file(Request('GET', b'/example.txt', None),
                              'example.txt',
                              1200)
Exemple #19
0
async def test_get_response_for_file_raise_for_file_not_found():
    with pytest.raises(FileNotFoundError):
        get_response_for_file(FilesHandler(),
                              Request("GET", b"/example.txt", None),
                              "example.txt", 1200)