18
18
_clean_link ,
19
19
_clean_url_path ,
20
20
_determine_base_url ,
21
- _get_html_page ,
22
- _get_html_response ,
23
- _make_html_page ,
24
- _NotHTML ,
21
+ _get_index_content ,
22
+ _get_simple_response ,
23
+ _make_index_content ,
24
+ _NotAPIContent ,
25
25
_NotHTTP ,
26
26
parse_links ,
27
27
)
40
40
"file:///opt/data/pip-18.0.tar.gz" ,
41
41
],
42
42
)
43
- def test_get_html_response_archive_to_naive_scheme (url : str ) -> None :
43
+ def test_get_simple_response_archive_to_naive_scheme (url : str ) -> None :
44
44
"""
45
- `_get_html_response ()` should error on an archive-like URL if the scheme
45
+ `_get_simple_response ()` should error on an archive-like URL if the scheme
46
46
does not allow "poking" without getting data.
47
47
"""
48
48
with pytest .raises (_NotHTTP ):
49
- _get_html_response (url , session = mock .Mock (PipSession ))
49
+ _get_simple_response (url , session = mock .Mock (PipSession ))
50
50
51
51
52
52
@pytest .mark .parametrize (
@@ -57,12 +57,12 @@ def test_get_html_response_archive_to_naive_scheme(url: str) -> None:
57
57
],
58
58
)
59
59
@mock .patch ("pip._internal.index.collector.raise_for_status" )
60
- def test_get_html_response_archive_to_http_scheme (
60
+ def test_get_simple_response_archive_to_http_scheme (
61
61
mock_raise_for_status : mock .Mock , url : str , content_type : str
62
62
) -> None :
63
63
"""
64
- `_get_html_response ()` should send a HEAD request on an archive-like URL
65
- if the scheme supports it, and raise `_NotHTML ` if the response isn't HTML.
64
+ `_get_simple_response ()` should send a HEAD request on an archive-like URL
65
+ if the scheme supports it, and raise `_NotAPIContent ` if the response isn't HTML.
66
66
"""
67
67
session = mock .Mock (PipSession )
68
68
session .head .return_value = mock .Mock (
@@ -72,8 +72,8 @@ def test_get_html_response_archive_to_http_scheme(
72
72
}
73
73
)
74
74
75
- with pytest .raises (_NotHTML ) as ctx :
76
- _get_html_response (url , session = session )
75
+ with pytest .raises (_NotAPIContent ) as ctx :
76
+ _get_simple_response (url , session = session )
77
77
78
78
session .assert_has_calls (
79
79
[
@@ -91,18 +91,18 @@ def test_get_html_response_archive_to_http_scheme(
91
91
("file:///opt/data/pip-18.0.tar.gz" ),
92
92
],
93
93
)
94
- def test_get_html_page_invalid_content_type_archive (
94
+ def test_get_index_content_invalid_content_type_archive (
95
95
caplog : pytest .LogCaptureFixture , url : str
96
96
) -> None :
97
- """`_get_html_page ()` should warn if an archive URL is not HTML
97
+ """`_get_index_content ()` should warn if an archive URL is not HTML
98
98
and therefore cannot be used for a HEAD request.
99
99
"""
100
100
caplog .set_level (logging .WARNING )
101
101
link = Link (url )
102
102
103
103
session = mock .Mock (PipSession )
104
104
105
- assert _get_html_page (link , session = session ) is None
105
+ assert _get_index_content (link , session = session ) is None
106
106
assert (
107
107
"pip._internal.index.collector" ,
108
108
logging .WARNING ,
@@ -119,11 +119,11 @@ def test_get_html_page_invalid_content_type_archive(
119
119
],
120
120
)
121
121
@mock .patch ("pip._internal.index.collector.raise_for_status" )
122
- def test_get_html_response_archive_to_http_scheme_is_html (
122
+ def test_get_simple_response_archive_to_http_scheme_is_html (
123
123
mock_raise_for_status : mock .Mock , url : str
124
124
) -> None :
125
125
"""
126
- `_get_html_response ()` should work with archive-like URLs if the HEAD
126
+ `_get_simple_response ()` should work with archive-like URLs if the HEAD
127
127
request is responded with text/html.
128
128
"""
129
129
session = mock .Mock (PipSession )
@@ -135,7 +135,7 @@ def test_get_html_response_archive_to_http_scheme_is_html(
135
135
)
136
136
session .get .return_value = mock .Mock (headers = {"Content-Type" : "text/html" })
137
137
138
- resp = _get_html_response (url , session = session )
138
+ resp = _get_simple_response (url , session = session )
139
139
140
140
assert resp is not None
141
141
assert session .mock_calls == [
@@ -163,9 +163,11 @@ def test_get_html_response_archive_to_http_scheme_is_html(
163
163
],
164
164
)
165
165
@mock .patch ("pip._internal.index.collector.raise_for_status" )
166
- def test_get_html_response_no_head (mock_raise_for_status : mock .Mock , url : str ) -> None :
166
+ def test_get_simple_response_no_head (
167
+ mock_raise_for_status : mock .Mock , url : str
168
+ ) -> None :
167
169
"""
168
- `_get_html_response ()` shouldn't send a HEAD request if the URL does not
170
+ `_get_simple_response ()` shouldn't send a HEAD request if the URL does not
169
171
look like an archive, only the GET request that retrieves data.
170
172
"""
171
173
session = mock .Mock (PipSession )
@@ -179,7 +181,7 @@ def test_get_html_response_no_head(mock_raise_for_status: mock.Mock, url: str) -
179
181
)
180
182
)
181
183
182
- resp = _get_html_response (url , session = session )
184
+ resp = _get_simple_response (url , session = session )
183
185
184
186
assert resp is not None
185
187
assert session .head .call_count == 0
@@ -197,11 +199,11 @@ def test_get_html_response_no_head(mock_raise_for_status: mock.Mock, url: str) -
197
199
198
200
199
201
@mock .patch ("pip._internal.index.collector.raise_for_status" )
200
- def test_get_html_response_dont_log_clear_text_password (
202
+ def test_get_simple_response_dont_log_clear_text_password (
201
203
mock_raise_for_status : mock .Mock , caplog : pytest .LogCaptureFixture
202
204
) -> None :
203
205
"""
204
- `_get_html_response ()` should redact the password from the index URL
206
+ `_get_simple_response ()` should redact the password from the index URL
205
207
in its DEBUG log message.
206
208
"""
207
209
session = mock .Mock (PipSession )
@@ -217,7 +219,7 @@ def test_get_html_response_dont_log_clear_text_password(
217
219
218
220
caplog .set_level (logging .DEBUG )
219
221
220
- resp = _get_html_response (
222
+ resp = _get_simple_response (
221
223
"https://user:[email protected] /simple/" ,
session = session
222
224
)
223
225
@@ -428,6 +430,7 @@ def _test_parse_links_data_attribute(
428
430
html_bytes = html .encode ("utf-8" )
429
431
page = IndexContent (
430
432
html_bytes ,
433
+ "text/html" ,
431
434
encoding = None ,
432
435
# parse_links() is cached by url, so we inject a random uuid to ensure
433
436
# the page content isn't cached.
@@ -505,13 +508,15 @@ def test_parse_links_caches_same_page_by_url() -> None:
505
508
506
509
page_1 = IndexContent (
507
510
html_bytes ,
511
+ "text/html" ,
508
512
encoding = None ,
509
513
url = url ,
510
514
)
511
515
# Make a second page with zero content, to ensure that it's not accessed,
512
516
# because the page was cached by url.
513
517
page_2 = IndexContent (
514
518
b"" ,
519
+ "text/html" ,
515
520
encoding = None ,
516
521
url = url ,
517
522
)
@@ -520,6 +525,7 @@ def test_parse_links_caches_same_page_by_url() -> None:
520
525
# verify that the result is not cached.
521
526
page_3 = IndexContent (
522
527
re .sub (b"pkg1" , b"pkg2" , html_bytes ),
528
+ "text/html" ,
523
529
encoding = None ,
524
530
url = url ,
525
531
cache_link_parsing = False ,
@@ -541,7 +547,9 @@ def test_parse_links_caches_same_page_by_url() -> None:
541
547
def test_parse_link_handles_deprecated_usage_properly () -> None :
542
548
html = b'<a href="/pkg1-1.0.tar.gz"></a><a href="/pkg1-2.0.tar.gz"></a>'
543
549
url = "https://example.com/simple/"
544
- page = IndexContent (html , encoding = None , url = url , cache_link_parsing = False )
550
+ page = IndexContent (
551
+ html , "text/html" , encoding = None , url = url , cache_link_parsing = False
552
+ )
545
553
546
554
parsed_links = list (parse_links (page , use_deprecated_html5lib = True ))
547
555
@@ -559,7 +567,7 @@ def test_request_http_error(
559
567
session = mock .Mock (PipSession )
560
568
session .get .return_value = mock .Mock ()
561
569
mock_raise_for_status .side_effect = NetworkConnectionError ("Http error" )
562
- assert _get_html_page (link , session = session ) is None
570
+ assert _get_index_content (link , session = session ) is None
563
571
assert "Could not fetch URL http://localhost: Http error - skipping" in caplog .text
564
572
565
573
@@ -568,19 +576,19 @@ def test_request_retries(caplog: pytest.LogCaptureFixture) -> None:
568
576
link = Link ("http://localhost" )
569
577
session = mock .Mock (PipSession )
570
578
session .get .side_effect = requests .exceptions .RetryError ("Retry error" )
571
- assert _get_html_page (link , session = session ) is None
579
+ assert _get_index_content (link , session = session ) is None
572
580
assert "Could not fetch URL http://localhost: Retry error - skipping" in caplog .text
573
581
574
582
575
- def test_make_html_page () -> None :
583
+ def test_make_index_content () -> None :
576
584
headers = {"Content-Type" : "text/html; charset=UTF-8" }
577
585
response = mock .Mock (
578
586
content = b"<content>" ,
579
587
url = "https://example.com/index.html" ,
580
588
headers = headers ,
581
589
)
582
590
583
- actual = _make_html_page (response )
591
+ actual = _make_index_content (response )
584
592
assert actual .content == b"<content>"
585
593
assert actual .encoding == "UTF-8"
586
594
assert actual .url == "https://example.com/index.html"
@@ -593,15 +601,15 @@ def test_make_html_page() -> None:
593
601
("git+https://github.com/pypa/pip.git" , "git" ),
594
602
],
595
603
)
596
- def test_get_html_page_invalid_scheme (
604
+ def test_get_index_content_invalid_scheme (
597
605
caplog : pytest .LogCaptureFixture , url : str , vcs_scheme : str
598
606
) -> None :
599
- """`_get_html_page ()` should error if an invalid scheme is given.
607
+ """`_get_index_content ()` should error if an invalid scheme is given.
600
608
601
609
Only file:, http:, https:, and ftp: are allowed.
602
610
"""
603
611
with caplog .at_level (logging .WARNING ):
604
- page = _get_html_page (Link (url ), session = mock .Mock (PipSession ))
612
+ page = _get_index_content (Link (url ), session = mock .Mock (PipSession ))
605
613
606
614
assert page is None
607
615
assert caplog .record_tuples == [
@@ -622,12 +630,12 @@ def test_get_html_page_invalid_scheme(
622
630
],
623
631
)
624
632
@mock .patch ("pip._internal.index.collector.raise_for_status" )
625
- def test_get_html_page_invalid_content_type (
633
+ def test_get_index_content_invalid_content_type (
626
634
mock_raise_for_status : mock .Mock ,
627
635
caplog : pytest .LogCaptureFixture ,
628
636
content_type : str ,
629
637
) -> None :
630
- """`_get_html_page ()` should warn if an invalid content-type is given.
638
+ """`_get_index_content ()` should warn if an invalid content-type is given.
631
639
Only text/html is allowed.
632
640
"""
633
641
caplog .set_level (logging .DEBUG )
@@ -641,7 +649,7 @@ def test_get_html_page_invalid_content_type(
641
649
"headers" : {"Content-Type" : content_type },
642
650
}
643
651
)
644
- assert _get_html_page (link , session = session ) is None
652
+ assert _get_index_content (link , session = session ) is None
645
653
mock_raise_for_status .assert_called_once_with (session .get .return_value )
646
654
assert (
647
655
"pip._internal.index.collector" ,
@@ -667,19 +675,19 @@ def make_fake_html_response(url: str) -> mock.Mock:
667
675
return mock .Mock (content = content , url = url , headers = {})
668
676
669
677
670
- def test_get_html_page_directory_append_index (tmpdir : Path ) -> None :
671
- """`_get_html_page ()` should append "index.html" to a directory URL."""
678
+ def test_get_index_content_directory_append_index (tmpdir : Path ) -> None :
679
+ """`_get_index_content ()` should append "index.html" to a directory URL."""
672
680
dirpath = tmpdir / "something"
673
681
dirpath .mkdir ()
674
682
dir_url = dirpath .as_uri ()
675
683
expected_url = "{}/index.html" .format (dir_url .rstrip ("/" ))
676
684
677
685
session = mock .Mock (PipSession )
678
686
fake_response = make_fake_html_response (expected_url )
679
- mock_func = mock .patch ("pip._internal.index.collector._get_html_response " )
687
+ mock_func = mock .patch ("pip._internal.index.collector._get_simple_response " )
680
688
with mock_func as mock_func :
681
689
mock_func .return_value = fake_response
682
- actual = _get_html_page (Link (dir_url ), session = session )
690
+ actual = _get_index_content (Link (dir_url ), session = session )
683
691
assert mock_func .mock_calls == [
684
692
mock .call (expected_url , session = session ),
685
693
], f"actual calls: { mock_func .mock_calls } "
@@ -779,16 +787,16 @@ def check_links_include(links: List[Link], names: List[str]) -> None:
779
787
780
788
781
789
class TestLinkCollector :
782
- @mock .patch ("pip._internal.index.collector._get_html_response " )
783
- def test_fetch_page (self , mock_get_html_response : mock .Mock ) -> None :
790
+ @mock .patch ("pip._internal.index.collector._get_simple_response " )
791
+ def test_fetch_response (self , mock_get_simple_response : mock .Mock ) -> None :
784
792
url = "https://pypi.org/simple/twine/"
785
793
786
794
fake_response = make_fake_html_response (url )
787
- mock_get_html_response .return_value = fake_response
795
+ mock_get_simple_response .return_value = fake_response
788
796
789
797
location = Link (url , cache_link_parsing = False )
790
798
link_collector = make_test_link_collector ()
791
- actual = link_collector .fetch_page (location )
799
+ actual = link_collector .fetch_response (location )
792
800
793
801
assert actual is not None
794
802
assert actual .content == fake_response .content
@@ -797,8 +805,8 @@ def test_fetch_page(self, mock_get_html_response: mock.Mock) -> None:
797
805
assert actual .cache_link_parsing == location .cache_link_parsing
798
806
799
807
# Also check that the right session object was passed to
800
- # _get_html_response ().
801
- mock_get_html_response .assert_called_once_with (
808
+ # _get_simple_response ().
809
+ mock_get_simple_response .assert_called_once_with (
802
810
url ,
803
811
session = link_collector .session ,
804
812
)
0 commit comments