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
1import httpcore2import pytest34import httpx5from httpx._utils import URLPattern678def 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, port181920@pytest.mark.parametrize(21 ["proxies", "expected_proxies"],22 [23 ("http://127.0.0.1", [("all://", "http://127.0.0.1")]),24 ({"all://": "http://127.0.0.1"}, [("all://", "http://127.0.0.1")]),25 (26 {"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")],28 ),29 (httpx.Proxy("http://127.0.0.1"), [("all://", "http://127.0.0.1")]),30 (31 {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")],36 ),37 ],38)39def test_proxies_parameter(proxies, expected_proxies):40 client = httpx.Client(proxies=proxies)4142 for proxy_key, url in expected_proxies:43 pattern = URLPattern(proxy_key)44 assert pattern in client._proxies45 proxy = client._proxies[pattern]46 assert isinstance(proxy, httpcore.SyncHTTPProxy)47 assert proxy.proxy_origin == url_to_origin(url)4849 assert len(expected_proxies) == len(client._proxies)505152PROXY_URL = "http://[::1]"535455@pytest.mark.parametrize(56 ["url", "proxies", "expected"],57 [58 ("http://example.com", None, None),59 ("http://example.com", {}, None),60 ("http://example.com", {"https://": PROXY_URL}, None),61 ("http://example.com", {"http://example.net": PROXY_URL}, None),62 # 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 # ...76 ("http://example.com:443", {"http://example.com": PROXY_URL}, PROXY_URL),77 ("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),80 ("http://example.com", {"all://example.com": PROXY_URL}, PROXY_URL),81 ("http://example.com", {"all://example.com:80": PROXY_URL}, None),82 ("http://example.com", {"http://example.com": PROXY_URL}, PROXY_URL),83 ("http://example.com", {"http://example.com:80": PROXY_URL}, None),84 ("http://example.com:8080", {"http://example.com:8080": PROXY_URL}, PROXY_URL),85 ("http://example.com:8080", {"http://example.com": PROXY_URL}, PROXY_URL),86 (87 "http://example.com",88 {89 "all://": PROXY_URL + ":1",90 "http://": PROXY_URL + ":2",91 "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 {99 "all://": PROXY_URL + ":1",100 "http://": PROXY_URL + ":2",101 "all://example.com": PROXY_URL + ":3",102 },103 PROXY_URL + ":3",104 ),105 (106 "http://example.com",107 {"all://": PROXY_URL + ":1", "http://": PROXY_URL + ":2"},108 PROXY_URL + ":2",109 ),110 ],111)112def test_transport_for_request(url, proxies, expected):113 client = httpx.Client(proxies=proxies)114 transport = client._transport_for_url(httpx.URL(url))115116 if expected is None:117 assert transport is client._transport118 else:119 assert isinstance(transport, httpcore.SyncHTTPProxy)120 assert transport.proxy_origin == url_to_origin(expected)121122123@pytest.mark.asyncio124async def test_async_proxy_close():125 try:126 client = httpx.AsyncClient(proxies={"https://": PROXY_URL})127 await client.get("http://example.com")128 finally:129 await client.aclose()130131132def test_sync_proxy_close():133 try:134 client = httpx.Client(proxies={"https://": PROXY_URL})135 client.get("http://example.com")136 finally:137 client.close()138139140def test_unsupported_proxy_scheme():141 with pytest.raises(ValueError):142 httpx.Client(proxies="ftp://127.0.0.1")143144145@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 ),154 # Auto prepend http scheme155 ("http://google.com", {"HTTP_PROXY": "example.com"}, "http://example.com"),156 (157 "http://google.com",158 {"HTTP_PROXY": "http://example.com", "NO_PROXY": "google.com"},159 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": "*"},238 None,239 ),240 ],241)242@pytest.mark.parametrize("client_class", [httpx.Client, httpx.AsyncClient])243def test_proxies_environ(monkeypatch, client_class, url, env, expected):244 for name, value in env.items():245 monkeypatch.setenv(name, value)246247 client = client_class()248 transport = client._transport_for_url(httpx.URL(url))249250 if expected is None:251 assert transport == client._transport252 else:253 assert transport.proxy_origin == url_to_origin(expected)254255256@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:266 httpx.Client(proxies=proxies)267268 warning_message = str(block.pop(DeprecationWarning))269270 assert expected_scheme in warning_message