Repository: pypa/pip
File: tests/functional/test_proxy.py (Python)

Code before:
import pytest
from proxy.http.proxy import HttpProxyBasePlugin

from tests.conftest import CertFactory
from tests.lib import PipTestEnvironment, TestData
from tests.lib.server import (
    authorization_response,
    make_mock_server,
    package_page,
    server_running,
)


class AccessLogPlugin(HttpProxyBasePlugin):
    def on_access_log(self, context: Dict[str, Any]) -> None:
        print(context)


@pytest.mark.network
def test_proxy_overrides_env(script: PipTestEnvironment) -> None:
    with proxy.Proxy(
        port=8899,
        num_acceptors=1,
    ), proxy.Proxy(plugins=[AccessLogPlugin], port=8888, num_acceptors=1):
        script.environ["http_proxy"] = "127:0.0.1:8888"
        script.environ["https_proxy"] = "127:0.0.1:8888"
        result = script.pip(
            "download",
            "--proxy",
            "http://127.0.0.1:8899",
            "--trusted-host",
            "127.0.0.1",
            "-d",
            "pip_downloads",
            "INITools==0.1",
        )
        result.did_create(Path("scratch") / "pip_downloads" / "INITools-0.1.tar.gz")
        assert "CONNECT" not in result.stdout


def test_proxy_does_not_override_netrc(
    script: PipTestEnvironment,
    data: TestData,
    cert_factory: CertFactory,
) -> None:
    cert_path = cert_factory()
    ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
    ctx.load_cert_chain(cert_path, cert_path)
    ctx.load_verify_locations(cafile=cert_path)
    ctx.verify_mode = ssl.CERT_REQUIRED


Review comment (bug):
I am looking at the code to figure out reasons behind Windows test failures.  Shouldn't this be like `127.` instead of `127:`.  Unsure if this even can be the root cause, but worth pointing out :)

Code after:
import pytest
from proxy.http.proxy import HttpProxyBasePlugin

from tests.conftest import CertFactory
from tests.lib import PipTestEnvironment, TestData
from tests.lib.server import (
    authorization_response,
    make_mock_server,
    package_page,
    server_running,
)


class AccessLogPlugin(HttpProxyBasePlugin):
    def on_access_log(self, context: Dict[str, Any]) -> None:
        print(context)


@pytest.mark.network
def test_proxy_overrides_env(
    script: PipTestEnvironment, capfd: pytest.CaptureFixture[str]
) -> None:
    with proxy.Proxy(
        port=8899,
        num_acceptors=1,
    ), proxy.Proxy(plugins=[AccessLogPlugin], port=8888, num_acceptors=1):
        script.environ["http_proxy"] = "127.0.0.1:8888"
        script.environ["https_proxy"] = "127.0.0.1:8888"
        result = script.pip(
            "download",
            "--proxy",
            "http://127.0.0.1:8899",
            "--trusted-host",
            "127.0.0.1",
            "-d",
            "pip_downloads",
            "INITools==0.1",
        )
        result.did_create(Path("scratch") / "pip_downloads" / "INITools-0.1.tar.gz")
        out, _ = capfd.readouterr()
        assert "CONNECT" not in out


def test_proxy_does_not_override_netrc(
    script: PipTestEnvironment,
    data: TestData,
    cert_factory: CertFactory,
) -> None:
    cert_path = cert_factory()
    ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
    ctx.load_cert_chain(cert_path, cert_path)