|
| 1 | +import ssl |
| 2 | +from pathlib import Path |
| 3 | +from typing import Any, Dict |
| 4 | + |
| 5 | +import proxy |
| 6 | +import pytest |
| 7 | +from proxy.http.proxy import HttpProxyBasePlugin |
| 8 | + |
| 9 | +from tests.conftest import CertFactory |
| 10 | +from tests.lib import PipTestEnvironment, TestData |
| 11 | +from tests.lib.server import ( |
| 12 | + authorization_response, |
| 13 | + make_mock_server, |
| 14 | + package_page, |
| 15 | + server_running, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +class AccessLogPlugin(HttpProxyBasePlugin): |
| 20 | + def on_access_log(self, context: Dict[str, Any]) -> None: |
| 21 | + print(context) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.network |
| 25 | +def test_proxy_overrides_env( |
| 26 | + script: PipTestEnvironment, capfd: pytest.CaptureFixture[str] |
| 27 | +) -> None: |
| 28 | + with proxy.Proxy( |
| 29 | + port=8899, |
| 30 | + num_acceptors=1, |
| 31 | + ), proxy.Proxy(plugins=[AccessLogPlugin], port=8888, num_acceptors=1): |
| 32 | + script.environ["http_proxy"] = "127.0.0.1:8888" |
| 33 | + script.environ["https_proxy"] = "127.0.0.1:8888" |
| 34 | + result = script.pip( |
| 35 | + "download", |
| 36 | + "--proxy", |
| 37 | + "http://127.0.0.1:8899", |
| 38 | + "--trusted-host", |
| 39 | + "127.0.0.1", |
| 40 | + "-d", |
| 41 | + "pip_downloads", |
| 42 | + "INITools==0.1", |
| 43 | + ) |
| 44 | + result.did_create(Path("scratch") / "pip_downloads" / "INITools-0.1.tar.gz") |
| 45 | + out, _ = capfd.readouterr() |
| 46 | + assert "CONNECT" not in out |
| 47 | + |
| 48 | + |
| 49 | +def test_proxy_does_not_override_netrc( |
| 50 | + script: PipTestEnvironment, |
| 51 | + data: TestData, |
| 52 | + cert_factory: CertFactory, |
| 53 | +) -> None: |
| 54 | + cert_path = cert_factory() |
| 55 | + ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
| 56 | + ctx.load_cert_chain(cert_path, cert_path) |
| 57 | + ctx.load_verify_locations(cafile=cert_path) |
| 58 | + ctx.verify_mode = ssl.CERT_REQUIRED |
| 59 | + |
| 60 | + server = make_mock_server(ssl_context=ctx) |
| 61 | + server.mock.side_effect = [ |
| 62 | + package_page( |
| 63 | + { |
| 64 | + "simple-3.0.tar.gz": "/files/simple-3.0.tar.gz", |
| 65 | + } |
| 66 | + ), |
| 67 | + authorization_response(data.packages / "simple-3.0.tar.gz"), |
| 68 | + authorization_response(data.packages / "simple-3.0.tar.gz"), |
| 69 | + ] |
| 70 | + |
| 71 | + url = f"https://{server.host}:{server.port}/simple" |
| 72 | + |
| 73 | + netrc = script.scratch_path / ".netrc" |
| 74 | + netrc.write_text(f"machine {server.host} login USERNAME password PASSWORD") |
| 75 | + with proxy.Proxy(port=8888, num_acceptors=1), server_running(server): |
| 76 | + script.environ["NETRC"] = netrc |
| 77 | + script.pip( |
| 78 | + "install", |
| 79 | + "--proxy", |
| 80 | + "http://127.0.0.1:8888", |
| 81 | + "--trusted-host", |
| 82 | + "127.0.0.1", |
| 83 | + "--no-cache-dir", |
| 84 | + "--index-url", |
| 85 | + url, |
| 86 | + "--cert", |
| 87 | + cert_path, |
| 88 | + "--client-cert", |
| 89 | + cert_path, |
| 90 | + "simple", |
| 91 | + ) |
| 92 | + script.assert_installed(simple="3.0") |
0 commit comments