mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 01:03:24 +00:00
lets try response headers as dicts
This commit is contained in:
@@ -15,7 +15,7 @@ def convert_to_unicode(resp):
|
|||||||
|
|
||||||
def convert_headers_to_bytes(resp):
|
def convert_headers_to_bytes(resp):
|
||||||
try:
|
try:
|
||||||
resp['headers'] = [h.encode('utf-8') for h in resp['headers']]
|
resp['headers'] = dict([(k.encode('utf-8'), [v.encode('utf-8') for v in values]) for k, values in resp['headers'].items()])
|
||||||
except (KeyError, TypeError):
|
except (KeyError, TypeError):
|
||||||
pass
|
pass
|
||||||
return resp
|
return resp
|
||||||
@@ -23,7 +23,7 @@ def convert_headers_to_bytes(resp):
|
|||||||
|
|
||||||
def convert_headers_to_unicode(resp):
|
def convert_headers_to_unicode(resp):
|
||||||
try:
|
try:
|
||||||
resp['headers'] = [h.decode('utf-8') for h in resp['headers']]
|
resp['headers'] = dict([(k.decode('utf-8'), [v.decode('utf-8') for v in values]) for k, values in resp['headers'].items()])
|
||||||
except (KeyError, TypeError):
|
except (KeyError, TypeError):
|
||||||
pass
|
pass
|
||||||
return resp
|
return resp
|
||||||
|
|||||||
@@ -42,25 +42,22 @@ class VCRFakeSocket(object):
|
|||||||
return 0 # wonder how bad this is....
|
return 0 # wonder how bad this is....
|
||||||
|
|
||||||
|
|
||||||
def parse_headers_backwards_compat(header_dict):
|
|
||||||
"""
|
|
||||||
In vcr 0.6.0, I changed the cassettes to store
|
|
||||||
headers as a list instead of a dict. This method
|
|
||||||
parses the old dictionary-style headers for
|
|
||||||
backwards-compatability reasons.
|
|
||||||
"""
|
|
||||||
msg = HTTPMessage(BytesIO(""))
|
|
||||||
for key, val in header_dict.items():
|
|
||||||
msg.addheader(key, val)
|
|
||||||
msg.headers.append("{0}:{1}".format(key, val))
|
|
||||||
return msg
|
|
||||||
|
|
||||||
|
|
||||||
def parse_headers(header_list):
|
def parse_headers(header_list):
|
||||||
if isinstance(header_list, dict):
|
"""
|
||||||
return parse_headers_backwards_compat(header_list)
|
Convert headers from our serialized dict with lists for keys to a
|
||||||
headers = b"".join(header_list) + b"\r\n"
|
HTTPMessage
|
||||||
return compat.get_httpmessage(headers)
|
"""
|
||||||
|
header_string = b""
|
||||||
|
for k, v in header_list.items():
|
||||||
|
for v in v:
|
||||||
|
header_string += k.encode('utf-8') + b":" + v.encode('utf-8') + b"\r\n"
|
||||||
|
return compat.get_httpmessage(header_string)
|
||||||
|
|
||||||
|
def serialize_headers(response):
|
||||||
|
out = {}
|
||||||
|
for k, v in response.getheaders():
|
||||||
|
out.setdefault(k, []).append(v)
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
class VCRHTTPResponse(HTTPResponse):
|
class VCRHTTPResponse(HTTPResponse):
|
||||||
@@ -246,7 +243,7 @@ class VCRConnection:
|
|||||||
'code': response.status,
|
'code': response.status,
|
||||||
'message': response.reason
|
'message': response.reason
|
||||||
},
|
},
|
||||||
'headers': compat.get_headers(response),
|
'headers': serialize_headers(response),
|
||||||
'body': {'string': response.read()},
|
'body': {'string': response.read()},
|
||||||
}
|
}
|
||||||
self.cassette.append(self._vcr_request, response)
|
self.cassette.append(self._vcr_request, response)
|
||||||
|
|||||||
Reference in New Issue
Block a user