Skip to content
Myr

URL.port becomes Optional[int] (#1080)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* URL.port becomes Optional[int], not int * Minor _transport_for_url refactor * Add docstring

authored by Tom Christie · committed by GitHub · parent 696c1ef · Browse files · 8 files+42 added−24 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+42 added−24 removed

  • tests:+21 added−9 removed
  • other:+21 added−15 removed

8 files+42 added−24 removed

modified:httpx/_client.py+8 added-8 removed
541 unmodified lines
542 """543 enforce_http_url(url)544545 if self._proxies and not should_not_be_proxied(url):546 is_default_port = (url.scheme == "http" and url.port == 80) or (547 url.scheme == "https" and url.port == 443548 )549 hostname = f"{url.host}:{url.port}"546 default_port = {"http": 80, "https": 443}[url.scheme]547 is_default_port = url.port is None or url.port == default_port548 port = url.port or default_port549 hostname = f"{url.host}:{port}"550 proxy_keys = (551 f"{url.scheme}://{hostname}",552 f"{url.scheme}://{url.host}" if is_default_port else None,553 f"all://{hostname}",
521 unmodified lines
1075 """1076 enforce_http_url(url)10771078 if self._proxies and not should_not_be_proxied(url):1079 is_default_port = (url.scheme == "http" and url.port == 80) or (1080 url.scheme == "https" and url.port == 4431081 )1082 hostname = f"{url.host}:{url.port}"1079 default_port = {"http": 80, "https": 443}[url.scheme]1080 is_default_port = url.port is None or url.port == default_port1081 port = url.port or default_port1082 hostname = f"{url.host}:{port}"1083 proxy_keys = (1084 f"{url.scheme}://{hostname}",1085 f"{url.scheme}://{url.host}" if is_default_port else None,1086 f"all://{hostname}",
422 unmodified lines
modified:httpx/_models.py+4 added-6 removed
103 unmodified lines
104 def host(self) -> str:105 return self._uri_reference.host or ""106107 @property108 def port(self) -> int:108 def port(self) -> typing.Optional[int]:109 port = self._uri_reference.port110 if port is None:111 return {"https": 443, "http": 80}[self.scheme]112 return int(port)110 return int(port) if port else None111112 @property113 def path(self) -> str:114 return self._uri_reference.path or "/"
13 unmodified lines
128 def fragment(self) -> str:129 return self._uri_reference.fragment or ""130131 @property134 def raw(self) -> typing.Tuple[bytes, bytes, int, bytes]:132 def raw(self) -> typing.Tuple[bytes, bytes, typing.Optional[int], bytes]:133 return (134 self.scheme.encode("ascii"),135 self.host.encode("ascii"),136 self.port,
27 unmodified lines
164 or "host" in kwargs165 or "port" in kwargs166 ):167 host = kwargs.pop("host", self.host)170 port = kwargs.pop("port", None if self.is_relative_url else self.port)168 port = kwargs.pop("port", self.port)169 username = kwargs.pop("username", self.username)170 password = kwargs.pop("password", self.password)171172 authority = host
963 unmodified lines
modified:httpx/_utils.py+9 added-1 removed
271 unmodified lines
272 if url.scheme not in ("http", "https"):273 raise InvalidURL('URL scheme must be "http" or "https".')274275276def port_or_default(url: "URL") -> typing.Optional[int]:277 if url.port is not None:278 return url.port279 return {"http": 80, "https": 443}.get(url.scheme)280281282def same_origin(url: "URL", other: "URL") -> bool:283 """284 Return 'True' if the given URLs share the same origin.285 """286 return (281 url.scheme == other.scheme and url.host == other.host and url.port == other.port287 url.scheme == other.scheme288 and url.host == other.host289 and port_or_default(url) == port_or_default(other)290 )291292293def should_not_be_proxied(url: "URL") -> bool:
136 unmodified lines
modified:tests/client/test_proxies.py+15 added-3 removed
2 unmodified lines
34import httpx567def url_to_origin(url: str):8 """9 Given a URL string, return the origin in the raw tuple format that10 `httpcore` uses for it's representation.11 """12 DEFAULT_PORTS = {b"http": 80, b"https": 443}13 scheme, host, explicit_port = httpx.URL(url).raw[:3]14 default_port = DEFAULT_PORTS[scheme]15 port = default_port if explicit_port is None else explicit_port16 return scheme, host, port171819@pytest.mark.parametrize(20 ["proxies", "expected_proxies"],21 [22 ("http://127.0.0.1", [("all", "http://127.0.0.1")]),
15 unmodified lines
38 for proxy_key, url in expected_proxies:39 assert proxy_key in client._proxies40 proxy = client._proxies[proxy_key]41 assert isinstance(proxy, httpcore.AsyncHTTPProxy)30 assert proxy.proxy_origin == httpx.URL(url).raw[:3]42 assert proxy.proxy_origin == url_to_origin(url)4344 assert len(expected_proxies) == len(client._proxies)4546
49 unmodified lines
96 if expected is None:97 assert transport is client._transport98 else:99 assert isinstance(transport, httpcore.AsyncHTTPProxy)88 assert transport.proxy_origin == httpx.URL(expected).raw[:3]100 assert transport.proxy_origin == url_to_origin(expected)101102103@pytest.mark.asyncio104async def test_async_proxy_close():
37 unmodified lines
142143 if expected is None:144 assert transport == client._transport145 else:134 assert transport.proxy_origin == httpx.URL(expected).raw[:3]146 assert transport.proxy_origin == url_to_origin(expected)
modified:tests/client/test_redirects.py+1 added-1 removed
240 unmodified lines
241 # https://github.com/encode/httpx/issues/771242 client = AsyncClient(transport=AsyncMockTransport())243 response = await client.get("http://example.org/malformed_redirect")244 assert response.status_code == codes.OK245 assert response.url == URL("https://example.org/")245 assert response.url == URL("https://example.org:443/")246 assert len(response.history) == 1247248249@pytest.mark.usefixtures("async_environment")
237 unmodified lines
modified:tests/models/test_requests.py+2 added-2 removed
100 unmodified lines
101def test_url():102 url = "http://example.org"103 request = httpx.Request("GET", url)104 assert request.url.scheme == "http"105 assert request.url.port == 80105 assert request.url.port is None106 assert request.url.full_path == "/"107108 url = "https://example.org/abc?foo=bar"109 request = httpx.Request("GET", url)110 assert request.url.scheme == "https"111 assert request.url.port == 443111 assert request.url.port is None112 assert request.url.full_path == "/abc?foo=bar"
modified:tests/models/test_url.py+2 added-2 removed
16 unmodified lines
17 "http://Königsgäßchen.de",18 "http://xn--knigsgchen-b4a3dun.de",19 "xn--knigsgchen-b4a3dun.de",20 "http",21 80,21 None,22 ),23 ("https://faß.de", "https://xn--fa-hia.de", "xn--fa-hia.de", "https", 443),23 ("https://faß.de", "https://xn--fa-hia.de", "xn--fa-hia.de", "https", None),24 (25 "https://βόλος.com:443",26 "https://xn--nxasmm1c.com:443",27 "xn--nxasmm1c.com",
160 unmodified lines
modified:tests/test_config.py+1 added-1 removed
212 unmodified lines
213 [214 ("https://example.com", "https://example.com", {}, "DEFAULT"),215 (216 "https://user:pass@example.com",217 "https://example.com:443",217 "https://example.com",218 {"proxy-authorization": "Basic dXNlcjpwYXNz"},219 "DEFAULT",220 ),221 ],
20 unmodified lines