diff --git a/requests/structures.py b/requests/structures.py index 935b0b3f..1e6e49ab 100644 --- a/requests/structures.py +++ b/requests/structures.py @@ -96,7 +96,7 @@ class HTTPHeaderDict(CaseInsensitiveDict): self.extend({} if data is None else data, **kwargs) - # + # # We'll store tuples in the internal dictionary, but present them as a # concatenated string when we use item access methods. # diff --git a/requests/utils.py b/requests/utils.py index 28a6977a..e7189ef6 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -51,7 +51,7 @@ if platform.system() == 'Windows': # provide a proxy_bypass version on Windows without DNS lookups def proxy_bypass_registry(host: str) -> bool: - import winreg # typing: ignore + import winreg # typing: ignore try: internetSettings = winreg.OpenKey( @@ -103,10 +103,15 @@ if platform.system() == 'Windows': return proxy_bypass_registry(host) -def dict_to_sequence(d: dict) -> typing.Union[typing.Optional[typing.ItemsView[typing.Any, typing.Any]], dict]: +def dict_to_sequence( + d: dict +) -> typing.Union[ + typing.Optional[typing.ItemsView[typing.Any, typing.Any]], dict +]: """Returns an internal sequence dictionary update.""" if hasattr(d, 'items'): return d.items() + return d @@ -208,7 +213,6 @@ def get_netrc_auth( # AppEngine hackiness. except (ImportError, AttributeError): pass - return None @@ -249,7 +253,9 @@ def from_key_val_list(value): return collections.OrderedDict(value) -def to_key_val_list(value) -> typing.List[typing.Tuple[typing.Text, typing.Text]]: +def to_key_val_list( + value +) -> typing.List[typing.Tuple[typing.Text, typing.Text]]: """Take an object and test to see if it can be represented as a dictionary. If it can be, return a list of tuples, e.g., @@ -334,7 +340,7 @@ def parse_dict_header(value) -> dict: :return: :class:`dict` :rtype: dict """ - result = {} # type: dict + result = {} # type: dict for item in _parse_list_header(value): if '=' not in item: result[item] = None @@ -600,7 +606,9 @@ def is_valid_cidr(string_network: str) -> bool: @contextlib.contextmanager -def set_environ(env_name: str, value: typing.Optional[str]) -> typing.Generator: +def set_environ( + env_name: str, value: typing.Optional[str] +) -> typing.Generator: """Set the environment variable 'env_name' to 'value' Save previous value, yield, and then restore the previous value stored in @@ -681,7 +689,10 @@ def get_environ_proxies( return getproxies() -def select_proxy(url: str, proxies: typing.Optional[typing.MutableMapping[typing.Text, typing.Text]]): +def select_proxy( + url: str, + proxies: typing.Optional[typing.MutableMapping[typing.Text, typing.Text]], +): """Select a proxy for the url, if applicable. :param url: The url being for the request @@ -737,7 +748,7 @@ def parse_header_links(value: str) -> typing.List[typing.MutableMapping]: :rtype: list """ - links = [] # type: typing.List + links = [] # type: typing.List replace_chars = ' \'"' value = value.strip(replace_chars) if not value: @@ -854,7 +865,9 @@ _CLEAN_HEADER_REGEX_BYTE = re.compile(b'^\\S[^\\r\\n]*$|^$') _CLEAN_HEADER_REGEX_STR = re.compile(r'^\S[^\r\n]*$|^$') -def check_header_validity(header: typing.Tuple[typing.Text, typing.Text]) -> None: +def check_header_validity( + header: typing.Tuple[typing.Text, typing.Text] +) -> None: """Verifies that header value is a string which doesn't contain leading whitespace or return characters. This prevents unintended header injection. diff --git a/tests/test_requests.py b/tests/test_requests.py index b70bdc50..4606ef5c 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -174,13 +174,13 @@ class TestRequests: prep = session.prepare_request(request) assert prep.url == 'http://example.com/?z=1&a=1&k=1&d=1' + # def test_params_bytes_are_encoded(self): # request = requests.Request( # 'GET', 'http://example.com', params=b'test=foo' # ).prepare( # ) # assert request.url == 'http://example.com/?test=foo' - def test_binary_put(self): request = requests.Request( 'PUT', 'http://example.com', data=u"ööö".encode("utf-8") @@ -650,6 +650,7 @@ class TestRequests: 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. @@ -658,7 +659,6 @@ class TestRequests: # r = requests.Request('GET', 'http://localhost', auth=auth) # p = r.prepare() # assert p.headers['Authorization'] == 'Basic xa9zZXJuYW1lOnRlc3TGtg==' - @pytest.mark.parametrize( 'url, exception', (('http://doesnotexist.google.com', ConnectionError), ('http://localhost:1', ConnectionError), ('http://fe80::5054:ff:fe5a:fc0', InvalidURL)), @@ -1119,12 +1119,12 @@ class TestRequests: 'Dummy-Auth-Test' ] == 'dummy-auth-test-ok' + # def test_prepare_request_with_bytestring_url(self): # req = requests.Request('GET', b'https://httpbin.org/') # s = requests.Session() # prep = s.prepare_request(req) # assert prep.url == "https://httpbin.org/" - # def test_request_with_bytestring_host(self, httpbin): # s = requests.Session() # resp = s.request( @@ -1134,7 +1134,6 @@ class TestRequests: # headers={'Host': b'httpbin.org'}, # ) # assert resp.cookies.get('cookie') == 'value' - def test_links(self): r = requests.Response() r.headers = { @@ -1633,10 +1632,7 @@ class TestRequests: def test_header_validation(self, httpbin): """Ensure prepare_headers regex isn't flagging valid header contents.""" headers_ok = { - 'foo': 'bar baz qux', - 'bar': 'fbbq', - 'baz': '', - 'qux': '1', + 'foo': 'bar baz qux', 'bar': 'fbbq', 'baz': '', 'qux': '1' } r = requests.get(httpbin('get'), headers=headers_ok) assert r.request.headers['foo'] == headers_ok['foo']