1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00

Move sanitize_content_filename to network.download

This commit is contained in:
Chris Hunt 2019-11-29 11:43:40 -05:00
parent 3fbc991f0c
commit 32b0fc23ab
4 changed files with 58 additions and 51 deletions

View file

@ -1,6 +1,7 @@
"""Download files with progress indicators.
"""
import logging
import os
from pip._vendor.requests.models import CONTENT_CHUNK_SIZE
@ -71,3 +72,11 @@ def _prepare_download(
return DownloadProgressProvider(
progress_bar, max=total_length
)(chunks)
def sanitize_content_filename(filename):
# type: (str) -> str
"""
Sanitize the "filename" value from a Content-Disposition header.
"""
return os.path.basename(filename)

View file

@ -28,7 +28,10 @@ from pip._internal.exceptions import (
PreviousBuildDirError,
VcsHashUnsupported,
)
from pip._internal.network.download import _prepare_download
from pip._internal.network.download import (
_prepare_download,
sanitize_content_filename,
)
from pip._internal.network.session import PipSession
from pip._internal.utils.compat import expanduser
from pip._internal.utils.filesystem import copy2_fixed
@ -305,14 +308,6 @@ def unpack_url(
)
def sanitize_content_filename(filename):
# type: (str) -> str
"""
Sanitize the "filename" value from a Content-Disposition header.
"""
return os.path.basename(filename)
def parse_content_disposition(content_disposition, default_filename):
# type: (str, str) -> str
"""

View file

@ -1,8 +1,13 @@
import pytest
import logging
import sys
import pytest
from pip._internal.models.link import Link
from pip._internal.network.download import _prepare_download
from pip._internal.network.download import (
_prepare_download,
sanitize_content_filename,
)
from tests.lib.requests_mocks import MockResponse
@ -34,3 +39,41 @@ def test_prepare_download__log(caplog, url, headers, from_cache, expected):
record = caplog.records[0]
assert record.levelname == 'INFO'
assert expected in record.message
@pytest.mark.parametrize("filename, expected", [
('dir/file', 'file'),
('../file', 'file'),
('../../file', 'file'),
('../', ''),
('../..', '..'),
('/', ''),
])
def test_sanitize_content_filename(filename, expected):
"""
Test inputs where the result is the same for Windows and non-Windows.
"""
assert sanitize_content_filename(filename) == expected
@pytest.mark.parametrize("filename, win_expected, non_win_expected", [
('dir\\file', 'file', 'dir\\file'),
('..\\file', 'file', '..\\file'),
('..\\..\\file', 'file', '..\\..\\file'),
('..\\', '', '..\\'),
('..\\..', '..', '..\\..'),
('\\', '', '\\'),
])
def test_sanitize_content_filename__platform_dependent(
filename,
win_expected,
non_win_expected
):
"""
Test inputs where the result is different for Windows and non-Windows.
"""
if sys.platform == 'win32':
expected = win_expected
else:
expected = non_win_expected
assert sanitize_content_filename(filename) == expected

View file

@ -1,7 +1,6 @@
import hashlib
import os
import shutil
import sys
from shutil import copy, rmtree
from tempfile import mkdtemp
@ -16,7 +15,6 @@ from pip._internal.operations.prepare import (
_copy_source_tree,
_download_http_url,
parse_content_disposition,
sanitize_content_filename,
unpack_file_url,
unpack_http_url,
)
@ -108,44 +106,6 @@ def test_unpack_http_url_bad_downloaded_checksum(mock_unpack_file):
rmtree(download_dir)
@pytest.mark.parametrize("filename, expected", [
('dir/file', 'file'),
('../file', 'file'),
('../../file', 'file'),
('../', ''),
('../..', '..'),
('/', ''),
])
def test_sanitize_content_filename(filename, expected):
"""
Test inputs where the result is the same for Windows and non-Windows.
"""
assert sanitize_content_filename(filename) == expected
@pytest.mark.parametrize("filename, win_expected, non_win_expected", [
('dir\\file', 'file', 'dir\\file'),
('..\\file', 'file', '..\\file'),
('..\\..\\file', 'file', '..\\..\\file'),
('..\\', '', '..\\'),
('..\\..', '..', '..\\..'),
('\\', '', '\\'),
])
def test_sanitize_content_filename__platform_dependent(
filename,
win_expected,
non_win_expected
):
"""
Test inputs where the result is different for Windows and non-Windows.
"""
if sys.platform == 'win32':
expected = win_expected
else:
expected = non_win_expected
assert sanitize_content_filename(filename) == expected
@pytest.mark.parametrize("content_disposition, default_filename, expected", [
('attachment;filename="../file"', 'df', 'file'),
])