)
from ._utils import (
NetRCInfo,
- URLMatcher,
+ URLPattern,
enforce_http_url,
get_environment_proxies,
get_logger,
trust_env=trust_env,
)
self._proxies: typing.Dict[
- URLMatcher, typing.Optional[httpcore.SyncHTTPTransport]
+ URLPattern, typing.Optional[httpcore.SyncHTTPTransport]
] = {
- URLMatcher(key): None
+ URLPattern(key): None
if proxy is None
else self._init_proxy_transport(
proxy,
enforce_http_url(request)
if self._proxies and not should_not_be_proxied(url):
- for matcher, transport in self._proxies.items():
- if matcher.matches(url):
+ for pattern, transport in self._proxies.items():
+ if pattern.matches(url):
return self._transport if transport is None else transport
return self._transport
trust_env=trust_env,
)
self._proxies: typing.Dict[
- URLMatcher, typing.Optional[httpcore.AsyncHTTPTransport]
+ URLPattern, typing.Optional[httpcore.AsyncHTTPTransport]
] = {
- URLMatcher(key): None
+ URLPattern(key): None
if proxy is None
else self._init_proxy_transport(
proxy,
enforce_http_url(request)
if self._proxies and not should_not_be_proxied(url):
- for matcher, transport in self._proxies.items():
- if matcher.matches(url):
+ for pattern, transport in self._proxies.items():
+ if pattern.matches(url):
return self._transport if transport is None else transport
return self._transport
return timedelta(seconds=self.end - self.start)
-class URLMatcher:
+class URLPattern:
"""
A utility class currently used for making lookups against proxy keys...
# Wildcard matching...
- >>> pattern = URLMatcher("all")
+ >>> pattern = URLPattern("all")
>>> pattern.matches(httpx.URL("http://example.com"))
True
# Witch scheme matching...
- >>> pattern = URLMatcher("https")
+ >>> pattern = URLPattern("https")
>>> pattern.matches(httpx.URL("https://example.com"))
True
>>> pattern.matches(httpx.URL("http://example.com"))
False
# With domain matching...
- >>> pattern = URLMatcher("https://example.com")
+ >>> pattern = URLPattern("https://example.com")
>>> pattern.matches(httpx.URL("https://example.com"))
True
>>> pattern.matches(httpx.URL("http://example.com"))
False
# Wildcard scheme, with domain matching...
- >>> pattern = URLMatcher("all://example.com")
+ >>> pattern = URLPattern("all://example.com")
>>> pattern.matches(httpx.URL("https://example.com"))
True
>>> pattern.matches(httpx.URL("http://example.com"))
False
# With port matching...
- >>> pattern = URLMatcher("https://example.com:1234")
+ >>> pattern = URLPattern("https://example.com:1234")
>>> pattern.matches(httpx.URL("https://example.com:1234"))
True
>>> pattern.matches(httpx.URL("https://example.com"))
@property
def priority(self) -> tuple:
"""
- The priority allows URLMatcher instances to be sortable, so that
+ The priority allows URLPattern instances to be sortable, so that
we can match from most specific to least specific.
"""
port_priority = -1 if self.port is not None else 0
def __hash__(self) -> int:
return hash(self.pattern)
- def __lt__(self, other: "URLMatcher") -> bool:
+ def __lt__(self, other: "URLPattern") -> bool:
return self.priority < other.priority
def __eq__(self, other: typing.Any) -> bool:
- return isinstance(other, URLMatcher) and self.pattern == other.pattern
+ return isinstance(other, URLPattern) and self.pattern == other.pattern
def warn_deprecated(message: str) -> None: # pragma: nocover
import pytest
import httpx
-from httpx._utils import URLMatcher
+from httpx._utils import URLPattern
def url_to_origin(url: str):
client = httpx.AsyncClient(proxies=proxies)
for proxy_key, url in expected_proxies:
- matcher = URLMatcher(proxy_key)
- assert matcher in client._proxies
- proxy = client._proxies[matcher]
+ pattern = URLPattern(proxy_key)
+ assert pattern in client._proxies
+ proxy = client._proxies[pattern]
assert isinstance(proxy, httpcore.AsyncHTTPProxy)
assert proxy.proxy_origin == url_to_origin(url)
from httpx._utils import (
ElapsedTimer,
NetRCInfo,
- URLMatcher,
+ URLPattern,
get_ca_bundle_from_env,
get_environment_proxies,
guess_json_utf,
],
)
def test_url_matches(pattern, url, expected):
- matcher = URLMatcher(pattern)
- assert matcher.matches(httpx.URL(url)) == expected
+ pattern = URLPattern(pattern)
+ assert pattern.matches(httpx.URL(url)) == expected
-def test_matcher_priority():
+def test_pattern_priority():
matchers = [
- URLMatcher("all://"),
- URLMatcher("http://"),
- URLMatcher("http://example.com"),
- URLMatcher("http://example.com:123"),
+ URLPattern("all://"),
+ URLPattern("http://"),
+ URLPattern("http://example.com"),
+ URLPattern("http://example.com:123"),
]
random.shuffle(matchers)
assert sorted(matchers) == [
- URLMatcher("http://example.com:123"),
- URLMatcher("http://example.com"),
- URLMatcher("http://"),
- URLMatcher("all://"),
+ URLPattern("http://example.com:123"),
+ URLPattern("http://example.com"),
+ URLPattern("http://"),
+ URLPattern("all://"),
]