Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions httpx/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import warnings
from collections.abc import MutableMapping
from http.cookiejar import Cookie, CookieJar
from urllib.parse import parse_qsl, urlencode
from urllib.parse import parse_qsl, quote, unquote, urlencode

import chardet
import rfc3986
Expand Down Expand Up @@ -54,6 +54,10 @@
)


def _quote(component: typing.Optional[str]) -> typing.Optional[str]:
return None if component is None else quote(component)


class URL:
def __init__(self, url: URLTypes = "", params: QueryParamTypes = None) -> None:
if isinstance(url, str):
Expand Down Expand Up @@ -94,12 +98,12 @@ def userinfo(self) -> str:
@property
def username(self) -> str:
userinfo = self._uri_reference.userinfo or ""
return userinfo.partition(":")[0]
return unquote(userinfo.partition(":")[0])

@property
def password(self) -> str:
userinfo = self._uri_reference.userinfo or ""
return userinfo.partition(":")[2]
return unquote(userinfo.partition(":")[2])

@property
def host(self) -> str:
Expand Down Expand Up @@ -169,8 +173,8 @@ def copy_with(self, **kwargs: typing.Any) -> "URL":
):
host = kwargs.pop("host", self.host)
port = kwargs.pop("port", self.port)
username = kwargs.pop("username", self.username)
password = kwargs.pop("password", self.password)
username = _quote(kwargs.pop("username", self.username))
password = _quote(kwargs.pop("password", self.password))

authority = host
if port is not None:
Expand Down
12 changes: 12 additions & 0 deletions tests/models/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,15 @@ def test_url_copywith_for_authority():
for k, v in copy_with_kwargs.items():
assert getattr(new, k) == v
assert str(new) == "https://username:password@example.net:444"


def test_url_copywith_for_userinfo():
copy_with_kwargs = {
"username": "tom@example.org",
"password": "abc123@ %",
}
url = URL("https://example.org")
new = url.copy_with(**copy_with_kwargs)
assert str(new) == "https://tom%40example.org:abc123%40%20%25@example.org"
assert new.username == "tom@example.org"
assert new.password == "abc123@ %"