Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use URL from response to support redirects #3198

Merged
merged 10 commits into from
Jan 5, 2021
12 changes: 10 additions & 2 deletions poetry/repositories/legacy_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,16 @@ 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
)
jpyams marked this conversation as resolved.
Show resolved Hide resolved
)

return Page(response.url, response.content, response.headers)
jpyams marked this conversation as resolved.
Show resolved Hide resolved
jpyams marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 15 additions & 0 deletions tests/repositories/test_legacy_repository.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import shutil

import pytest
import requests

from poetry.core.packages import Dependency
from poetry.factory import Factory
Expand Down Expand Up @@ -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):
jpyams marked this conversation as resolved.
Show resolved Hide resolved
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/"