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

fix requests auto decode file #8701

Merged
5 changes: 4 additions & 1 deletion src/poetry/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,11 @@ def __init__(
self._dest = dest

get = requests.get if not session else session.get
headers = {"Accept-Encoding": "Identity"}

self._response = get(url, stream=True, timeout=REQUESTS_TIMEOUT)
self._response = get(
url, stream=True, headers=headers, timeout=REQUESTS_TIMEOUT
)
self._response.raise_for_status()

@cached_property
Expand Down
20 changes: 20 additions & 0 deletions tests/utils/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@

from poetry.core.utils.helpers import parse_requires

from poetry.utils.helpers import download_file
from poetry.utils.helpers import get_file_hash


if TYPE_CHECKING:
from pathlib import Path

from httpretty import httpretty

from tests.types import FixtureDirGetter


Expand Down Expand Up @@ -119,3 +124,18 @@ def test_guaranteed_hash(
) -> None:
file_path = fixture_dir("distributions") / "demo-0.1.0.tar.gz"
assert get_file_hash(file_path, hash_name) == expected


def test_download_file(
http: type[httpretty], fixture_dir: FixtureDirGetter, tmp_path: Path
) -> None:
file_path = fixture_dir("distributions") / "demo-0.1.0.tar.gz"
url = "https://foo.com/demo-0.1.0.tar.gz"
http.register_uri(http.GET, url, body=file_path.read_bytes())
dest = tmp_path / "demo-0.1.0.tar.gz"

download_file(url, dest)

expect_sha_256 = "9fa123ad707a5c6c944743bf3e11a0e80d86cb518d3cf25320866ca3ef43e2ad"
assert get_file_hash(dest) == expect_sha_256
assert http.last_request().headers["Accept-Encoding"] == "Identity"