1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
import logging
import sys
from typing import Dict
import pytest
from pip._internal.models.link import Link
from pip._internal.network.download import (
_prepare_download,
parse_content_disposition,
sanitize_content_filename,
)
from tests.lib.requests_mocks import MockResponse
@pytest.mark.parametrize(
"url, headers, from_cache, expected",
[
(
"http://example.com/foo.tgz",
{},
False,
"Downloading http://example.com/foo.tgz",
),
(
"http://example.com/foo.tgz",
{"content-length": "2"},
False,
"Downloading http://example.com/foo.tgz (2 bytes)",
),
(
"http://example.com/foo.tgz",
{"content-length": "2"},
True,
"Using cached http://example.com/foo.tgz (2 bytes)",
),
("https://files.pythonhosted.org/foo.tgz", {}, False, "Downloading foo.tgz"),
(
"https://files.pythonhosted.org/foo.tgz",
{"content-length": "2"},
False,
"Downloading foo.tgz (2 bytes)",
),
(
"https://files.pythonhosted.org/foo.tgz",
{"content-length": "2"},
True,
"Using cached foo.tgz",
),
],
)
def test_prepare_download__log(
caplog: pytest.LogCaptureFixture,
url: str,
headers: Dict[str, str],
from_cache: bool,
expected: str,
) -> None:
caplog.set_level(logging.INFO)
resp = MockResponse(b"")
resp.url = url
resp.headers = headers
if from_cache:
resp.from_cache = from_cache
link = Link(url)
_prepare_download(resp, link, progress_bar="on")
assert len(caplog.records) == 1
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: str, expected: str) -> None:
"""
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: str, win_expected: str, non_win_expected: str
) -> None:
"""
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"),
],
)
def test_parse_content_disposition(
content_disposition: str, default_filename: str, expected: str
) -> None:
actual = parse_content_disposition(content_disposition, default_filename)
assert actual == expected
|