Skip to content

Commit fc53f71

Browse files
committed
More tests on successful scenarios, fix docstrings
1 parent 200f126 commit fc53f71

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

src/pip/_internal/index.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def _match_vcs_scheme(url):
7272

7373

7474
def _is_url_like_archive(url):
75-
"""Check whether the URL looks like an archive.
75+
"""Return whether the URL looks like an archive.
7676
"""
7777
filename = Link(url).filename
7878
for bad_ext in ARCHIVE_EXTENSIONS:
@@ -128,8 +128,8 @@ def _get_html_response(url, session):
128128
Raise `_NotHTTP` if the content type cannot be determined, or
129129
`_NotHTML` if it is not HTML.
130130
2. Actually perform the request. Raise HTTP exceptions on network failures.
131-
3. Check whether Content-Type header to make sure the thing we got is HTML,
132-
and raise `_NotHTML` if it's not.
131+
3. Check the Content-Type header to make sure we got HTML, and raise
132+
`_NotHTML` otherwise.
133133
"""
134134
if _is_url_like_archive(url):
135135
_ensure_html_response(url, session=session)

tests/unit/test_index_html_page.py

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
from pip._internal.download import PipSession
88
from pip._internal.index import (
9-
Link, PackageFinder, _determine_base_url, _get_html_page,
10-
_get_html_response, _NotHTML, _NotHTTP, egg_info_matches,
9+
Link, _get_html_page, _get_html_response, _NotHTML, _NotHTTP,
1110
)
1211

1312

@@ -36,8 +35,8 @@ def test_get_html_response_archive_to_naive_scheme(url):
3635
)
3736
def test_get_html_response_archive_to_http_scheme(url, content_type):
3837
"""
39-
`_get_html_response()` should send a HEAD request on and archive-like URL
40-
if the scheme supports it.
38+
`_get_html_response()` should send a HEAD request on an archive-like URL
39+
if the scheme supports it, and raise `_NotHTML` if the response isn't HTML.
4140
"""
4241
session = mock.Mock(PipSession)
4342
session.head.return_value = mock.Mock(**{
@@ -54,6 +53,71 @@ def test_get_html_response_archive_to_http_scheme(url, content_type):
5453
assert ctx.value.args == (content_type, "HEAD")
5554

5655

56+
@pytest.mark.parametrize(
57+
"url",
58+
[
59+
"http://python.org/python-3.7.1.zip",
60+
"https://pypi.org/pip-18.0.tar.gz",
61+
],
62+
)
63+
def test_get_html_response_archive_to_http_scheme_is_html(url):
64+
"""
65+
`_get_html_response()` should work with archive-like URLs if the HEAD
66+
request is responded with text/html.
67+
"""
68+
session = mock.Mock(PipSession)
69+
session.head.return_value = mock.Mock(**{
70+
"request.method": "HEAD",
71+
"headers": {"Content-Type": "text/html"},
72+
})
73+
session.get.return_value = mock.Mock(headers={"Content-Type": "text/html"})
74+
75+
resp = _get_html_response(url, session=session)
76+
77+
assert resp is not None
78+
assert session.mock_calls == [
79+
mock.call.head(url, allow_redirects=True),
80+
mock.call.head().raise_for_status(),
81+
mock.call.get(url, headers={
82+
"Accept": "text/html", "Cache-Control": "max-age=0",
83+
}),
84+
mock.call.get().raise_for_status(),
85+
]
86+
87+
88+
@pytest.mark.parametrize(
89+
"url",
90+
[
91+
"https://pypi.org/simple/pip",
92+
"https://pypi.org/simple/pip/",
93+
"https://python.org/sitemap.xml",
94+
],
95+
)
96+
def test_get_html_response_no_head(url):
97+
"""
98+
`_get_html_response()` shouldn't send a HEAD request if the URL does not
99+
look like an archive, only the GET request that retrieves data.
100+
"""
101+
session = mock.Mock(PipSession)
102+
103+
# Mock the headers dict to ensure it is accessed.
104+
session.get.return_value = mock.Mock(headers=mock.Mock(**{
105+
"get.return_value": "text/html",
106+
}))
107+
108+
resp = _get_html_response(url, session=session)
109+
110+
assert resp is not None
111+
assert session.head.call_count == 0
112+
assert session.get.mock_calls == [
113+
mock.call(url, headers={
114+
"Accept": "text/html", "Cache-Control": "max-age=0",
115+
}),
116+
mock.call().raise_for_status(),
117+
mock.call().headers.get("Content-Type", ""),
118+
]
119+
120+
57121
@pytest.mark.parametrize(
58122
"url, vcs_scheme",
59123
[

0 commit comments

Comments
 (0)