misc: use legacy urls to call up to date urls (#65024)
gitea-wip/combo/pipeline/head There was a failure building this commit Details
gitea/combo/pipeline/head Something is wrong with the build of this commit Details

This commit is contained in:
Emmanuel Cazenave 2022-05-09 17:13:29 +02:00
parent a97c740dac
commit 0850accd99
3 changed files with 17 additions and 0 deletions

View File

@ -59,6 +59,14 @@ class Requests(RequestsSession):
# don't use persistent cookies
self.cookies.clear()
# search in legacy urls
legacy_urls_mapping = getattr(settings, 'LEGACY_URLS_MAPPING', None)
if legacy_urls_mapping:
splitted_url = urllib.parse.urlparse(url)
hostname = splitted_url.netloc
if hostname in legacy_urls_mapping:
url = splitted_url._replace(netloc=legacy_urls_mapping[hostname]).geturl()
if remote_service == 'auto':
remote_service = get_known_service_for_url(url)
if remote_service:

View File

@ -107,3 +107,5 @@ USER_PROFILE_CONFIG = {
},
]
}
LEGACY_URLS_MAPPING = {'old.org': 'new.org'}

View File

@ -150,3 +150,10 @@ def test_requests_cache():
# check with unicode url
assert requests.get('http://cache.example.org/éléphant').content == b'hello second world'
def test_requests_to_legacy_urls():
with mock.patch('combo.utils.requests_wrapper.RequestsSession.request') as requests_get:
requests_get.return_value = mock.Mock(content=b'hello world', status_code=200)
assert requests.get('http://old.org/?one=1&two=2#anchor').content == b'hello world'
assert requests_get.call_args.args[1] == 'http://new.org/?one=1&two=2#anchor'