Skip to content
Myr

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 file

Take it

Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085

lines 1–29why, one step back
1import pytest
23import 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)

Propogate HTTPProxy config from Client(#377)Seth Michael Larsonbefore adoption336d22d

lines 30–32why, one step back
30
3132def test_proxies_has_same_properties_as_dispatch():

Drop sync (#544)Tom Christiebefore adoption206c537

line 33why, one step back
33 client = httpx.Client(

Get test suite back to ~100% line coverage (#406)Seth Michael Larsonbefore adoption05f5dc2

lines 34–39why, one step back
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 )

Propogate HTTPProxy config from Client(#377)Seth Michael Larsonbefore adoption336d22d

lines 40–50why, one step back
40 pool = client.dispatch
41 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)

Get test suite back to ~100% line coverage (#406)Seth Michael Larsonbefore adoption05f5dc2

lines 51–98why, one step back
51
5253PROXY_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):

Drop sync (#544)Tom Christiebefore adoption206c537

line 99why, one step back
99 client = httpx.Client(proxies=proxies)

Clean up '_dispatcher_for_request' into 'dispatcher_for_url' (#561)Tom Christiebefore adoptione045b86

line 100why, one step back
100 dispatcher = client.dispatcher_for_url(httpx.URL(url))

Get test suite back to ~100% line coverage (#406)Seth Michael Larsonbefore adoption05f5dc2

lines 101–110why, one step back
101
102 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):

Drop sync (#544)Tom Christiebefore adoption206c537

line 111why, one step back
111 httpx.Client(proxies="ftp://127.0.0.1")