return Origin(self)
def copy_with(self, **kwargs: typing.Any) -> "URL":
+ if (
+ "username" in kwargs
+ or "password" in kwargs
+ or "host" in kwargs
+ or "port" in kwargs
+ ):
+ host = kwargs.pop("host", self.host)
+ port = kwargs.pop("port", self.port)
+ username = kwargs.pop("username", self.username)
+ password = kwargs.pop("password", self.password)
+
+ authority = host
+ if port is not None:
+ authority += f":{port}"
+ if username is not None:
+ userpass = username
+ if password is not None:
+ userpass += f":{password}"
+ authority = f"{userpass}@{authority}"
+
+ kwargs["authority"] = authority
+
return URL(self._uri_reference.copy_with(**kwargs).unsplit())
def join(self, relative_url: URLTypes) -> "URL":
assert origin.is_ssl
assert origin.host == "example.com"
assert origin.port == 443
+
+
+def test_url_copywith_for_authority():
+ copy_with_kwargs = {
+ "username": "username",
+ "password": "password",
+ "port": 444,
+ "host": "example.net",
+ }
+ url = URL("https://example.org")
+ new = url.copy_with(**copy_with_kwargs)
+ for k, v in copy_with_kwargs.items():
+ assert getattr(new, k) == v
+ assert str(new) == "https://username:password@example.net:444"