remove allowance of non-string/bytes auth values

This commit is contained in:
Nate Prewitt
2016-12-09 08:21:42 -07:00
parent c7e5c0d2b7
commit 500dc75c8c
2 changed files with 16 additions and 27 deletions
+4 -25
View File
@@ -12,7 +12,6 @@ import re
import time
import hashlib
import threading
import warnings
from base64 import b64encode
@@ -29,33 +28,13 @@ CONTENT_TYPE_MULTI_PART = 'multipart/form-data'
def _basic_auth_str(username, password):
"""Returns a Basic Auth string."""
# "I want us to put a big-ol' comment on top of it that
# says that this behaviour is dumb but we need to preserve
# it because people are relying on it."
# - Lukasa
#
# These are here solely to maintain backwards compatibility
# for things like ints. This will be removed in 3.0.0.
if not isinstance(username, basestring):
warnings.warn(
"Non-string usernames will no longer be supported in Requests "
"3.0.0. Please convert the object you've passed in ({!r}) to "
"a string or bytes object in the near future to avoid "
"problems.".format(username),
category=DeprecationWarning,
)
username = str(username)
raise TypeError('username must be of type str or bytes, '
'instead it was %s' % type(username))
if not isinstance(password, basestring):
warnings.warn(
"Non-string passwords will no longer be supported in Requests "
"3.0.0. Please convert the object you've passed in ({!r}) to "
"a string or bytes object in the near future to avoid "
"problems.".format(password),
category=DeprecationWarning,
)
password = str(password)
# -- End Removal --
raise TypeError('password must be of type str or bytes, '
'instead it was %s' % type(password))
if isinstance(username, str):
username = username.encode('latin1')
+12 -2
View File
@@ -514,8 +514,6 @@ class TestRequests:
'username, password', (
('user', 'pass'),
(u'имя'.encode('utf-8'), u'пароль'.encode('utf-8')),
(42, 42),
(None, None),
))
def test_set_basicauth(self, httpbin, username, password):
auth = (username, password)
@@ -526,6 +524,18 @@ class TestRequests:
assert p.headers['Authorization'] == _basic_auth_str(username, password)
@pytest.mark.parametrize(
'username, password', (
('user', 1234),
(None, 'test'),
))
def test_non_str_basicauth(self, username, password):
"""Ensure we only allow string or bytes values for basicauth"""
with pytest.raises(TypeError) as e:
requests.auth._basic_auth_str(username, password)
assert 'must be of type str or bytes' in str(e)
def test_basicauth_encodes_byte_strings(self):
"""Ensure b'test' formats as the byte string "test" rather
than the unicode string "b'test'" in Python 3.