def test_get_html_response_archive_to_naive_scheme(url): """ `_get_html_response()` should error on an archive-like URL if the scheme does not allow "poking" without getting data. """ with pytest.raises(_NotHTTP): _get_html_response(url, session=mock.Mock(PipSession))
def test_get_html_response_archive_to_http_scheme( mock_raise_for_status: mock.Mock, url: str, content_type: str ) -> None: """ `_get_html_response()` should send a HEAD request on an archive-like URL if the scheme supports it, and raise `_NotHTML` if the response isn't HTML. """ session = mock.Mock(PipSession) session.head.return_value = mock.Mock( **{ "request.method": "HEAD", "headers": {"Content-Type": content_type}, } ) with pytest.raises(_NotHTML) as ctx: _get_html_response(url, session=session) session.assert_has_calls( [ mock.call.head(url, allow_redirects=True), ] ) mock_raise_for_status.assert_called_once_with(session.head.return_value) assert ctx.value.args == (content_type, "HEAD")
def test_get_html_response_archive_to_http_scheme_is_html(url): """ `_get_html_response()` should work with archive-like URLs if the HEAD request is responded with text/html. """ session = mock.Mock(PipSession) session.head.return_value = mock.Mock(**{ "request.method": "HEAD", "headers": { "Content-Type": "text/html" }, }) session.get.return_value = mock.Mock(headers={"Content-Type": "text/html"}) resp = _get_html_response(url, session=session) assert resp is not None assert session.mock_calls == [ mock.call.head(url, allow_redirects=True), mock.call.head().raise_for_status(), mock.call.get(url, headers={ "Accept": "text/html", "Cache-Control": "max-age=0", }), mock.call.get().raise_for_status(), ]
def test_get_html_response_dont_log_clear_text_password(caplog): """ `_get_html_response()` should redact the password from the index URL in its DEBUG log message. """ session = mock.Mock(PipSession) # Mock the headers dict to ensure it is accessed. session.get.return_value = mock.Mock(headers=mock.Mock( **{ "get.return_value": "text/html", })) caplog.set_level(logging.DEBUG) resp = _get_html_response("https://*****:*****@example.com/simple/", session=session) assert resp is not None assert len(caplog.records) == 1 record = caplog.records[0] assert record.levelname == 'DEBUG' assert record.message.splitlines() == [ "Getting page https://user:****@example.com/simple/", ]
def test_get_html_response_no_head(url): """ `_get_html_response()` shouldn't send a HEAD request if the URL does not look like an archive, only the GET request that retrieves data. """ session = mock.Mock(PipSession) # Mock the headers dict to ensure it is accessed. session.get.return_value = mock.Mock(headers=mock.Mock( **{ "get.return_value": "text/html", })) resp = _get_html_response(url, session=session) assert resp is not None assert session.head.call_count == 0 assert session.get.mock_calls == [ mock.call(url, headers={ "Accept": "text/html", "Cache-Control": "max-age=0", }), mock.call().raise_for_status(), mock.call().headers.get("Content-Type", ""), ]
def test_get_html_response_dont_log_clear_text_password( mock_raise_for_status: mock.Mock, caplog: pytest.LogCaptureFixture) -> None: """ `_get_html_response()` should redact the password from the index URL in its DEBUG log message. """ session = mock.Mock(PipSession) # Mock the headers dict to ensure it is accessed. session.get.return_value = mock.Mock(headers=mock.Mock( **{ "get.return_value": "text/html", })) caplog.set_level(logging.DEBUG) resp = _get_html_response("https://*****:*****@example.com/simple/", session=session) assert resp is not None mock_raise_for_status.assert_called_once_with(resp) assert len(caplog.records) == 1 record = caplog.records[0] assert record.levelname == "DEBUG" assert record.message.splitlines() == [ "Getting page https://user:****@example.com/simple/", ]
def test_get_html_response_override_cache_control(url, headers, cache_control): """ `_get_html_response()` should use the session's default value for the Cache-Control header if provided. """ session = mock.Mock(PipSession) session.headers = headers session.head.return_value = mock.Mock(**{ "request.method": "HEAD", "headers": { "Content-Type": "text/html" }, }) session.get.return_value = mock.Mock(headers={"Content-Type": "text/html"}) resp = _get_html_response(url, session=session) assert resp is not None assert session.mock_calls == [ mock.call.head(url, allow_redirects=True), mock.call.head().raise_for_status(), mock.call.get(url, headers={ "Accept": "text/html", "Cache-Control": cache_control, }), mock.call.get().raise_for_status(), ]