]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
fix migration script
authorKevin McCarthy <me@kevinmccarthy.org>
Mon, 5 May 2014 03:35:45 +0000 (17:35 -1000)
committerKevin McCarthy <me@kevinmccarthy.org>
Sat, 10 May 2014 21:52:35 +0000 (11:52 -1000)
tests/fixtures/migration/new_cassette.json
tests/fixtures/migration/new_cassette.yaml
vcr/migration.py

index ea7b4c2fcccf4534e2e6ebfb9b5f522d5dad13ae..c23262ea84221789ef62892eadd4cbe7535bdcb4 100644 (file)
                 "code": 200
             }, 
             "headers": {
-                "Access-Control-Allow-Origin": ["*"],
-                "Content-Type": ["application/json"],
-                "Date": ["Mon, 21 Apr 2014 23:13:40 GMT"],
-                "Server": ["gunicorn/0.17.4"],
-                "Content-Length": ["32"],
-                "Connection": ["keep-alive"]
+                "access-control-allow-origin": ["*"],
+                "content-type": ["application/json"],
+                "date": ["Mon, 21 Apr 2014 23:13:40 GMT"],
+                "server": ["gunicorn/0.17.4"],
+                "content-length": ["32"],
+                "connection": ["keep-alive"]
             }, 
             "body": {
                 "string": "{\n  \"origin\": \"217.122.164.194\"\n}"
index ea3745991f7e4574cd03eef8bbe219a6fddc89ed..49a4e1a036c8a2a6dca66cb404b48fdf12c3fd5a 100644 (file)
@@ -9,10 +9,10 @@
   response:
     body: {string: !!python/unicode "{\n  \"origin\": \"217.122.164.194\"\n}"}
     headers:
-      Access-Control-Allow-Origin: ['*']
-      Content-Type: [application/json]
-      Date: ['Mon, 21 Apr 2014 23:06:09 GMT']
-      Server: [gunicorn/0.17.4]
-      Content-Length: ['32']
-      Connection: [keep-alive]
+      access-control-allow-origin: ['*']
+      content-type: [application/json]
+      date: ['Mon, 21 Apr 2014 23:06:09 GMT']
+      server: [gunicorn/0.17.4]
+      content-length: ['32']
+      connection: [keep-alive]
     status: {code: 200, message: OK}
index edb3b9161a288895c96b16e50131e8e554b37bd8..e9387ea197b0bf8f191499530dc413928840879c 100644 (file)
@@ -21,6 +21,7 @@ import yaml
 
 from .serializers import compat, yamlserializer
 from . import request
+from stubs.compat import get_httpmessage
 
 # Use the libYAML versions if possible
 try:
@@ -49,12 +50,20 @@ def migrate_json(in_fp, out_fp):
     data = json.load(in_fp)
     for item in data:
         req = item['request']
+        res = item['response']
         uri = dict((k, req.pop(k)) for k in PARTS)
         req['uri'] = build_uri(**uri)
         # convert headers to dict of lists
         headers = req['headers']
         for k in headers:
             headers[k] = [headers[k]]
+        response_headers = {}
+        for k, v in get_httpmessage(b"".join(res['headers'])).items():
+            response_headers.setdefault(k, [])
+            response_headers[k].append(v)
+        res['headers'] = response_headers
+
+
     json.dump(data, out_fp, indent=4)
 
 
@@ -84,7 +93,7 @@ def _old_deserialize(cassette_string):
 
 def migrate_yml(in_fp, out_fp):
     (requests, responses) = _old_deserialize(in_fp.read())
-    for req in requests:
+    for req, res in zip(requests, responses):
         if not isinstance(req, request.Request):
             raise Exception("already migrated")
         else:
@@ -100,6 +109,11 @@ def migrate_yml(in_fp, out_fp):
             req.headers = {}
             for key, value in headers:
                 req.add_header(key, value)
+            response_headers = {}
+            for k, v in get_httpmessage(b"".join(res['headers'])).items():
+                response_headers.setdefault(k, [])
+                response_headers[k].append(v)
+            res['headers'] = response_headers
 
     data = yamlserializer.serialize({
         "requests": requests,
@@ -122,7 +136,7 @@ def migrate(file_path, migration_fn):
 def try_migrate(path):
     try:  # try to migrate as json
         migrate(path, migrate_json)
-    except Exception:  # probably the file is not a json
+    except Exception as e:  # probably the file is not a json
         try:  # let's try to migrate as yaml
             migrate(path, migrate_yml)
         except Exception:  # oops probably the file is not a cassette