Skip to content
Snippets Groups Projects
Verified Commit a5ac8f4a authored by Antoine R. Dumont's avatar Antoine R. Dumont
Browse files

conftest: Add better support for multiple visits

parent 50fbc844
No related branches found
No related tags found
No related merge requests found
......@@ -30,7 +30,7 @@ def swh_config(monkeypatch):
return conffile
def get_response_cb(request, context, ignore_urls=[], visit=None):
def get_response_cb(request, context, ignore_urls=[], visits=None):
"""Mount point callback to fetch on disk the content of a request
This is meant to be used as 'body' argument of the requests_mock.get()
......@@ -61,10 +61,11 @@ def get_response_cb(request, context, ignore_urls=[], visit=None):
Args:
request (requests.Request): Object requests
context (requests.Context): Object holding response metadata
information (status_code, headers, etc...)
information (status_code, headers, etc...)
ignore_urls (List): urls whose status response should be 404 even if
the local file exists
visit (Optional[int]): Visit number for the given url (can be None)
the local file exists
visits (Optional[Dict]): Map of url, number of visits. If None, disable
multi visit support (default)
Returns:
Optional[FileDescriptor] on the on disk file to read from the test
......@@ -85,9 +86,14 @@ def get_response_cb(request, context, ignore_urls=[], visit=None):
filename = filename[:-1]
filename = filename.replace('/', '_')
filepath = path.join(DATADIR, dirname, filename)
if visit:
filepath = filepath + '_visit%s' % visit
if visits is not None:
visit = visits.get(url, 0)
visits[url] = visit + 1
if visit:
filepath = filepath + '_visit%s' % visit
if not path.isfile(filepath):
logger.debug('not found filepath: %s', filepath)
context.status_code = 404
return None
fd = open(filepath, 'rb')
......@@ -104,13 +110,9 @@ def local_get_factory(ignore_urls=[],
ignore_urls=ignore_urls)
requests_mock.get(re.compile('https://'), body=cb)
else:
requests_mock.get(re.compile('https'), [
{
'body': partial(
get_response_cb,
ignore_urls=ignore_urls,
visit=i)
} for i in range(MAX_VISIT_FILES)]
visits = {}
requests_mock.get(re.compile('https'), body=partial(
get_response_cb, ignore_urls=ignore_urls, visits=visits)
)
return requests_mock
......
"foobar"
......@@ -9,15 +9,37 @@ import requests
from swh.loader.package.tests.conftest import local_get_factory
def test_get_response_cb_with_visits_nominal(local_get_visits):
response = requests.get('https://example.com/file.json')
assert response.ok
assert response.json() == {'hello': 'you'}
response = requests.get('https://example.com/file.json')
assert response.ok
assert response.json() == {'hello': 'world'}
response = requests.get('https://example.com/file.json')
assert not response.ok
assert response.status_code == 404
def test_get_response_cb_with_visits(local_get_visits):
response = requests.get('https://example.com/file.json')
assert response.ok
assert response.json() == {'hello': 'you'}
response = requests.get('https://example.com/other.json')
assert response.ok
assert response.json() == "foobar"
response = requests.get('https://example.com/file.json')
assert response.ok
assert response.json() == {'hello': 'world'}
response = requests.get('https://example.com/other.json')
assert not response.ok
assert response.status_code == 404
response = requests.get('https://example.com/file.json')
assert not response.ok
assert response.status_code == 404
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment