6
6
7
7
from pip ._internal .download import PipSession
8
8
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 ,
11
10
)
12
11
13
12
@@ -36,8 +35,8 @@ def test_get_html_response_archive_to_naive_scheme(url):
36
35
)
37
36
def test_get_html_response_archive_to_http_scheme (url , content_type ):
38
37
"""
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 .
41
40
"""
42
41
session = mock .Mock (PipSession )
43
42
session .head .return_value = mock .Mock (** {
@@ -54,6 +53,71 @@ def test_get_html_response_archive_to_http_scheme(url, content_type):
54
53
assert ctx .value .args == (content_type , "HEAD" )
55
54
56
55
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
+
57
121
@pytest .mark .parametrize (
58
122
"url, vcs_scheme" ,
59
123
[
0 commit comments