Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle URL quoting username and password components. #1159

Merged
merged 5 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 6 additions & 6 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 @@ -100,12 +100,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 @@ -175,8 +175,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) or "")
password = quote(kwargs.pop("password", self.password) or "")

authority = host
if port is not None:
Expand All @@ -193,7 +193,7 @@ def copy_with(self, **kwargs: typing.Any) -> "URL":

def join(self, url: URLTypes) -> "URL":
"""
Return an absolute URL, using given this URL as the base.
Return an absolute URL, using this URL as the base.
"""
if self.is_relative_url:
return URL(url)
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 @@ -187,6 +187,18 @@ def test_url_copywith_for_authority():
assert str(new) == "https://username:[email protected]:444"


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


def test_url_invalid():
with pytest.raises(InvalidURL):
URL("https://😇/")