Skip to content
Closed
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
4 changes: 2 additions & 2 deletions src/requests/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ def _basic_auth_str(username, password):
# -- End Removal --

if isinstance(username, str):
username = username.encode("latin1")
username = username.encode("utf-8")

if isinstance(password, str):
password = password.encode("latin1")
password = password.encode("utf-8")

authstr = "Basic " + to_native_string(
b64encode(b":".join((username, password))).strip()
Expand Down
36 changes: 32 additions & 4 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2216,6 +2216,36 @@ def get_redirect_target(self, resp):
assert not r.history[1].is_redirect
assert r.url == urls_test[2]

def test_http_digest_auth_non_latin_credentials(self, httpbin):
auth = HTTPDigestAuth("Сергей_Ласточкин", "1234")
url = httpbin("digest-auth", "auth", "Сергей_Ласточкин", "1234", "MD5", "never")

r = requests.get(url, auth=auth)
assert r.status_code == 200

r = requests.get(url)
assert r.status_code == 401

s = requests.session()
s.auth = HTTPDigestAuth("Сергей_Ласточкин", "1234")
r = s.get(url)
assert r.status_code == 200

def test_http_digest_auth_non_latin_credentials_with_bytes(self, httpbin):
auth = HTTPDigestAuth("Сергей_Ласточкин".encode("utf-8"), "1234".encode("utf-8"))
url = httpbin("digest-auth", "auth", "Сергей_Ласточкин", "1234", "MD5", "never")

r = requests.get(url, auth=auth)
assert r.status_code == 200

r = requests.get(url)
assert r.status_code == 401

s = requests.session()
s.auth = HTTPDigestAuth("Сергей_Ласточкин".encode("utf-8"), "1234".encode("utf-8"))
r = s.get(url)
assert r.status_code == 200


class TestCaseInsensitiveDict:
@pytest.mark.parametrize(
Expand Down Expand Up @@ -2713,10 +2743,8 @@ def test_redirecting_to_bad_url(self, httpbin, url, exception):
@pytest.mark.parametrize(
"input, expected",
(
(
b"http+unix://%2Fvar%2Frun%2Fsocket/path%7E",
"http+unix://%2Fvar%2Frun%2Fsocket/path~",
),
b"http+unix://%2Fvar%2Frun%2Fsocket/path%7E",
"http+unix://%2Fvar%2Frun%2Fsocket/path~",
(
"http+unix://%2Fvar%2Frun%2Fsocket/path%7E",
"http+unix://%2Fvar%2Frun%2Fsocket/path~",
Expand Down