def test_proxies_has_same_properties_as_dispatch():
- client = httpx.AsyncClient(proxies="http://127.0.0.1")
+ client = httpx.AsyncClient(
+ proxies="http://127.0.0.1",
+ verify="/path/to/verify",
+ cert="/path/to/cert",
+ trust_env=False,
+ timeout=30,
+ http_versions=["HTTP/1.1"],
+ )
pool = client.dispatch
proxy = client.proxies["all"]
"backend",
]:
assert getattr(pool, prop) == getattr(proxy, prop)
+
+
+PROXY_URL = "http://[::1]"
+
+
+@pytest.mark.parametrize(
+ ["url", "proxies", "expected"],
+ [
+ ("http://example.com", None, None),
+ ("http://example.com", {}, None),
+ ("http://example.com", {"https": PROXY_URL}, None),
+ ("http://example.com", {"http://example.net": PROXY_URL}, None),
+ ("http://example.com:443", {"http://example.com": PROXY_URL}, None),
+ ("http://example.com", {"all": PROXY_URL}, PROXY_URL),
+ ("http://example.com", {"http": PROXY_URL}, PROXY_URL),
+ ("http://example.com", {"all://example.com": PROXY_URL}, PROXY_URL),
+ ("http://example.com", {"all://example.com:80": PROXY_URL}, PROXY_URL),
+ ("http://example.com", {"http://example.com": PROXY_URL}, PROXY_URL),
+ ("http://example.com", {"http://example.com:80": PROXY_URL}, PROXY_URL),
+ ("http://example.com:8080", {"http://example.com:8080": PROXY_URL}, PROXY_URL),
+ ("http://example.com:8080", {"http://example.com": PROXY_URL}, None),
+ (
+ "http://example.com",
+ {
+ "all": PROXY_URL + ":1",
+ "http": PROXY_URL + ":2",
+ "all://example.com": PROXY_URL + ":3",
+ "http://example.com": PROXY_URL + ":4",
+ },
+ PROXY_URL + ":4",
+ ),
+ (
+ "http://example.com",
+ {
+ "all": PROXY_URL + ":1",
+ "http": PROXY_URL + ":2",
+ "all://example.com": PROXY_URL + ":3",
+ },
+ PROXY_URL + ":3",
+ ),
+ (
+ "http://example.com",
+ {"all": PROXY_URL + ":1", "http": PROXY_URL + ":2"},
+ PROXY_URL + ":2",
+ ),
+ ],
+)
+def test_dispatcher_for_request(url, proxies, expected):
+ client = httpx.AsyncClient(proxies=proxies)
+ request = httpx.AsyncRequest("GET", url)
+ dispatcher = client._dispatcher_for_request(request, client.proxies)
+
+ if expected is None:
+ assert isinstance(dispatcher, httpx.ConnectionPool)
+ assert dispatcher is client.dispatch
+ else:
+ assert isinstance(dispatcher, httpx.HTTPProxy)
+ assert dispatcher.proxy_url == expected
+
+
+def test_unsupported_proxy_scheme():
+ with pytest.raises(ValueError):
+ httpx.AsyncClient(proxies="ftp://127.0.0.1")
backend=backend,
)
async with httpx.HTTPProxy(
- proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode=proxy_mode
+ proxy_url="http://127.0.0.1:8000",
+ backend=raw_io,
+ proxy_mode=proxy_mode,
+ proxy_headers={"Proxy-Authorization": "test", "Override": "2"},
) as proxy:
- response = await proxy.request("GET", f"http://example.com")
+ response = await proxy.request(
+ "GET", f"http://example.com", headers={"override": "1"}
+ )
assert response.status_code == 200
assert response.headers["Server"] == "origin-server"
assert recv[1].startswith(
b"GET http://example.com HTTP/1.1\r\nhost: example.com\r\n"
)
+ assert b"proxy-authorization: test" in recv[1]
+ assert b"override: 1" in recv[1]
def test_proxy_url_with_username_and_password():