Skip to content

Commit 19f7535

Browse files
committed
Test against netrc env
1 parent c971ffe commit 19f7535

File tree

1 file changed

+59
-7
lines changed

1 file changed

+59
-7
lines changed

tests/functional/test_proxy.py

+59-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
import ssl
2+
from pathlib import Path
13
from typing import Any, Dict, Optional
24

35
import proxy
46
import pytest
57
from proxy.http.proxy import HttpProxyBasePlugin
68

7-
from tests.lib import PipTestEnvironment
8-
from tests.lib.path import Path
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+
)
917

1018

1119
class AccessLogPlugin(HttpProxyBasePlugin):
@@ -15,14 +23,12 @@ def on_access_log(self, context: Dict[str, Any]) -> Optional[Dict[str, Any]]:
1523

1624

1725
@pytest.mark.network
18-
def test_proxy_overrides_env(
19-
script: PipTestEnvironment, monkeypatch: pytest.MonkeyPatch
20-
) -> None:
21-
monkeypatch.setenv("http_proxy", "127:0.0.1:8888")
22-
monkeypatch.setenv("https_proxy", "127:0.0.1:8888")
26+
def test_proxy_overrides_env(script: PipTestEnvironment) -> None:
2327
with proxy.Proxy(
2428
port=8899,
2529
), proxy.Proxy(plugins=[AccessLogPlugin], port=8888):
30+
script.environ["http_proxy"] = "127:0.0.1:8888"
31+
script.environ["https_proxy"] = "127:0.0.1:8888"
2632
result = script.pip(
2733
"download",
2834
"--proxy",
@@ -35,3 +41,49 @@ def test_proxy_overrides_env(
3541
)
3642
result.did_create(Path("scratch") / "pip_downloads" / "INITools-0.1.tar.gz")
3743
assert "CONNECT" not in result.stdout
44+
45+
46+
def test_proxy_does_not_override_netrc(
47+
script: PipTestEnvironment,
48+
data: TestData,
49+
cert_factory: CertFactory,
50+
) -> None:
51+
cert_path = cert_factory()
52+
ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
53+
ctx.load_cert_chain(cert_path, cert_path)
54+
ctx.load_verify_locations(cafile=cert_path)
55+
ctx.verify_mode = ssl.CERT_REQUIRED
56+
57+
server = make_mock_server(ssl_context=ctx)
58+
server.mock.side_effect = [
59+
package_page(
60+
{
61+
"simple-3.0.tar.gz": "/files/simple-3.0.tar.gz",
62+
}
63+
),
64+
authorization_response(data.packages / "simple-3.0.tar.gz"),
65+
authorization_response(data.packages / "simple-3.0.tar.gz"),
66+
]
67+
68+
url = f"https://{server.host}:{server.port}/simple"
69+
70+
netrc = script.scratch_path / ".netrc"
71+
netrc.write_text(f"machine {server.host} login USERNAME password PASSWORD")
72+
with proxy.Proxy(port=8888), server_running(server):
73+
script.environ["NETRC"] = netrc
74+
script.pip(
75+
"install",
76+
"--proxy",
77+
"http://127.0.0.1:8888",
78+
"--trusted-host",
79+
"127.0.0.1",
80+
"--no-cache-dir",
81+
"--index-url",
82+
url,
83+
"--cert",
84+
cert_path,
85+
"--client-cert",
86+
cert_path,
87+
"simple",
88+
)
89+
script.assert_installed(simple="3.0")

0 commit comments

Comments
 (0)