Changeset View
Changeset View
Standalone View
Standalone View
swh/core/pytest_plugin.py
Show All 39 Lines | def get_response_cb(request, context, datadir, | ||||
Eg. if you use the requests_mock fixture in your test file as: | Eg. if you use the requests_mock fixture in your test file as: | ||||
requests_mock.get('https://nowhere.com', body=get_response_cb) | requests_mock.get('https://nowhere.com', body=get_response_cb) | ||||
# or even | # or even | ||||
requests_mock.get(re.compile('https://'), body=get_response_cb) | requests_mock.get(re.compile('https://'), body=get_response_cb) | ||||
then a call requests.get like: | then a call requests.get like: | ||||
requests.get('https://nowhere.com/path/to/resource') | requests.get('https://nowhere.com/path/to/resource?a=b&c=d') | ||||
will look the content of the response in: | will look the content of the response in: | ||||
datadir/nowhere.com/path_to_resource | datadir/nowhere.com/path_to_resource,a=b,c=d | ||||
Args: | Args: | ||||
request (requests.Request): Object requests | request (requests.Request): Object requests | ||||
context (requests.Context): Object holding response metadata | context (requests.Context): Object holding response metadata | ||||
information (status_code, headers, etc...) | information (status_code, headers, etc...) | ||||
ignore_urls: urls whose status response should be 404 even if the local | ignore_urls: urls whose status response should be 404 even if the local | ||||
file exists | file exists | ||||
visits: Dict of url, number of visits. If None, disable multi visit | visits: Dict of url, number of visits. If None, disable multi visit | ||||
Show All 11 Lines | if request.url in ignore_urls: | ||||
return None | return None | ||||
url = urlparse(request.url) | url = urlparse(request.url) | ||||
dirname = url.hostname # pypi.org | files.pythonhosted.org | dirname = url.hostname # pypi.org | files.pythonhosted.org | ||||
# url.path: pypi/<project>/json -> local file: pypi_<project>_json | # url.path: pypi/<project>/json -> local file: pypi_<project>_json | ||||
filename = url.path[1:] | filename = url.path[1:] | ||||
if filename.endswith('/'): | if filename.endswith('/'): | ||||
filename = filename[:-1] | filename = filename[:-1] | ||||
filename = filename.replace('/', '_') | filename = filename.replace('/', '_') | ||||
if url.query: | |||||
filename += ',' + url.query.replace('&', ',') | |||||
filepath = path.join(datadir, dirname, filename) | filepath = path.join(datadir, dirname, filename) | ||||
if visits is not None: | if visits is not None: | ||||
visit = visits.get(url, 0) | visit = visits.get(url, 0) | ||||
visits[url] = visit + 1 | visits[url] = visit + 1 | ||||
if visit: | if visit: | ||||
filepath = filepath + '_visit%s' % visit | filepath = filepath + '_visit%s' % visit | ||||
if not path.isfile(filepath): | if not path.isfile(filepath): | ||||
▲ Show 20 Lines • Show All 88 Lines • Show Last 20 Lines |