]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
lets try response headers as dicts
authorKevin McCarthy <me@kevinmccarthy.org>
Sun, 4 May 2014 04:35:24 +0000 (18:35 -1000)
committerKevin McCarthy <me@kevinmccarthy.org>
Sat, 10 May 2014 21:52:34 +0000 (11:52 -1000)
vcr/serializers/compat.py
vcr/stubs/__init__.py

index 489a5085e4f05d8d29c13860e7e8c1fd00b2d37f..30ffc9f21f242ea2b0f4bdc5edcd297aacaee24a 100644 (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
index 2d88da274f7e98bffc11932e555542ab9fe07580..dde5efd3a461e2d800a23d6e5e773d2c8e6ec9fb 100644 (file)
@@ -42,25 +42,22 @@ class VCRFakeSocket(object):
         return 0  # wonder how bad this is....
 
 
-def parse_headers_backwards_compat(header_dict):
+def parse_headers(header_list):
     """
-    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.
+    Convert headers from our serialized dict with lists for keys to a
+    HTTPMessage
     """
-    msg = HTTPMessage(BytesIO(""))
-    for key, val in header_dict.items():
-        msg.addheader(key, val)
-        msg.headers.append("{0}:{1}".format(key, val))
-    return msg
-
+    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 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)
+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)