enforce_http_url(url)
if self._proxies and not should_not_be_proxied(url):
- is_default_port = (url.scheme == "http" and url.port == 80) or (
- url.scheme == "https" and url.port == 443
- )
- hostname = f"{url.host}:{url.port}"
+ default_port = {"http": 80, "https": 443}[url.scheme]
+ is_default_port = url.port is None or url.port == default_port
+ port = url.port or default_port
+ hostname = f"{url.host}:{port}"
proxy_keys = (
f"{url.scheme}://{hostname}",
f"{url.scheme}://{url.host}" if is_default_port else None,
enforce_http_url(url)
if self._proxies and not should_not_be_proxied(url):
- is_default_port = (url.scheme == "http" and url.port == 80) or (
- url.scheme == "https" and url.port == 443
- )
- hostname = f"{url.host}:{url.port}"
+ default_port = {"http": 80, "https": 443}[url.scheme]
+ is_default_port = url.port is None or url.port == default_port
+ port = url.port or default_port
+ hostname = f"{url.host}:{port}"
proxy_keys = (
f"{url.scheme}://{hostname}",
f"{url.scheme}://{url.host}" if is_default_port else None,
return self._uri_reference.host or ""
@property
- def port(self) -> int:
+ def port(self) -> typing.Optional[int]:
port = self._uri_reference.port
- if port is None:
- return {"https": 443, "http": 80}[self.scheme]
- return int(port)
+ return int(port) if port else None
@property
def path(self) -> str:
return self._uri_reference.fragment or ""
@property
- def raw(self) -> typing.Tuple[bytes, bytes, int, bytes]:
+ def raw(self) -> typing.Tuple[bytes, bytes, typing.Optional[int], bytes]:
return (
self.scheme.encode("ascii"),
self.host.encode("ascii"),
or "port" in kwargs
):
host = kwargs.pop("host", self.host)
- port = kwargs.pop("port", None if self.is_relative_url else self.port)
+ port = kwargs.pop("port", self.port)
username = kwargs.pop("username", self.username)
password = kwargs.pop("password", self.password)
raise InvalidURL('URL scheme must be "http" or "https".')
+def port_or_default(url: "URL") -> typing.Optional[int]:
+ if url.port is not None:
+ return url.port
+ return {"http": 80, "https": 443}.get(url.scheme)
+
+
def same_origin(url: "URL", other: "URL") -> bool:
"""
Return 'True' if the given URLs share the same origin.
"""
return (
- url.scheme == other.scheme and url.host == other.host and url.port == other.port
+ url.scheme == other.scheme
+ and url.host == other.host
+ and port_or_default(url) == port_or_default(other)
)
import httpx
+def url_to_origin(url: str):
+ """
+ Given a URL string, return the origin in the raw tuple format that
+ `httpcore` uses for it's representation.
+ """
+ DEFAULT_PORTS = {b"http": 80, b"https": 443}
+ scheme, host, explicit_port = httpx.URL(url).raw[:3]
+ default_port = DEFAULT_PORTS[scheme]
+ port = default_port if explicit_port is None else explicit_port
+ return scheme, host, port
+
+
@pytest.mark.parametrize(
["proxies", "expected_proxies"],
[
assert proxy_key in client._proxies
proxy = client._proxies[proxy_key]
assert isinstance(proxy, httpcore.AsyncHTTPProxy)
- assert proxy.proxy_origin == httpx.URL(url).raw[:3]
+ assert proxy.proxy_origin == url_to_origin(url)
assert len(expected_proxies) == len(client._proxies)
assert transport is client._transport
else:
assert isinstance(transport, httpcore.AsyncHTTPProxy)
- assert transport.proxy_origin == httpx.URL(expected).raw[:3]
+ assert transport.proxy_origin == url_to_origin(expected)
@pytest.mark.asyncio
if expected is None:
assert transport == client._transport
else:
- assert transport.proxy_origin == httpx.URL(expected).raw[:3]
+ assert transport.proxy_origin == url_to_origin(expected)
client = AsyncClient(transport=AsyncMockTransport())
response = await client.get("http://example.org/malformed_redirect")
assert response.status_code == codes.OK
- assert response.url == URL("https://example.org/")
+ assert response.url == URL("https://example.org:443/")
assert len(response.history) == 1
url = "http://example.org"
request = httpx.Request("GET", url)
assert request.url.scheme == "http"
- assert request.url.port == 80
+ assert request.url.port is None
assert request.url.full_path == "/"
url = "https://example.org/abc?foo=bar"
request = httpx.Request("GET", url)
assert request.url.scheme == "https"
- assert request.url.port == 443
+ assert request.url.port is None
assert request.url.full_path == "/abc?foo=bar"
"http://xn--knigsgchen-b4a3dun.de",
"xn--knigsgchen-b4a3dun.de",
"http",
- 80,
+ None,
),
- ("https://faß.de", "https://xn--fa-hia.de", "xn--fa-hia.de", "https", 443),
+ ("https://faß.de", "https://xn--fa-hia.de", "xn--fa-hia.de", "https", None),
(
"https://βόλος.com:443",
"https://xn--nxasmm1c.com:443",
("https://example.com", "https://example.com", {}, "DEFAULT"),
(
"https://user:pass@example.com",
- "https://example.com:443",
+ "https://example.com",
{"proxy-authorization": "Basic dXNlcjpwYXNz"},
"DEFAULT",
),