From 5aef6e7583d8aa77aa847b68dcd753772b375435 Mon Sep 17 00:00:00 2001 From: Allan Crooks Date: Sun, 10 Sep 2017 16:11:24 +0100 Subject: [PATCH] Use HTTPHeaderDict for response headers. --- requests/adapters.py | 4 ++-- tests/test_requests.py | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/requests/adapters.py b/requests/adapters.py index b2c1d4e3..7c883552 100644 --- a/requests/adapters.py +++ b/requests/adapters.py @@ -31,7 +31,7 @@ from .compat import urlparse, basestring from .utils import (DEFAULT_CA_BUNDLE_PATH, get_encoding_from_headers, prepend_scheme_if_needed, get_auth_from_url, urldefragauth, select_proxy) -from .structures import CaseInsensitiveDict +from .structures import HTTPHeaderDict from .cookies import extract_cookies_to_jar from .exceptions import (ConnectionError, ConnectTimeout, ReadTimeout, SSLError, ProxyError, RetryError, InvalidScheme) @@ -274,7 +274,7 @@ class HTTPAdapter(BaseAdapter): response.status_code = getattr(resp, 'status', None) # Make headers case-insensitive. - response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {})) + response.headers = HTTPHeaderDict(getattr(resp, 'headers', {})) # Set encoding. response.encoding = get_encoding_from_headers(response.headers) diff --git a/tests/test_requests.py b/tests/test_requests.py index 8aa0af0d..8f5fc796 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -2186,6 +2186,12 @@ class TestRequests: fruits = resp.headers['fruit'] assert fruits == 'Apple, Blood Orange, Banana, Berry, Blue' + # As we are using HTTPHeaderDict, we should be able to extract the + # individual header values too. + assert resp.headers.multiget('fruit') == ( + 'Apple', 'Blood Orange', 'Banana', 'Berry, Blue' + ) + def test_multiple_response_headers_with_same_name_diff_case(self, httpbin): # urllib3 seems to have trouble guaranteeing the order of the items when # the case is different, so we just need to make sure all of the items @@ -2196,9 +2202,14 @@ class TestRequests: # These are all possible acceptable combinations for the header. fruit_choices = ['Apple', 'Blood Orange', 'Banana', 'Berry, Blue'] fruit_permutations = itertools.permutations(fruit_choices) - fruit_headers = set(', '.join(fp) for fp in fruit_permutations) + fruit_multiheaders = set(tuple(fp) for fp in fruit_permutations) + fruit_headers = set(', '.join(fp) for fp in fruit_multiheaders) assert resp.headers['fruit'] in fruit_headers + # As we are using HTTPHeaderDict, we should be able to extract the + # individual header values too. + assert resp.headers.multiget('fruit') in fruit_multiheaders + class TestCaseInsensitiveDict: