Raise warning if proxy key is eg. "all" instead of "all://". (#1127)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* #1105 added deprecation warning, raised when we try to use proxies={"http": ...} instead of {"http://": ...}. Updated docs and added unit, which check the warning presence * Update tests/client/test_proxies.py Co-authored-by: Florimond Manca <florimond.manca@gmail.com> * Update tests/client/test_proxies.py
authored by cdeler · committed by GitHub · parent 1da46f3 · Browse files · 3 files·+53 added−28 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Tom Christie <tom@tomchristie.com>
- Co-authored-by: Florimond Manca <florimond.manca@gmail.com>
Diff stats
3 files+53 added−28 removed
- tests:+38 added−18 removed
- other:+15 added−10 removed
Provenance
- Commit
7279ed4- Tree
066548a- Author
cdeler <serj.krotov@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
1da46f3
3 files changed+53 added−28 removed
modified:docs/advanced.md+10 added-10 removed
282 unmodified lines283 For more advanced use cases, pass a proxies `dict`. For example, to route HTTP and HTTPS requests to 2 different proxies, respectively located at `http://localhost:8030`, and `http://localhost:8031`, pass a `dict` of proxy URLs:284 285 ```python286 proxies = {287- "http": "http://localhost:8030",288- "https": "http://localhost:8031",287+ "http://": "http://localhost:8030",288+ "https://": "http://localhost:8031",289 }290 291 with httpx.Client(proxies=proxies) as client:292 ...1 unmodified line294 295 For detailed information about proxy routing, see the [Routing](#routing) section.296 297 !!! tip "Gotcha"298- In most cases, the proxy URL for the `https` key _should_ use the `http://` scheme (that's not a typo!).298+ In most cases, the proxy URL for the `https://` key _should_ use the `http://` scheme (that's not a typo!).299 300 This is because HTTP proxying requires initiating a connection with the proxy server. While it's possible that your proxy supports doing it via HTTPS, most proxies only support doing it via HTTP.301 302 For more information, see [FORWARD vs TUNNEL](#forward-vs-tunnel).3 unmodified lines306 Proxy credentials can be passed as the `userinfo` section of the proxy URL. For example:307 308 ```python309 proxies = {310- "http": "http://username:password@localhost:8030",310+ "http://": "http://username:password@localhost:8030",311 # ...312 }313 ```314 315 ### Routing316 317 HTTPX provides fine-grained controls for deciding which requests should go through a proxy, and which shouldn't. This process is known as proxy routing.318 319-The `proxies` dictionary maps URL patterns ("proxy keys") to proxy URLs. HTTPX matches requested URLs against proxy keys to decide which proxy should be used, if any. Matching is done from most specific proxy keys (e.g. `https://<domain>:<port>`) to least specific ones (e.g. `https`).319+The `proxies` dictionary maps URL patterns ("proxy keys") to proxy URLs. HTTPX matches requested URLs against proxy keys to decide which proxy should be used, if any. Matching is done from most specific proxy keys (e.g. `https://<domain>:<port>`) to least specific ones (e.g. `https://`).320 321 HTTPX supports routing proxies based on **scheme**, **domain**, **port**, or a combination of these.322 323 #### Wildcard routing1 unmodified line325 Route everything through a proxy...326 327 ```python328 proxies = {329- "all": "http://localhost:8030",329+ "all://": "http://localhost:8030",330 }331 ```332 333 #### Scheme routing1 unmodified line335 Route HTTP requests through one proxy, and HTTPS requests through another...336 337 ```python338 proxies = {339- "http": "http://localhost:8030",340- "https": "http://localhost:8031",339+ "http://": "http://localhost:8030",340+ "https://": "http://localhost:8031",341 }342 ```343 344 #### Domain routing56 unmodified lines401 402 ```python403 proxies = {404 # Route requests through a proxy by default...405- "all": "http://localhost:8031",405+ "all://": "http://localhost:8031",406 # Except those for "example.com".407 "all://example.com": None,408 }409 ```4 unmodified lines414 415 ```python416 proxies = {417 # Route all traffic through a proxy by default...418- "all": "http://localhost:8030",418+ "all://": "http://localhost:8030",419 # But don't use proxies for HTTPS requests to "domain.io"...420 "https://domain.io": None,421 # And use another proxy for requests to "example.com" and its subdomains...422 "all://*example.com": "http://localhost:8031",438 unmodified linesmodified:httpx/_utils.py+5 added
476 unmodified lines477 def __init__(self, pattern: str) -> None:478 from ._models import URL479 480 if pattern and ":" not in pattern:481+ warn_deprecated(482+ f"Proxy keys should use proper URL forms rather "483+ f"than plain scheme strings. "484+ f'Instead of "{pattern}", use "{pattern}://"'485+ )486 pattern += "://"487 488 url = URL(pattern)489 self.pattern = pattern58 unmodified linesmodified:tests/client/test_proxies.py+38 added-18 removed
18 unmodified lines19 20 @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")]),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")],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")]),29+ (httpx.Proxy("http://127.0.0.1"), [("all://", "http://127.0.0.1")]),30 (31- {"https": httpx.Proxy("https://127.0.0.1"), "all": "http://127.0.0.1"},32- [("all", "http://127.0.0.1"), ("https", "https://127.0.0.1")],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 )39 def test_proxies_parameter(proxies, expected_proxies):16 unmodified lines56 ["url", "proxies", "expected"],57 [58 ("http://example.com", None, None),59 ("http://example.com", {}, None),57- ("http://example.com", {"https": PROXY_URL}, 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),8 unmodified lines73 ("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),74- ("http://example.com", {"all": PROXY_URL}, PROXY_URL),75- ("http://example.com", {"all": PROXY_URL, "http://example.com": None}, None),76- ("http://example.com", {"http": 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),1 unmodified line85 ("http://example.com:8080", {"http://example.com": PROXY_URL}, PROXY_URL),86 (87 "http://example.com",88 {86- "all": PROXY_URL + ":1",87- "http": PROXY_URL + ":2",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 {96- "all": PROXY_URL + ":1",97- "http": PROXY_URL + ":2",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",104- {"all": PROXY_URL + ":1", "http": PROXY_URL + ":2"},107+ {"all://": PROXY_URL + ":1", "http://": PROXY_URL + ":2"},108 PROXY_URL + ":2",109 ),110 ],111 )10 unmodified lines122 123 124 @pytest.mark.asyncio125 async def test_async_proxy_close():123- client = httpx.AsyncClient(proxies={"all": PROXY_URL})126+ client = httpx.AsyncClient(proxies={"all://": PROXY_URL})127 await client.aclose()128 129 130 def test_sync_proxy_close():128- client = httpx.Client(proxies={"all": PROXY_URL})131+ client = httpx.Client(proxies={"all://": PROXY_URL})132 client.close()133 134 135 def test_unsupported_proxy_scheme():110 unmodified lines246 if expected is None:247 assert transport == client._transport248 else:249 assert transport.proxy_origin == url_to_origin(expected)250+251+252+@pytest.mark.parametrize(253+ ["proxies", "expected_scheme"],254+ [255+ ({"http": "http://127.0.0.1"}, "http://"),256+ ({"https": "http://127.0.0.1"}, "https://"),257+ ({"all": "http://127.0.0.1"}, "all://"),258+ ],259+)260+def test_for_deprecated_proxy_params(proxies, expected_scheme):261+ with pytest.deprecated_call() as block:262+ httpx.AsyncClient(proxies=proxies)263+264+ warning_message = str(block.pop(DeprecationWarning))265+266+ assert expected_scheme in warning_message