test_proxies.py
This file
Path
tests/client/test_proxies.py
265 lines · 9.4 KiB · Python
Last changed by
add socks5h proxy support (#3178)Bin Liubefore adoption12be5c4
History of this fileTake it
1import httpcore2import pytest34import httpx567def url_to_origin(url: str) -> httpcore.URL:8 """9 Given a URL string, return the origin in the raw tuple format that10 `httpcore` uses for it's representation.11 """12 u = httpx.URL(url)13 return httpcore.URL(scheme=u.raw_scheme, host=u.raw_host, port=u.port, target="/")141516def test_socks_proxy():17 url = httpx.URL("http://www.example.com")1819 for proxy in ("socks5://localhost/", "socks5h://localhost/"):20 client = httpx.Client(proxy=proxy)21 transport = client._transport_for_url(url)22 assert isinstance(transport, httpx.HTTPTransport)23 assert isinstance(transport._pool, httpcore.SOCKSProxy)2425 async_client = httpx.AsyncClient(proxy=proxy)26 async_transport = async_client._transport_for_url(url)27 assert isinstance(async_transport, httpx.AsyncHTTPTransport)28 assert isinstance(async_transport._pool, httpcore.AsyncSOCKSProxy)293031PROXY_URL = "http://[::1]"323334@pytest.mark.parametrize(35 ["url", "proxies", "expected"],36 [37 ("http://example.com", {}, None),38 ("http://example.com", {"https://": PROXY_URL}, None),39 ("http://example.com", {"http://example.net": PROXY_URL}, None),40 # Using "*" should match any domain name.41 ("http://example.com", {"http://*": PROXY_URL}, PROXY_URL),42 ("https://example.com", {"http://*": PROXY_URL}, None),43 # Using "example.com" should match example.com, but not www.example.com44 ("http://example.com", {"http://example.com": PROXY_URL}, PROXY_URL),45 ("http://www.example.com", {"http://example.com": PROXY_URL}, None),46 # Using "*.example.com" should match www.example.com, but not example.com47 ("http://example.com", {"http://*.example.com": PROXY_URL}, None),48 ("http://www.example.com", {"http://*.example.com": PROXY_URL}, PROXY_URL),49 # Using "*example.com" should match example.com and www.example.com50 ("http://example.com", {"http://*example.com": PROXY_URL}, PROXY_URL),51 ("http://www.example.com", {"http://*example.com": PROXY_URL}, PROXY_URL),52 ("http://wwwexample.com", {"http://*example.com": PROXY_URL}, None),53 # ...54 ("http://example.com:443", {"http://example.com": PROXY_URL}, PROXY_URL),55 ("http://example.com", {"all://": PROXY_URL}, PROXY_URL),56 ("http://example.com", {"http://": PROXY_URL}, PROXY_URL),57 ("http://example.com", {"all://example.com": PROXY_URL}, PROXY_URL),58 ("http://example.com", {"http://example.com": PROXY_URL}, PROXY_URL),59 ("http://example.com", {"http://example.com:80": PROXY_URL}, PROXY_URL),60 ("http://example.com:8080", {"http://example.com:8080": PROXY_URL}, PROXY_URL),61 ("http://example.com:8080", {"http://example.com": PROXY_URL}, PROXY_URL),62 (63 "http://example.com",64 {65 "all://": PROXY_URL + ":1",66 "http://": PROXY_URL + ":2",67 "all://example.com": PROXY_URL + ":3",68 "http://example.com": PROXY_URL + ":4",69 },70 PROXY_URL + ":4",71 ),72 (73 "http://example.com",74 {75 "all://": PROXY_URL + ":1",76 "http://": PROXY_URL + ":2",77 "all://example.com": PROXY_URL + ":3",78 },79 PROXY_URL + ":3",80 ),81 (82 "http://example.com",83 {"all://": PROXY_URL + ":1", "http://": PROXY_URL + ":2"},84 PROXY_URL + ":2",85 ),86 ],87)88def test_transport_for_request(url, proxies, expected):89 mounts = {key: httpx.HTTPTransport(proxy=value) for key, value in proxies.items()}90 client = httpx.Client(mounts=mounts)9192 transport = client._transport_for_url(httpx.URL(url))9394 if expected is None:95 assert transport is client._transport96 else:97 assert isinstance(transport, httpx.HTTPTransport)98 assert isinstance(transport._pool, httpcore.HTTPProxy)99 assert transport._pool._proxy_url == url_to_origin(expected)100101102@pytest.mark.anyio103@pytest.mark.network104async def test_async_proxy_close():105 try:106 transport = httpx.AsyncHTTPTransport(proxy=PROXY_URL)107 client = httpx.AsyncClient(mounts={"https://": transport})108 await client.get("http://example.com")109 finally:110 await client.aclose()111112113@pytest.mark.network114def test_sync_proxy_close():115 try:116 transport = httpx.HTTPTransport(proxy=PROXY_URL)117 client = httpx.Client(mounts={"https://": transport})118 client.get("http://example.com")119 finally:120 client.close()121122123def test_unsupported_proxy_scheme():124 with pytest.raises(ValueError):125 httpx.Client(proxy="ftp://127.0.0.1")126127128@pytest.mark.parametrize(129 ["url", "env", "expected"],130 [131 ("http://google.com", {}, None),132 (133 "http://google.com",134 {"HTTP_PROXY": "http://example.com"},135 "http://example.com",136 ),137 # Auto prepend http scheme138 ("http://google.com", {"HTTP_PROXY": "example.com"}, "http://example.com"),139 (140 "http://google.com",141 {"HTTP_PROXY": "http://example.com", "NO_PROXY": "google.com"},142 None,143 ),144 # Everything proxied when NO_PROXY is empty/unset145 (146 "http://127.0.0.1",147 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": ""},148 "http://localhost:123",149 ),150 # Not proxied if NO_PROXY matches URL.151 (152 "http://127.0.0.1",153 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "127.0.0.1"},154 None,155 ),156 # Proxied if NO_PROXY scheme does not match URL.157 (158 "http://127.0.0.1",159 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "https://127.0.0.1"},160 "http://localhost:123",161 ),162 # Proxied if NO_PROXY scheme does not match host.163 (164 "http://127.0.0.1",165 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "1.1.1.1"},166 "http://localhost:123",167 ),168 # Not proxied if NO_PROXY matches host domain suffix.169 (170 "http://courses.mit.edu",171 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "mit.edu"},172 None,173 ),174 # Proxied even though NO_PROXY matches host domain *prefix*.175 (176 "https://mit.edu.info",177 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "mit.edu"},178 "http://localhost:123",179 ),180 # Not proxied if one item in NO_PROXY case matches host domain suffix.181 (182 "https://mit.edu.info",183 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "mit.edu,edu.info"},184 None,185 ),186 # Not proxied if one item in NO_PROXY case matches host domain suffix.187 # May include whitespace.188 (189 "https://mit.edu.info",190 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "mit.edu, edu.info"},191 None,192 ),193 # Proxied if no items in NO_PROXY match.194 (195 "https://mit.edu.info",196 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "mit.edu,mit.info"},197 "http://localhost:123",198 ),199 # Proxied if NO_PROXY domain doesn't match.200 (201 "https://foo.example.com",202 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "www.example.com"},203 "http://localhost:123",204 ),205 # Not proxied for subdomains matching NO_PROXY, with a leading ".".206 (207 "https://www.example1.com",208 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": ".example1.com"},209 None,210 ),211 # Proxied, because NO_PROXY subdomains only match if "." separated.212 (213 "https://www.example2.com",214 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "ample2.com"},215 "http://localhost:123",216 ),217 # No requests are proxied if NO_PROXY="*" is set.218 (219 "https://www.example3.com",220 {"ALL_PROXY": "http://localhost:123", "NO_PROXY": "*"},221 None,222 ),223 ],224)225@pytest.mark.parametrize("client_class", [httpx.Client, httpx.AsyncClient])226def test_proxies_environ(monkeypatch, client_class, url, env, expected):227 for name, value in env.items():228 monkeypatch.setenv(name, value)229230 client = client_class()231 transport = client._transport_for_url(httpx.URL(url))232233 if expected is None:234 assert transport == client._transport235 else:236 assert transport._pool._proxy_url == url_to_origin(expected)237238239@pytest.mark.parametrize(240 ["proxies", "is_valid"],241 [242 ({"http": "http://127.0.0.1"}, False),243 ({"https": "http://127.0.0.1"}, False),244 ({"all": "http://127.0.0.1"}, False),245 ({"http://": "http://127.0.0.1"}, True),246 ({"https://": "http://127.0.0.1"}, True),247 ({"all://": "http://127.0.0.1"}, True),248 ],249)250def test_for_deprecated_proxy_params(proxies, is_valid):251 mounts = {key: httpx.HTTPTransport(proxy=value) for key, value in proxies.items()}252253 if not is_valid:254 with pytest.raises(ValueError):255 httpx.Client(mounts=mounts)256 else:257 httpx.Client(mounts=mounts)258259260def test_proxy_with_mounts():261 proxy_transport = httpx.HTTPTransport(proxy="http://127.0.0.1")262 client = httpx.Client(mounts={"http://": proxy_transport})263264 transport = client._transport_for_url(httpx.URL("http://example.com"))265 assert transport == proxy_transport