Changeset View
Changeset View
Standalone View
Standalone View
swh/core/pytest_plugin.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import logging | import logging | ||||
import re | import re | ||||
import pytest | import pytest | ||||
import requests | import requests | ||||
from functools import partial | from functools import partial | ||||
from os import path | from os import path | ||||
from typing import Dict, List, Optional | from typing import Dict, List, Optional | ||||
from urllib.parse import urlparse | from urllib.parse import urlparse, unquote | ||||
from requests.adapters import BaseAdapter | from requests.adapters import BaseAdapter | ||||
from requests.structures import CaseInsensitiveDict | from requests.structures import CaseInsensitiveDict | ||||
from requests.utils import get_encoding_from_headers | from requests.utils import get_encoding_from_headers | ||||
logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | def get_response_cb(request, context, datadir, | ||||
Returns: | Returns: | ||||
Optional[FileDescriptor] on disk file to read from the test context | Optional[FileDescriptor] on disk file to read from the test context | ||||
""" | """ | ||||
logger.debug('get_response_cb(%s, %s)', request, context) | logger.debug('get_response_cb(%s, %s)', request, context) | ||||
logger.debug('url: %s', request.url) | logger.debug('url: %s', request.url) | ||||
logger.debug('ignore_urls: %s', ignore_urls) | logger.debug('ignore_urls: %s', ignore_urls) | ||||
if request.url in ignore_urls: | unquoted_url = unquote(request.url) | ||||
if unquoted_url in ignore_urls: | |||||
context.status_code = 404 | context.status_code = 404 | ||||
return None | return None | ||||
url = urlparse(request.url) | url = urlparse(unquoted_url) | ||||
# http://pypi.org ~> http_pypi.org | # http://pypi.org ~> http_pypi.org | ||||
# https://files.pythonhosted.org ~> https_files.pythonhosted.org | # https://files.pythonhosted.org ~> https_files.pythonhosted.org | ||||
dirname = '%s_%s' % (url.scheme, url.hostname) | dirname = '%s_%s' % (url.scheme, url.hostname) | ||||
# 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('/', '_') | ||||
▲ Show 20 Lines • Show All 210 Lines • Show Last 20 Lines |