From 63a952e92dc0b801cf06be97e4cd28c509e4aae0 Mon Sep 17 00:00:00 2001 From: John Peter Yamauchi Date: Tue, 5 Jan 2021 17:55:26 -0600 Subject: [PATCH] legacy repository: support redirected url --- poetry/repositories/legacy_repository.py | 13 +++++++++++-- tests/repositories/test_legacy_repository.py | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/poetry/repositories/legacy_repository.py b/poetry/repositories/legacy_repository.py index 7442d65a561..75d0a83ff75 100755 --- a/poetry/repositories/legacy_repository.py +++ b/poetry/repositories/legacy_repository.py @@ -394,8 +394,17 @@ def _get(self, endpoint): # type: (str) -> Union[Page, None] if response.status_code in (401, 403): self._log( - "Authorization error accessing {url}".format(url=url), level="warn" + "Authorization error accessing {url}".format(url=response.url), + level="warn", ) return - return Page(url, response.content, response.headers) + if response.url != url: + self._log( + "Response URL {response_url} differs from request URL {url}".format( + response_url=response.url, url=url + ), + level="debug", + ) + + return Page(response.url, response.content, response.headers) diff --git a/tests/repositories/test_legacy_repository.py b/tests/repositories/test_legacy_repository.py index 47ccc1052f9..5b29735aecb 100644 --- a/tests/repositories/test_legacy_repository.py +++ b/tests/repositories/test_legacy_repository.py @@ -1,6 +1,7 @@ import shutil import pytest +import requests from poetry.core.packages import Dependency from poetry.factory import Factory @@ -332,3 +333,17 @@ def test_get_4xx_and_5xx_raises(http): for endpoint in endpoints: with pytest.raises(RepositoryError): repo._get(endpoint) + + +def test_get_redirected_response_url(http, monkeypatch): + repo = MockHttpRepository({"/foo": 200}, http) + redirect_url = "http://legacy.redirect.bar" + + def get_mock(url): + response = requests.Response() + response.status_code = 200 + response.url = redirect_url + "/foo" + return response + + monkeypatch.setattr(repo.session, "get", get_mock) + assert repo._get("/foo")._url == "http://legacy.redirect.bar/foo/"