def test_get_html_page_directory_append_index(tmpdir): """`_get_html_page()` should append "index.html" to a directory URL. """ dirpath = tmpdir.mkdir("something") dir_url = "file:///{}".format( urllib_request.pathname2url(dirpath).lstrip("/"), ) session = mock.Mock(PipSession) with mock.patch("pip._internal.collector._get_html_response") as mock_func: _get_html_page(Link(dir_url), session=session) assert mock_func.mock_calls == [ mock.call( "{}/index.html".format(dir_url.rstrip("/")), session=session, ), ]
def test_request_retries(caplog): caplog.set_level(logging.DEBUG) link = Link('http://localhost') session = Mock(PipSession) session.get.side_effect = requests.exceptions.RetryError('Retry error') assert _get_html_page(link, session=session) is None assert ('Could not fetch URL http://localhost: Retry error - skipping' in caplog.text)
def test_request_http_error(caplog): caplog.set_level(logging.DEBUG) link = Link('http://localhost') session = Mock(PipSession) session.get.return_value = resp = Mock() resp.raise_for_status.side_effect = requests.HTTPError('Http error') assert _get_html_page(link, session=session) is None assert ('Could not fetch URL http://localhost: Http error - skipping' in caplog.text)
def test_get_html_page_invalid_scheme(caplog, url, vcs_scheme): """`_get_html_page()` should error if an invalid scheme is given. Only file:, http:, https:, and ftp: are allowed. """ with caplog.at_level(logging.DEBUG): page = _get_html_page(Link(url), session=mock.Mock(PipSession)) assert page is None assert caplog.record_tuples == [ ( "pip._internal.collector", logging.DEBUG, "Cannot look at {} URL {}".format(vcs_scheme, url), ), ]
def test_get_html_page_directory_append_index(tmpdir): """`_get_html_page()` should append "index.html" to a directory URL. """ dirpath = tmpdir.mkdir("something") dir_url = "file:///{}".format( urllib_request.pathname2url(dirpath).lstrip("/"), ) expected_url = "{}/index.html".format(dir_url.rstrip("/")) session = mock.Mock(PipSession) fake_response = make_fake_html_response(expected_url) with mock.patch("pip._internal.collector._get_html_response") as mock_func: mock_func.return_value = fake_response actual = _get_html_page(Link(dir_url), session=session) assert mock_func.mock_calls == [ mock.call(expected_url, session=session), ], 'actual calls: {}'.format(mock_func.mock_calls) assert actual.content == fake_response.content assert actual.encoding is None assert actual.url == expected_url