From 3d7b75cc13adc99f759d2b58920702972251f22b Mon Sep 17 00:00:00 2001 From: codeskyblue Date: Thu, 21 Dec 2023 01:42:30 +0800 Subject: [PATCH] fix requests auto decode file (#8701) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Randy Döring <30527984+radoering@users.noreply.github.com> --- src/poetry/utils/helpers.py | 5 ++++- tests/utils/test_helpers.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/poetry/utils/helpers.py b/src/poetry/utils/helpers.py index 5c312a6b6a9..e2ee03b467b 100644 --- a/src/poetry/utils/helpers.py +++ b/src/poetry/utils/helpers.py @@ -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 diff --git a/tests/utils/test_helpers.py b/tests/utils/test_helpers.py index bf69bea7f44..84e1520a39b 100644 --- a/tests/utils/test_helpers.py +++ b/tests/utils/test_helpers.py @@ -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 @@ -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"