]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
Store Headers as a List
authorKevin McCarthy <me@kevinmccarthy.org>
Sun, 8 Dec 2013 01:50:30 +0000 (15:50 -1000)
committerKevin McCarthy <me@kevinmccarthy.org>
Mon, 16 Dec 2013 02:56:39 +0000 (16:56 -1000)
This is a backwards-incompatible change that will store headers
as a list rather than a dictionary.  The reason being that you can
have multiple values for a single header, and concatenating them
together with commas can create an unparseable string (sometimes
the header values can have commas in them)

tests/integration/test_wild.py
vcr/stubs/__init__.py

index 13f6caa842cfba51b0b945f3581185816b2d0b85..0c4c2d9ab32965154d4d1a5fbf3ad139cb7f5e4b 100644 (file)
@@ -52,3 +52,20 @@ def test_flickr_should_respond_with_200(tmpdir):
     with vcr.use_cassette(testfile):
         r = requests.post("http://api.flickr.com/services/upload")
         assert r.status_code == 200
+
+def x_test_zip_file(tmpdir):
+    # TODO: How do I make this pass?
+    zipfile = "http://www.colorado.edu/conflict/peace/download/peace_example.ZIP"
+    testfile = str(tmpdir.join('test.json'))
+    with vcr.use_cassette(testfile, serializer='json'):
+        r = requests.post(zipfile)
+
+def test_cookies(tmpdir):
+    testfile = str(tmpdir.join('cookies.yml'))
+    with vcr.use_cassette(testfile):
+        s = requests.Session()
+        r1 = s.get("http://httpbin.org/cookies/set?k1=v1&k2=v2")
+        r2 = s.get("http://httpbin.org/cookies")
+        assert len(r2.json()['cookies']) == 2
+
+
index 9871069713340c417636f650b424fd0806876f02..801a93cc5a7f6de4a9c9613c5266d3ace78674ba 100644 (file)
@@ -6,6 +6,14 @@ from cStringIO import StringIO
 from vcr.request import Request
 
 
+def parse_headers(header_list):
+    headers = "".join(header_list) + "\r\n"
+    msg = HTTPMessage(StringIO(headers))
+    msg.fp.seek(0)
+    msg.readheaders()
+    return msg
+
+
 class VCRHTTPResponse(object):
     """
     Stub reponse class that gets returned instead of a HTTPResponse
@@ -18,17 +26,8 @@ class VCRHTTPResponse(object):
         self._content = StringIO(self.recorded_response['body']['string'])
         self.closed = False
 
-        # We are skipping the header parsing (they have already been parsed
-        # at this point) and directly  adding the headers to the header
-        # container, so just pass an empty StringIO.
-        self.msg = HTTPMessage(StringIO(''))
-
-        for key, val in self.recorded_response['headers'].iteritems():
-            self.msg.addheader(key, val)
-            # msg.addheaders adds the headers to msg.dict, but not to
-            # the msg.headers list representation of headers, so
-            # I have to add it to both.
-            self.msg.headers.append("{0}:{1}".format(key, val))
+        headers = self.recorded_response['headers']
+        self.msg = parse_headers(headers)
 
         self.length = self.msg.getheader('content-length') or None
 
@@ -47,7 +46,8 @@ class VCRHTTPResponse(object):
         return self.closed
 
     def getheaders(self):
-        return self.recorded_response['headers'].iteritems()
+        headers = parse_headers(self.recorded_response['headers'])
+        return headers.dict.iteritems()
 
 
 class VCRConnectionMixin:
@@ -201,7 +201,7 @@ class VCRConnectionMixin:
                     'code': response.status,
                     'message': response.reason
                 },
-                'headers': dict(response.getheaders()),
+                'headers': response.msg.headers,
                 'body': {'string': response.read()},
             }
             self.cassette.append(self._vcr_request, response)