blob: 7aa613574f916ca3d956470c9c3fdde1300d5d4f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
"""Example of using requests-cache with the requests-mock library"""
import pytest
from requests_mock import Adapter
from requests_cache import CachedSession
URL = 'https://some_test_url'
@pytest.fixture(scope='function')
def mock_session():
"""Fixture that provides a CachedSession that will make mock requests where it would normally
make real requests"""
adapter = Adapter()
adapter.register_uri(
'GET',
URL,
headers={'Content-Type': 'text/plain'},
text='Mock response!',
status_code=200,
)
session = CachedSession(backend='memory')
session.mount('https://', adapter)
yield session
def test_mock_session(mock_session):
"""Test that the mock_session fixture is working as expected"""
response_1 = mock_session.get(URL)
assert response_1.text == 'Mock response!'
assert getattr(response_1, 'from_cache', False) is False
response_2 = mock_session.get(URL)
assert response_2.text == 'Mock response!'
assert response_2.from_cache is True
|