Use HTTPHeaderDict for response headers.

This commit is contained in:
Allan Crooks
2017-09-10 16:11:24 +01:00
parent 95b127714b
commit 5aef6e7583
2 changed files with 14 additions and 3 deletions
+2 -2
View File
@@ -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)
+12 -1
View File
@@ -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: