@@ -60,6 +60,7 @@ def test_get_html_response_archive_to_http_scheme(url, content_type):
60
60
if the scheme supports it, and raise `_NotHTML` if the response isn't HTML.
61
61
"""
62
62
session = mock .Mock (PipSession )
63
+ session .headers = {}
63
64
session .head .return_value = mock .Mock (** {
64
65
"request.method" : "HEAD" ,
65
66
"headers" : {"Content-Type" : content_type },
@@ -87,6 +88,7 @@ def test_get_html_response_archive_to_http_scheme_is_html(url):
87
88
request is responded with text/html.
88
89
"""
89
90
session = mock .Mock (PipSession )
91
+ session .headers = {}
90
92
session .head .return_value = mock .Mock (** {
91
93
"request.method" : "HEAD" ,
92
94
"headers" : {"Content-Type" : "text/html" },
@@ -120,6 +122,7 @@ def test_get_html_response_no_head(url):
120
122
look like an archive, only the GET request that retrieves data.
121
123
"""
122
124
session = mock .Mock (PipSession )
125
+ session .headers = {}
123
126
124
127
# Mock the headers dict to ensure it is accessed.
125
128
session .get .return_value = mock .Mock (headers = mock .Mock (** {
@@ -145,6 +148,7 @@ def test_get_html_response_dont_log_clear_text_password(caplog):
145
148
in its DEBUG log message.
146
149
"""
147
150
session = mock .Mock (PipSession )
151
+ session .headers = {}
148
152
149
153
# Mock the headers dict to ensure it is accessed.
150
154
session .get .return_value = mock .Mock (headers = mock .Mock (** {
@@ -167,6 +171,57 @@ def test_get_html_response_dont_log_clear_text_password(caplog):
167
171
]
168
172
169
173
174
+ @pytest .mark .parametrize (
175
+ ("url" , "headers" , "cache_control" ),
176
+ [
177
+ (
178
+ "http://python.org/python-3.7.1.zip" ,
179
+ {},
180
+ "max-age=0" ,
181
+ ),
182
+ (
183
+ "https://pypi.org/pip-18.0.tar.gz" ,
184
+ {},
185
+ "max-age=0" ,
186
+ ),
187
+ (
188
+ "http://python.org/python-3.7.1.zip" ,
189
+ {"Cache-Control" : "max-age=1" },
190
+ "max-age=1" ,
191
+ ),
192
+ (
193
+ "https://pypi.org/pip-18.0.tar.gz" ,
194
+ {"Cache-Control" : "max-age=1" },
195
+ "max-age=1" ,
196
+ ),
197
+ ],
198
+ )
199
+ def test_get_html_response_override_cache_control (url , headers , cache_control ):
200
+ """
201
+ `_get_html_response()` should use the session's default value for the
202
+ Cache-Control header if provided.
203
+ """
204
+ session = mock .Mock (PipSession )
205
+ session .headers = headers
206
+ session .head .return_value = mock .Mock (** {
207
+ "request.method" : "HEAD" ,
208
+ "headers" : {"Content-Type" : "text/html" },
209
+ })
210
+ session .get .return_value = mock .Mock (headers = {"Content-Type" : "text/html" })
211
+
212
+ resp = _get_html_response (url , session = session )
213
+
214
+ assert resp is not None
215
+ assert session .mock_calls == [
216
+ mock .call .head (url , allow_redirects = True ),
217
+ mock .call .head ().raise_for_status (),
218
+ mock .call .get (url , headers = {
219
+ "Accept" : "text/html" , "Cache-Control" : cache_control ,
220
+ }),
221
+ mock .call .get ().raise_for_status (),
222
+ ]
223
+
224
+
170
225
@pytest .mark .parametrize (
171
226
("html" , "url" , "expected" ),
172
227
[
@@ -416,6 +471,7 @@ def test_request_http_error(caplog):
416
471
caplog .set_level (logging .DEBUG )
417
472
link = Link ('http://localhost' )
418
473
session = Mock (PipSession )
474
+ session .headers = {}
419
475
session .get .return_value = resp = Mock ()
420
476
resp .raise_for_status .side_effect = requests .HTTPError ('Http error' )
421
477
assert _get_html_page (link , session = session ) is None
@@ -429,6 +485,7 @@ def test_request_retries(caplog):
429
485
caplog .set_level (logging .DEBUG )
430
486
link = Link ('http://localhost' )
431
487
session = Mock (PipSession )
488
+ session .headers = {}
432
489
session .get .side_effect = requests .exceptions .RetryError ('Retry error' )
433
490
assert _get_html_page (link , session = session ) is None
434
491
assert (
@@ -501,6 +558,7 @@ def test_get_html_page_directory_append_index(tmpdir):
501
558
expected_url = "{}/index.html" .format (dir_url .rstrip ("/" ))
502
559
503
560
session = mock .Mock (PipSession )
561
+ session .headers = {}
504
562
fake_response = make_fake_html_response (expected_url )
505
563
mock_func = mock .patch ("pip._internal.index.collector._get_html_response" )
506
564
with mock_func as mock_func :
0 commit comments