Пример #1
0
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()
Пример #2
0
def test_http_file_forbidden(mock_responses):
    url = get_data_url('does_not_exist')
    mock_responses.add(responses.HEAD, url, status=403)
    with requests.Session() as session:
        with pytest.raises(PermissionError) as exc_info:
            fetch_requests.HttpFile(session, url)
    assert exc_info.value.filename == url
Пример #3
0
def test_http_file_ranges_not_accepted(mock_responses):
    url = get_data_url('rfi_mask_ranges.h5')
    with requests.Session() as session:
        with pytest.raises(
                OSError,
                match='Server does not accept byte ranges') as exc_info:
            fetch_requests.HttpFile(session, url)
    assert exc_info.value.filename == url
Пример #4
0
def test_http_file_url_attr(mock_responses, web_server):
    url = get_data_url('subdir/redirect.h5')
    new_url = web_server('rfi_mask_ranges.h5')
    mock_responses.add(responses.HEAD,
                       url,
                       headers={'Location': new_url},
                       status=307)
    mock_responses.add_passthru(web_server(''))
    with requests.Session() as session:
        with fetch_requests.HttpFile(session, url) as file:
            assert file.url == new_url
Пример #5
0
def test_http_file_no_content_length(mock_responses):
    url = get_data_url('rfi_mask_ranges.h5')
    mock_responses.replace(responses.HEAD,
                           url,
                           content_type='application/x-hdf5',
                           headers={'Accept-Ranges': 'bytes'})
    with requests.Session() as session:
        with pytest.raises(OSError,
                           match='Server did not provide Content-Length header'
                           ) as exc_info:
            fetch_requests.HttpFile(session, url)
    assert exc_info.value.filename == url
Пример #6
0
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
Пример #7
0
def test_http_file_not_found(web_server):
    with requests.Session() as session:
        with pytest.raises(FileNotFoundError) as exc_info:
            fetch_requests.HttpFile(session, web_server('does_not_exist'))
    assert exc_info.value.filename == web_server('does_not_exist')
Пример #8
0
def http_file(web_server):
    with requests.Session() as session:
        with fetch_requests.HttpFile(session,
                                     web_server('all_bytes.bin')) as file:
            yield file