1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-10 09:35:34 +00:00

fix migration script

This commit is contained in:
Kevin McCarthy
2014-05-04 17:35:45 -10:00
parent bc45a965b2
commit 23b5d49736
3 changed files with 28 additions and 14 deletions

View File

@@ -16,12 +16,12 @@
"code": 200 "code": 200
}, },
"headers": { "headers": {
"Access-Control-Allow-Origin": ["*"], "access-control-allow-origin": ["*"],
"Content-Type": ["application/json"], "content-type": ["application/json"],
"Date": ["Mon, 21 Apr 2014 23:13:40 GMT"], "date": ["Mon, 21 Apr 2014 23:13:40 GMT"],
"Server": ["gunicorn/0.17.4"], "server": ["gunicorn/0.17.4"],
"Content-Length": ["32"], "content-length": ["32"],
"Connection": ["keep-alive"] "connection": ["keep-alive"]
}, },
"body": { "body": {
"string": "{\n \"origin\": \"217.122.164.194\"\n}" "string": "{\n \"origin\": \"217.122.164.194\"\n}"

View File

@@ -9,10 +9,10 @@
response: response:
body: {string: !!python/unicode "{\n \"origin\": \"217.122.164.194\"\n}"} body: {string: !!python/unicode "{\n \"origin\": \"217.122.164.194\"\n}"}
headers: headers:
Access-Control-Allow-Origin: ['*'] access-control-allow-origin: ['*']
Content-Type: [application/json] content-type: [application/json]
Date: ['Mon, 21 Apr 2014 23:06:09 GMT'] date: ['Mon, 21 Apr 2014 23:06:09 GMT']
Server: [gunicorn/0.17.4] server: [gunicorn/0.17.4]
Content-Length: ['32'] content-length: ['32']
Connection: [keep-alive] connection: [keep-alive]
status: {code: 200, message: OK} status: {code: 200, message: OK}

View File

@@ -21,6 +21,7 @@ import yaml
from .serializers import compat, yamlserializer from .serializers import compat, yamlserializer
from . import request from . import request
from stubs.compat import get_httpmessage
# Use the libYAML versions if possible # Use the libYAML versions if possible
try: try:
@@ -49,12 +50,20 @@ def migrate_json(in_fp, out_fp):
data = json.load(in_fp) data = json.load(in_fp)
for item in data: for item in data:
req = item['request'] req = item['request']
res = item['response']
uri = dict((k, req.pop(k)) for k in PARTS) uri = dict((k, req.pop(k)) for k in PARTS)
req['uri'] = build_uri(**uri) req['uri'] = build_uri(**uri)
# convert headers to dict of lists # convert headers to dict of lists
headers = req['headers'] headers = req['headers']
for k in headers: for k in headers:
headers[k] = [headers[k]] 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) json.dump(data, out_fp, indent=4)
@@ -84,7 +93,7 @@ def _old_deserialize(cassette_string):
def migrate_yml(in_fp, out_fp): def migrate_yml(in_fp, out_fp):
(requests, responses) = _old_deserialize(in_fp.read()) (requests, responses) = _old_deserialize(in_fp.read())
for req in requests: for req, res in zip(requests, responses):
if not isinstance(req, request.Request): if not isinstance(req, request.Request):
raise Exception("already migrated") raise Exception("already migrated")
else: else:
@@ -100,6 +109,11 @@ def migrate_yml(in_fp, out_fp):
req.headers = {} req.headers = {}
for key, value in headers: for key, value in headers:
req.add_header(key, value) 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({ data = yamlserializer.serialize({
"requests": requests, "requests": requests,
@@ -122,7 +136,7 @@ def migrate(file_path, migration_fn):
def try_migrate(path): def try_migrate(path):
try: # try to migrate as json try: # try to migrate as json
migrate(path, migrate_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 try: # let's try to migrate as yaml
migrate(path, migrate_yml) migrate(path, migrate_yml)
except Exception: # oops probably the file is not a cassette except Exception: # oops probably the file is not a cassette