1
0
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:
Kevin McCarthy
2014-05-03 18:35:24 -10:00
parent f479f205ad
commit 1f99ede46f
2 changed files with 18 additions and 21 deletions

View File

@@ -15,7 +15,7 @@ def convert_to_unicode(resp):
def convert_headers_to_bytes(resp):
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):
pass
return resp
@@ -23,7 +23,7 @@ def convert_headers_to_bytes(resp):
def convert_headers_to_unicode(resp):
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):
pass
return resp

View File

@@ -42,25 +42,22 @@ class VCRFakeSocket(object):
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):
if isinstance(header_list, dict):
return parse_headers_backwards_compat(header_list)
headers = b"".join(header_list) + b"\r\n"
return compat.get_httpmessage(headers)
"""
Convert headers from our serialized dict with lists for keys to a
HTTPMessage
"""
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):
@@ -246,7 +243,7 @@ class VCRConnection:
'code': response.status,
'message': response.reason
},
'headers': compat.get_headers(response),
'headers': serialize_headers(response),
'body': {'string': response.read()},
}
self.cassette.append(self._vcr_request, response)