Skip to content
Myr

adding string casting for non-bytes values

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Nate Prewitt · committed by Nate Prewitt · parent 11a86a5 · Browse files · 2 files+27 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+27 added−2 removed

  • tests:+11 added−0 removed
  • other:+16 added−2 removed

2 files+27 added−2 removed

modified:requests/auth.py+16 added-2 removed
13 unmodified lines
14import threading1516from base64 import b64encode1718from .compat import urlparse, str18from .compat import urlparse, str, basestring19from .cookies import extract_cookies_to_jar20from ._internal_utils import to_native_string21from .utils import parse_dict_header22from .status_codes import codes
3 unmodified lines
262728def _basic_auth_str(username, password):29 """Returns a Basic Auth string."""30 3031 # "I want us to put a big-ol' comment on top of it that32 # says that this behaviour is dumb but we need to preserve33 # it because people are relying on it."34 # - Lukasa35 #36 # These are here solely to maintain backwards compatibility37 # for things like ints. This will be removed in 3.0.0.38 if not isinstance(username, basestring):39 username = str(username)4041 if not isinstance(password, basestring):42 password = str(password)43 # -- End Removal --4445 if isinstance(username, str):46 username = username.encode('latin1')4748 if isinstance(password, str):
225 unmodified lines
modified:tests/test_requests.py+11 added
482 unmodified lines
483 @pytest.mark.parametrize(484 'username, password', (485 ('user', 'pass'),486 (u'имя'.encode('utf-8'), u'пароль'.encode('utf-8')),487 (42, 42)488 ))489 def test_set_basicauth(self, httpbin, username, password):490 auth = (username, password)491 url = httpbin('get')
2 unmodified lines
494 p = r.prepare()495496 assert p.headers['Authorization'] == _basic_auth_str(username, password)497498 def test_basicauth_encodes_byte_strings(self):499 """Ensure b'test' formats as the byte string "test" rather500 than the unicode string "b'test'" in Python 3.501 """502 auth = (b'\xc5\xafsername', b'test\xc6\xb6')503 r = requests.Request('GET', 'http://localhost', auth=auth)504 p = r.prepare()505506 assert p.headers['Authorization'] == 'Basic xa9zZXJuYW1lOnRlc3TGtg=='507508 @pytest.mark.parametrize(509 'url, exception', (510 # Connecting to an unknown domain should raise a ConnectionError511 ('http://doesnotexist.google.com', ConnectionError),
1784 unmodified lines