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()
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
def test_file_info(): info = FileInfo.from_path(files2_index_path) assert info.mime == "text/html" data = info.to_dict() assert data["size"] == info.size assert data["etag"] == info.etag assert data["mime"] == info.mime assert data["modified_time"] == info.modified_time assert info.mime in repr(info)
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
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()
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