예제 #1
0
def test_raw_response__reset(mock_session):
    response = CachedResponse(mock_session.get(MOCKED_URL))
    response.raw.read(None)
    assert response.raw.read(1) == b''
    assert response.raw._fp.closed is True

    response.reset()
    assert response.raw.read(None) == b'mock response'
예제 #2
0
def convert_old_response(cached_response, timestamp):
    temp_response = Response()
    for field in Response.__attrs__:
        setattr(temp_response, field, getattr(cached_response, field, None))

    new_response = CachedResponse(temp_response)
    new_response.created_at = timestamp
    return new_response
예제 #3
0
def test_format_response():
    response = CachedResponse(status_code=200, expires=datetime(2021, 1, 1), headers={'Age': '0'})
    response_str = format_response(response)

    assert 'cached; expires in ' in response_str
    assert 'Age: 0' in response_str
    response.expires = None
    assert 'never expires' in format_response(response)
예제 #4
0
def test_revalidate__shorten_expiration(mock_session):
    # Start with a non-expired response
    response = CachedResponse(
        mock_session.get(MOCKED_URL),
        expire_after=datetime.utcnow() + timedelta(seconds=1),
    )
    assert response.is_expired is False

    # Set expiration in the past and revalidate
    is_expired = response.revalidate(datetime.utcnow() - timedelta(seconds=0.05))
    assert is_expired is response.is_expired is True
예제 #5
0
def test_revalidate__extend_expiration(mock_session):
    # Start with an expired response
    response = CachedResponse(
        mock_session.get(MOCKED_URL),
        expire_after=datetime.utcnow() - timedelta(seconds=0.05),
    )
    assert response.is_expired is True

    # Set expiration in the future and revalidate
    is_expired = response.revalidate(datetime.utcnow() + timedelta(seconds=0.05))
    assert is_expired is response.is_expired is False
    sleep(0.1)
    assert response.is_expired is True
예제 #6
0
def test_raw_response__stream(mock_session):
    response = CachedResponse(mock_session.get(MOCKED_URL))
    data = b''
    for chunk in response.raw.stream(1):
        data += chunk
    assert data == b'mock response'
    assert response.raw._fp.closed
예제 #7
0
def test_raw_response__read(mock_session):
    response = CachedResponse(mock_session.get(MOCKED_URL))
    assert isinstance(response.raw, CachedHTTPResponse)
    assert response.raw.read(10) == b'mock respo'
    assert response.raw.read(None) == b'nse'
    assert response.raw.read(1) == b''
    assert response.raw._fp.closed is True
예제 #8
0
def test_response_decode(tempfile_session):
    """Test that a gzip-compressed raw response can be manually uncompressed with decode_content"""
    response = tempfile_session.get(httpbin('gzip'))
    assert b'gzipped' in response.content

    cached = CachedResponse(response)
    assert b'gzipped' in cached.content
    assert b'gzipped' in cached.raw.read(None, decode_content=True)
예제 #9
0
def test_basic_attrs(mock_session):
    response = CachedResponse(mock_session.get(MOCKED_URL))

    assert response.from_cache is True
    assert response.url == MOCKED_URL
    assert response.status_code == 200
    assert response.reason is None
    assert response.encoding == 'ISO-8859-1'
    assert response.headers['Content-Type'] == 'text/plain'
    assert response.text == 'mock response'
    assert response.created_at is not None
    assert response.expires is None
    assert response.is_expired is False
예제 #10
0
def test_raw_response__decode(mock_session):
    """Test that a gzip-compressed raw response can be manually uncompressed with decode_content"""
    url = f'{MOCKED_URL}/utf-8'
    mock_session.mock_adapter.register_uri(
        'GET',
        url,
        status_code=200,
        body=BytesIO(gzip.compress(b'compressed response')),
        headers={'content-encoding': 'gzip'},
    )
    response = CachedResponse(mock_session.get(url))
    # Requests will have already read this, but let's just pretend we want to do it manually
    response.raw._fp = BytesIO(gzip.compress(b'compressed response'))
    assert response.raw.read(None, decode_content=True) == b'compressed response'
예제 #11
0
def test_raw_response__close(mock_session):
    response = CachedResponse(mock_session.get(MOCKED_URL))
    response.close()
    assert response.raw._fp.closed is True
예제 #12
0
def test_history(mock_session):
    original_response = mock_session.get(MOCKED_URL)
    original_response.history = [mock_session.get(MOCKED_URL)] * 3
    response = CachedResponse(original_response)
    assert len(response.history) == 3
    assert all([isinstance(r, CachedResponse) for r in response.history])
예제 #13
0
def test_expiration(expire_after, is_expired, mock_session):
    response = CachedResponse(mock_session.get(MOCKED_URL), expire_after)
    assert response.from_cache is True
    assert response.is_expired == is_expired