0001-misc-use-legacy-urls-to-call-up-to-date-urls-65025.patch
passerelle/utils/__init__.py | ||
---|---|---|
19 | 19 |
import hashlib |
20 | 20 |
import re |
21 | 21 |
import time |
22 |
import urllib.parse |
|
22 | 23 |
import warnings |
23 | 24 |
from functools import wraps |
24 | 25 |
from io import BytesIO |
... | ... | |
283 | 284 |
cache_duration = kwargs.pop('cache_duration', None) |
284 | 285 |
invalidate_cache = kwargs.pop('invalidate_cache', False) |
285 | 286 | |
287 |
# search in legacy urls |
|
288 |
legacy_urls_mapping = getattr(settings, 'LEGACY_URLS_MAPPING', None) |
|
289 |
if legacy_urls_mapping: |
|
290 |
splitted_url = urllib.parse.urlparse(url) |
|
291 |
hostname = splitted_url.netloc |
|
292 |
if hostname in legacy_urls_mapping: |
|
293 |
url = splitted_url._replace(netloc=legacy_urls_mapping[hostname]).geturl() |
|
294 | ||
286 | 295 |
if self.resource: |
287 | 296 |
if 'auth' not in kwargs: |
288 | 297 |
username = getattr(self.resource, 'basic_auth_username', None) |
tests/settings.py | ||
---|---|---|
71 | 71 |
} |
72 | 72 | |
73 | 73 |
LOGGED_REQUESTS_MAX_SIZE = 4999 |
74 | ||
75 |
LEGACY_URLS_MAPPING = {'old.org': 'new.org'} |
tests/test_requests.py | ||
---|---|---|
4 | 4 |
import mohawk |
5 | 5 |
import pytest |
6 | 6 |
import requests |
7 |
import responses |
|
7 | 8 |
from django.test import override_settings |
8 | 9 |
from httmock import HTTMock, response, urlmatch |
9 | 10 |
from urllib3.exceptions import ReadTimeoutError |
... | ... | |
514 | 515 |
with pytest.raises(requests.ConnectionError): |
515 | 516 |
session.get('http://example.net/whatever') |
516 | 517 |
assert mocked_make_request.call_count == 4 |
518 | ||
519 | ||
520 |
@responses.activate |
|
521 |
def test_requests_to_legacy_urls(log_level): |
|
522 |
responses.add( |
|
523 |
responses.GET, |
|
524 |
"https://new.org/foobar", |
|
525 |
json={"foo": "bar"}, |
|
526 |
status=200, |
|
527 |
) |
|
528 |
logger = logging.getLogger('requests') |
|
529 |
logger.setLevel(log_level) |
|
530 |
requests = Request(logger=logger) |
|
531 |
resp = requests.get('https://old.org/foobar') |
|
532 |
assert resp.json() == {"foo": "bar"} |
|
533 |
assert resp.request.url == 'https://new.org/foobar' |
|
517 |
- |