test_proxies.py
This file
Path
tests/client/test_proxies.py
111 lines · 3.6 KiB · Python
Last changed by
Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
History of this fileTake it
1import pytest23import httpx456@pytest.mark.parametrize(7 ["proxies", "expected_proxies"],8 [9 ("http://127.0.0.1", [("all", "http://127.0.0.1")]),10 ({"all": "http://127.0.0.1"}, [("all", "http://127.0.0.1")]),11 (12 {"http": "http://127.0.0.1", "https": "https://127.0.0.1"},13 [("http", "http://127.0.0.1"), ("https", "https://127.0.0.1")],14 ),15 (httpx.HTTPProxy("http://127.0.0.1"), [("all", "http://127.0.0.1")]),16 (17 {"https": httpx.HTTPProxy("https://127.0.0.1"), "all": "http://127.0.0.1"},18 [("all", "http://127.0.0.1"), ("https", "https://127.0.0.1")],19 ),20 ],21)22def test_proxies_parameter(proxies, expected_proxies):23 client = httpx.Client(proxies=proxies)2425 for proxy_key, url in expected_proxies:26 assert proxy_key in client.proxies27 assert client.proxies[proxy_key].proxy_url == url2829 assert len(expected_proxies) == len(client.proxies)303132def test_proxies_has_same_properties_as_dispatch():33 client = httpx.Client(34 proxies="http://127.0.0.1",35 verify="/path/to/verify",36 cert="/path/to/cert",37 trust_env=False,38 timeout=30,39 )40 pool = client.dispatch41 proxy = client.proxies["all"]4243 assert isinstance(proxy, httpx.HTTPProxy)4445 for prop in [46 "verify",47 "cert",48 "pool_limits",49 ]:50 assert getattr(pool, prop) == getattr(proxy, prop)515253PROXY_URL = "http://[::1]"545556@pytest.mark.parametrize(57 ["url", "proxies", "expected"],58 [59 ("http://example.com", None, None),60 ("http://example.com", {}, None),61 ("http://example.com", {"https": PROXY_URL}, None),62 ("http://example.com", {"http://example.net": PROXY_URL}, None),63 ("http://example.com:443", {"http://example.com": PROXY_URL}, None),64 ("http://example.com", {"all": PROXY_URL}, PROXY_URL),65 ("http://example.com", {"http": PROXY_URL}, PROXY_URL),66 ("http://example.com", {"all://example.com": PROXY_URL}, PROXY_URL),67 ("http://example.com", {"all://example.com:80": PROXY_URL}, PROXY_URL),68 ("http://example.com", {"http://example.com": PROXY_URL}, PROXY_URL),69 ("http://example.com", {"http://example.com:80": PROXY_URL}, PROXY_URL),70 ("http://example.com:8080", {"http://example.com:8080": PROXY_URL}, PROXY_URL),71 ("http://example.com:8080", {"http://example.com": PROXY_URL}, None),72 (73 "http://example.com",74 {75 "all": PROXY_URL + ":1",76 "http": PROXY_URL + ":2",77 "all://example.com": PROXY_URL + ":3",78 "http://example.com": PROXY_URL + ":4",79 },80 PROXY_URL + ":4",81 ),82 (83 "http://example.com",84 {85 "all": PROXY_URL + ":1",86 "http": PROXY_URL + ":2",87 "all://example.com": PROXY_URL + ":3",88 },89 PROXY_URL + ":3",90 ),91 (92 "http://example.com",93 {"all": PROXY_URL + ":1", "http": PROXY_URL + ":2"},94 PROXY_URL + ":2",95 ),96 ],97)98def test_dispatcher_for_request(url, proxies, expected):99 client = httpx.Client(proxies=proxies)100 dispatcher = client.dispatcher_for_url(httpx.URL(url))101102 if expected is None:103 assert dispatcher is client.dispatch104 else:105 assert isinstance(dispatcher, httpx.HTTPProxy)106 assert dispatcher.proxy_url == expected107108109def test_unsupported_proxy_scheme():110 with pytest.raises(ValueError):111 httpx.Client(proxies="ftp://127.0.0.1")