"""
url = self.base_url.join(relative_url=url)
if url.scheme == "http" and hstspreload.in_hsts_preload(url.host):
- url = url.copy_with(scheme="https")
+ port = None if url.port == 80 else url.port
+ url = url.copy_with(scheme="https", port=port)
return url
def merge_cookies(
@property
def authority(self) -> str:
+ port_str = self._uri_reference.port
+ default_port_str = {"https": "443", "http": "80"}.get(self.scheme, "")
+ if port_str is None or port_str == default_port_str:
+ return self._uri_reference.host or ""
return self._uri_reference.authority or ""
@property
authority = host
if port is not None:
authority += f":{port}"
- if username is not None:
+ if username:
userpass = username
- if password is not None:
+ if password:
userpass += f":{password}"
authority = f"{userpass}@{authority}"
@pytest.mark.asyncio
-async def test_host_without_auth_in_header():
+async def test_host_with_auth_and_port_in_url():
+ """
+ The Host header should only include the hostname, or hostname:port
+ (for non-default ports only). Any userinfo or default port should not
+ be present.
+ """
url = "http://username:password@example.org:80/echo_headers"
client = Client(dispatch=MockDispatch())
"accept": "*/*",
"accept-encoding": "gzip, deflate, br",
"connection": "keep-alive",
- "host": "example.org:80",
+ "host": "example.org",
+ "user-agent": f"python-httpx/{__version__}",
+ "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
+ }
+ }
+
+
+@pytest.mark.asyncio
+async def test_host_with_non_default_port_in_url():
+ """
+ If the URL includes a non-default port, then it should be included in
+ the Host header.
+ """
+ url = "http://username:password@example.org:123/echo_headers"
+
+ client = Client(dispatch=MockDispatch())
+ response = await client.get(url)
+
+ assert response.status_code == 200
+ assert response.json() == {
+ "headers": {
+ "accept": "*/*",
+ "accept-encoding": "gzip, deflate, br",
+ "connection": "keep-alive",
+ "host": "example.org:123",
"user-agent": f"python-httpx/{__version__}",
"authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
}
repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')"
)
- new = url.copy_with(scheme="http")
- assert new == URL("http://example.org:123/path/to/somewhere?abc=123#anchor")
+ new = url.copy_with(scheme="http", port=None)
+ assert new == URL("http://example.org/path/to/somewhere?abc=123#anchor")
assert new.scheme == "http"