def test_http_file_content_encoding_get(mock_responses): url = get_data_url('rfi_mask_ranges.h5') mock_responses.replace(responses.HEAD, url, headers={ 'Content-Length': str(len(get_data('rfi_mask_ranges.h5'))), 'Accept-Ranges': 'bytes' }) mock_responses.replace(responses.GET, url, headers={ 'Content-Length': str(len(get_data('rfi_mask_ranges.h5'))), 'Accept-Ranges': 'bytes', 'Content-Encoding': 'gzip' }) with requests.Session() as session: with fetch_requests.HttpFile(session, url) as file: with pytest.raises( OSError, match='Server provided Content-Encoding header'): file.read()
async def test_fetch_model_checksum_ok(mock_aioresponses) -> None: data = get_data('rfi_mask_ranges.h5') digest = hashlib.sha256(data).hexdigest() url = get_data_url(f'sha256_{digest}.h5') mock_aioresponses.get(url, content_type='application/x-hdf5', body=data) with await fetch_aiohttp.fetch_model(url, DummyModel) as model: assert model.checksum == digest
async def test_fetch_model_checksum_bad(mock_aioresponses) -> None: data = get_data('rfi_mask_ranges.h5') digest = hashlib.sha256(data).hexdigest() url = get_data_url(f'sha256_{digest}.h5') # Now invalidate the digest data += b'blahblahblah' mock_aioresponses.get(url, content_type='application/x-hdf5', body=data) with pytest.raises(models.ChecksumError) as exc_info: await fetch_aiohttp.fetch_model(url, DummyModel) assert exc_info.value.url == url
async def test_fetch_model_bad_content_type(mock_aioresponses) -> None: data = get_data('rfi_mask_ranges.h5') url = get_data_url('bad_content_type.h5') mock_aioresponses.get(url, content_type='image/png', body=data) with pytest.raises( models.FileTypeError, match='Expected application/x-hdf5, not image/png') as exc_info: async with fetch_aiohttp.Fetcher() as fetcher: await fetcher.get(url, DummyModel) assert exc_info.value.url == url assert exc_info.value.original_url == url
def test_fetch_model_bad_content_type(mock_responses, lazy) -> None: data = get_data('rfi_mask_ranges.h5') url = get_data_url('bad_content_type.h5') mock_responses.add(responses.HEAD, url, content_type='image/png', headers={ 'Accept-Ranges': 'bytes', 'Content-Length': str(len(data)) }) mock_responses.add(responses.GET, url, content_type='image/png', body=data) with pytest.raises( models.FileTypeError, match='Expected application/x-hdf5, not image/png') as exc_info: with fetch_requests.Fetcher() as fetcher: fetcher.get(url, DummyModel, lazy=lazy) assert exc_info.value.url == url assert exc_info.value.original_url == url
def test_http_file_range_ignored(mock_responses): url = get_data_url('rfi_mask_ranges.h5') data = get_data('rfi_mask_ranges.h5') mock_responses.replace(responses.HEAD, url, headers={ 'Accept-Ranges': 'bytes', 'Content-Length': str(len(data)) }) mock_responses.replace(responses.GET, url, body=data, stream=True) with requests.Session() as session: with fetch_requests.HttpFile(session, url) as file: with pytest.raises(OSError, match='Did not receive expected byte range'): file.read(10) # Reading the whole file should work even if the server doesn't send # back partial content. file.seek(0) test_data = file.read(len(data)) assert test_data == data