Exemple #1
0
def test_Result_json():
    r = http.Result('{"this":"that"}', b'{"this":"that"}')
    assert r.json() == {'this': 'that'}
    r = http.Result('{"this":"that"', b'{"this":"that"')
    assert r == '{"this":"that"'
    with pytest.raises(errors.RequestError,
                       match=r'^Malformed JSON: {"this":"that": '):
        r.json()
Exemple #2
0
def test_Result_repr(kwargs, exp_kwargs):
    r = http.Result(**kwargs)
    exp_kwargs_string = ', '.join(f'{k}={v!r}' for k, v in exp_kwargs.items())
    assert repr(r) == f'Result({exp_kwargs_string})'
Exemple #3
0
def test_Result_status_code():
    r = http.Result('foo', b'foo')
    assert r.status_code is None
    r = http.Result('foo', b'foo', status_code=304)
    assert r.status_code == 304
Exemple #4
0
def test_Result_headers():
    r = http.Result('foo', b'foo')
    assert r.headers == {}
    r = http.Result('foo', b'foo', headers={'a': '1', 'b': '2'})
    assert r.headers == {'a': '1', 'b': '2'}
Exemple #5
0
def test_Result_bytes():
    r = http.Result('föö', bytes('föö', 'utf-8'))
    assert r.bytes == bytes('föö', 'utf-8')
    assert str(r.bytes, 'utf-8') == 'föö'
Exemple #6
0
def test_Result_is_string():
    r = http.Result('foo', b'foo')
    assert isinstance(r, str)
    assert r == 'foo'
Exemple #7
0

def test_from_cache_with_unreadable_cache_file(tmp_path):
    cache_filepath = tmp_path / 'cache_file'
    cache_filepath.write_text('mock data')
    cache_filepath.chmod(0o000)
    try:
        assert http._from_cache(cache_filepath) is None
    finally:
        cache_filepath.chmod(0o600)


@pytest.mark.parametrize(
    argnames='cached_data, max_age, cache_file_age, exp_return_value',
    argvalues=(
        ('föö', 100, 99, http.Result('föö', b'f\xc3\xb6\xc3\xb6')),
        ('föö', 100, 101, None),
    ),
)
def test_from_cache_refuses_to_read_old_cache_file(cached_data, max_age,
                                                   cache_file_age,
                                                   exp_return_value, tmp_path):
    cache_filepath = tmp_path / 'cache/file'
    cache_filepath.parent.mkdir(parents=True)
    cache_filepath.write_text(cached_data, encoding='UTF-8')
    mtime = time.time() - cache_file_age
    os.utime(cache_filepath, times=(mtime, mtime))
    cached_result = http._from_cache(cache_filepath, max_age=max_age)
    assert cached_result == exp_return_value