Projet

Général

Profil

0001-misc-use-legacy-urls-to-call-up-to-date-urls-64416.patch

Emmanuel Cazenave, 21 avril 2022 14:49

Télécharger (2,55 ko)

Voir les différences:

Subject: [PATCH] misc: use legacy urls to call up to date urls (#64416)

 combo/utils/requests_wrapper.py | 15 +++++++++++++++
 tests/settings.py               |  2 ++
 tests/test_requests.py          |  7 +++++++
 3 files changed, 24 insertions(+)
combo/utils/requests_wrapper.py
44 44
        return request
45 45

  
46 46

  
47
def translate_from_legacy_urls(url):
48
    if not url:
49
        return ''
50
    legacy_urls_mapping = getattr(settings, 'LEGACY_URLS_MAPPING', None)
51
    if not legacy_urls_mapping:
52
        return url
53
    splitted_url = urllib.parse.urlparse(url)
54
    hostname = splitted_url.netloc
55
    if hostname not in legacy_urls_mapping:
56
        return url
57
    return splitted_url._replace(netloc=legacy_urls_mapping[hostname]).geturl()
58

  
59

  
47 60
class Requests(RequestsSession):
48 61
    def request(self, method, url, **kwargs):
49 62
        remote_service = kwargs.pop('remote_service', None)
......
59 72
        # don't use persistent cookies
60 73
        self.cookies.clear()
61 74

  
75
        url = translate_from_legacy_urls(url)
76

  
62 77
        if remote_service == 'auto':
63 78
            remote_service = get_known_service_for_url(url)
64 79
            if remote_service:
tests/settings.py
65 65
    },
66 66
}
67 67

  
68
LEGACY_URLS_MAPPING = {'old.org': 'new.org'}
69

  
68 70
MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage'
69 71

  
70 72
LINGO_API_SIGN_KEY = '12345'
tests/test_requests.py
150 150

  
151 151
        # check with unicode url
152 152
        assert requests.get('http://cache.example.org/éléphant').content == b'hello second world'
153

  
154

  
155
def test_requests_to_legacy_urls():
156
    with mock.patch('combo.utils.requests_wrapper.RequestsSession.request') as requests_get:
157
        requests_get.return_value = mock.Mock(content=b'hello world', status_code=200)
158
        assert requests.get('http://old.org/').content == b'hello world'
159
        assert requests_get.call_args.args[1] == 'http://new.org/'
153
-