test_proxies.py
This file
Path
tests/client/test_proxies.py
270 lines · 9.6 KiB · Python
Last changed by
Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
History of this fileTake it
The walk stopped after 200 commits: lines older than 9b6605c point at it.
Type check tests (#1054)Florimond Mancabefore adoption0296c2b
line 1why, one step back1import httpcoreUpdates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
lines 2–4why, one step back2import pytest34import httpxRename URLMatcher -> URLPattern (#1109)Florimond Mancabefore adoption71d5234
line 5why, one step back5from httpx._utils import URLPatternUpdates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
lines 6–7why, one step back67URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480
lines 8–19why, one step back8def url_to_origin(url: str):9 """10 Given a URL string, return the origin in the raw tuple format that11 `httpcore` uses for it's representation.12 """13 DEFAULT_PORTS = {b"http": 80, b"https": 443}14 scheme, host, explicit_port = httpx.URL(url).raw[:3]15 default_port = DEFAULT_PORTS[scheme]16 port = default_port if explicit_port is None else explicit_port17 return scheme, host, port1819Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
lines 20–22why, one step back20@pytest.mark.parametrize(21 ["proxies", "expected_proxies"],22 [Raise warning if proxy key is eg. "all" instead of "all://". (#1127)cdelerbefore adoption7279ed4
lines 23–24why, one step back23 ("http://127.0.0.1", [("all://", "http://127.0.0.1")]),24 ({"all://": "http://127.0.0.1"}, [("all://", "http://127.0.0.1")]),Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
line 25why, one step back25 (Raise warning if proxy key is eg. "all" instead of "all://". (#1127)cdelerbefore adoption7279ed4
lines 26–27why, one step back26 {"http://": "http://127.0.0.1", "https://": "https://127.0.0.1"},27 [("http://", "http://127.0.0.1"), ("https://", "https://127.0.0.1")],Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
line 28why, one step back28 ),Raise warning if proxy key is eg. "all" instead of "all://". (#1127)cdelerbefore adoption7279ed4
line 29why, one step back29 (httpx.Proxy("http://127.0.0.1"), [("all://", "http://127.0.0.1")]),Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
line 30why, one step back30 (Raise warning if proxy key is eg. "all" instead of "all://". (#1127)cdelerbefore adoption7279ed4
lines 31–35why, one step back31 {32 "https://": httpx.Proxy("https://127.0.0.1"),33 "all://": "http://127.0.0.1",34 },35 [("all://", "http://127.0.0.1"), ("https://", "https://127.0.0.1")],Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
lines 36–39why, one step back36 ),37 ],38)39def test_proxies_parameter(proxies, expected_proxies):Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 40why, one step back40 client = httpx.Client(proxies=proxies)Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
lines 41–42why, one step back4142 for proxy_key, url in expected_proxies:Rename URLMatcher -> URLPattern (#1109)Florimond Mancabefore adoption71d5234
lines 43–45why, one step back43 pattern = URLPattern(proxy_key)44 assert pattern in client._proxies45 proxy = client._proxies[pattern]Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 46why, one step back46 assert isinstance(proxy, httpcore.SyncHTTPProxy)URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480
line 47why, one step back47 assert proxy.proxy_origin == url_to_origin(url)Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
line 48why, one step back48More public API tightening (#1065)Tom Christiebefore adoptiona89d4ad
line 49why, one step back49 assert len(expected_proxies) == len(client._proxies)Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
lines 50–59why, one step back505152PROXY_URL = "http://[::1]"535455@pytest.mark.parametrize(56 ["url", "proxies", "expected"],57 [58 ("http://example.com", None, None),59 ("http://example.com", {}, None),Raise warning if proxy key is eg. "all" instead of "all://". (#1127)cdelerbefore adoption7279ed4
line 60why, one step back60 ("http://example.com", {"https://": PROXY_URL}, None),Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
line 61why, one step back61 ("http://example.com", {"http://example.net": PROXY_URL}, None),Cleaner no proxy support (#1103)Tom Christiebefore adoption682cad3
lines 62–75why, one step back62 # Using "*" should match any domain name.63 ("http://example.com", {"http://*": PROXY_URL}, PROXY_URL),64 ("https://example.com", {"http://*": PROXY_URL}, None),65 # Using "example.com" should match example.com, but not www.example.com66 ("http://example.com", {"http://example.com": PROXY_URL}, PROXY_URL),67 ("http://www.example.com", {"http://example.com": PROXY_URL}, None),68 # Using "*.example.com" should match www.example.com, but not example.com69 ("http://example.com", {"http://*.example.com": PROXY_URL}, None),70 ("http://www.example.com", {"http://*.example.com": PROXY_URL}, PROXY_URL),71 # Using "*example.com" should match example.com and www.example.com72 ("http://example.com", {"http://*example.com": PROXY_URL}, PROXY_URL),73 ("http://www.example.com", {"http://*example.com": PROXY_URL}, PROXY_URL),74 ("http://wwwexample.com", {"http://*example.com": PROXY_URL}, None),75 # ...URL matching (#1098)Tom Christiebefore adoptiondf54890
line 76why, one step back76 ("http://example.com:443", {"http://example.com": PROXY_URL}, PROXY_URL),Raise warning if proxy key is eg. "all" instead of "all://". (#1127)cdelerbefore adoption7279ed4
lines 77–79why, one step back77 ("http://example.com", {"all://": PROXY_URL}, PROXY_URL),78 ("http://example.com", {"all://": PROXY_URL, "http://example.com": None}, None),79 ("http://example.com", {"http://": PROXY_URL}, PROXY_URL),Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
line 80why, one step back80 ("http://example.com", {"all://example.com": PROXY_URL}, PROXY_URL),URL matching (#1098)Tom Christiebefore adoptiondf54890
line 81why, one step back81 ("http://example.com", {"all://example.com:80": PROXY_URL}, None),Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
line 82why, one step back82 ("http://example.com", {"http://example.com": PROXY_URL}, PROXY_URL),URL matching (#1098)Tom Christiebefore adoptiondf54890
line 83why, one step back83 ("http://example.com", {"http://example.com:80": PROXY_URL}, None),Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
line 84why, one step back84 ("http://example.com:8080", {"http://example.com:8080": PROXY_URL}, PROXY_URL),URL matching (#1098)Tom Christiebefore adoptiondf54890
line 85why, one step back85 ("http://example.com:8080", {"http://example.com": PROXY_URL}, PROXY_URL),Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
lines 86–88why, one step back86 (87 "http://example.com",88 {Raise warning if proxy key is eg. "all" instead of "all://". (#1127)cdelerbefore adoption7279ed4
lines 89–90why, one step back89 "all://": PROXY_URL + ":1",90 "http://": PROXY_URL + ":2",Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
lines 91–98why, one step back91 "all://example.com": PROXY_URL + ":3",92 "http://example.com": PROXY_URL + ":4",93 },94 PROXY_URL + ":4",95 ),96 (97 "http://example.com",98 {Raise warning if proxy key is eg. "all" instead of "all://". (#1127)cdelerbefore adoption7279ed4
lines 99–100why, one step back99 "all://": PROXY_URL + ":1",100 "http://": PROXY_URL + ":2",Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
lines 101–106why, one step back101 "all://example.com": PROXY_URL + ":3",102 },103 PROXY_URL + ":3",104 ),105 (106 "http://example.com",Raise warning if proxy key is eg. "all" instead of "all://". (#1127)cdelerbefore adoption7279ed4
line 107why, one step back107 {"all://": PROXY_URL + ":1", "http://": PROXY_URL + ":2"},Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
lines 108–112why, one step back108 PROXY_URL + ":2",109 ),110 ],111)112def test_transport_for_request(url, proxies, expected):Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 113why, one step back113 client = httpx.Client(proxies=proxies)Update to httpcore 0.10 (#1126)Tom Christiebefore adoption876e722
line 114why, one step back114 transport = client._transport_for_url(httpx.URL(url))Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
lines 115–116why, one step back115116 if expected is None:More public API tightening (#1065)Tom Christiebefore adoptiona89d4ad
line 117why, one step back117 assert transport is client._transportUpdates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
line 118why, one step back118 else:Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 119why, one step back119 assert isinstance(transport, httpcore.SyncHTTPProxy)URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480
line 120why, one step back120 assert transport.proxy_origin == url_to_origin(expected)Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
lines 121–122why, one step back121122Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 123–124why, one step back123@pytest.mark.asyncio124async def test_async_proxy_close():Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 125why, one step back125 try:Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
lines 126–127why, one step back126 client = httpx.AsyncClient(proxies={"https://": PROXY_URL})127 await client.get("http://example.com")Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
lines 128–129why, one step back128 finally:129 await client.aclose()Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 130–132why, one step back130131132def test_sync_proxy_close():Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 133why, one step back133 try:Issue warning on unclosed `AsyncClient`. (#1197)cdelerbefore adoptiondef9f1c
lines 134–135why, one step back134 client = httpx.Client(proxies={"https://": PROXY_URL})135 client.get("http://example.com")Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
lines 136–137why, one step back136 finally:137 client.close()Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 138–139why, one step back138139Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
lines 140–141why, one step back140def test_unsupported_proxy_scheme():141 with pytest.raises(ValueError):Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 142why, one step back142 httpx.Client(proxies="ftp://127.0.0.1")Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
lines 143–153why, one step back143144145@pytest.mark.parametrize(146 ["url", "env", "expected"],147 [148 ("http://google.com", {}, None),149 (150 "http://google.com",151 {"HTTP_PROXY": "http://example.com"},152 "http://example.com",153 ),Handle bare env proxy hostname gracefully (#1120)Joebefore adoptiond76b2c2
lines 154–155why, one step back154 # Auto prepend http scheme155 ("http://google.com", {"HTTP_PROXY": "example.com"}, "http://example.com"),Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
lines 156–158why, one step back156 (157 "http://google.com",158 {"HTTP_PROXY": "http://example.com", "NO_PROXY": "google.com"},Cleaner no proxy support (#1103)Tom Christiebefore adoption682cad3
lines 159–237why, one step back159 None,160 ),161 # Everything proxied when NO_PROXY is empty/unset162 (163 "http://127.0.0.1",164 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": ""},165 "http://localhost:123",166 ),167 # Not proxied if NO_PROXY matches URL.168 (169 "http://127.0.0.1",170 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "127.0.0.1"},171 None,172 ),173 # Proxied if NO_PROXY scheme does not match URL.174 (175 "http://127.0.0.1",176 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "https://127.0.0.1"},177 "http://localhost:123",178 ),179 # Proxied if NO_PROXY scheme does not match host.180 (181 "http://127.0.0.1",182 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "1.1.1.1"},183 "http://localhost:123",184 ),185 # Not proxied if NO_PROXY matches host domain suffix.186 (187 "http://courses.mit.edu",188 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "mit.edu"},189 None,190 ),191 # Proxied even though NO_PROXY matches host domain *prefix*.192 (193 "https://mit.edu.info",194 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "mit.edu"},195 "http://localhost:123",196 ),197 # Not proxied if one item in NO_PROXY case matches host domain suffix.198 (199 "https://mit.edu.info",200 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "mit.edu,edu.info"},201 None,202 ),203 # Not proxied if one item in NO_PROXY case matches host domain suffix.204 # May include whitespace.205 (206 "https://mit.edu.info",207 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "mit.edu, edu.info"},208 None,209 ),210 # Proxied if no items in NO_PROXY match.211 (212 "https://mit.edu.info",213 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "mit.edu,mit.info"},214 "http://localhost:123",215 ),216 # Proxied if NO_PROXY domain doesn't match.217 (218 "https://foo.example.com",219 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "www.example.com"},220 "http://localhost:123",221 ),222 # Not proxied for subdomains matching NO_PROXY, with a leading ".".223 (224 "https://www.example1.com",225 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": ".example1.com"},226 None,227 ),228 # Proxied, because NO_PROXY subdomains only match if "." seperated.229 (230 "https://www.example2.com",231 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "ample2.com"},232 "http://localhost:123",233 ),234 # No requests are proxied if NO_PROXY="*" is set.235 (236 "https://www.example3.com",237 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "*"},Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
lines 238–241why, one step back238 None,239 ),240 ],241)Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210
lines 242–243why, one step back242@pytest.mark.parametrize("client_class", [httpx.Client, httpx.AsyncClient])243def test_proxies_environ(monkeypatch, client_class, url, env, expected):Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
lines 244–246why, one step back244 for name, value in env.items():245 monkeypatch.setenv(name, value)246Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210
line 247why, one step back247 client = client_class()Update to httpcore 0.10 (#1126)Tom Christiebefore adoption876e722
line 248why, one step back248 transport = client._transport_for_url(httpx.URL(url))Updates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
lines 249–250why, one step back249250 if expected is None:More public API tightening (#1065)Tom Christiebefore adoptiona89d4ad
line 251why, one step back251 assert transport == client._transportUpdates for httpcore 0.9 interface. (#967)Tom Christiebefore adoption9b6605c
line 252why, one step back252 else:URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480
line 253why, one step back253 assert transport.proxy_origin == url_to_origin(expected)Raise warning if proxy key is eg. "all" instead of "all://". (#1127)cdelerbefore adoption7279ed4
lines 254–265why, one step back254255256@pytest.mark.parametrize(257 ["proxies", "expected_scheme"],258 [259 ({"http": "http://127.0.0.1"}, "http://"),260 ({"https": "http://127.0.0.1"}, "https://"),261 ({"all": "http://127.0.0.1"}, "all://"),262 ],263)264def test_for_deprecated_proxy_params(proxies, expected_scheme):265 with pytest.deprecated_call() as block:Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 266why, one step back266 httpx.Client(proxies=proxies)Raise warning if proxy key is eg. "all" instead of "all://". (#1127)cdelerbefore adoption7279ed4
lines 267–270why, one step back267268 warning_message = str(block.pop(DeprecationWarning))269270 assert expected_scheme in warning_message