1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-08 16:53:23 +00:00

update serializers

This commit is contained in:
Kevin McCarthy
2014-05-06 19:20:42 -10:00
parent 2c33ae2664
commit b43c63f284
5 changed files with 24 additions and 24 deletions

View File

@@ -410,6 +410,16 @@ single cassette.
*Note*: Back up your cassettes files before migration.
The migration *should* only modify cassettes using the old 0.x format.
## New serializer / deserializer API
If you made a custom serializer, you will need to update it to match the new
API in version 1.0.x
* Serializers now take dicts and return strings.
* Deserializers take strings and return dicts (instead of requests, responses
pair)
## Changelog
* 1.0.0 (in development) - _BACKWARDS INCOMPATIBLE_: Please see the 'upgrade'
section in the README. Add support for filtering sensitive data from

View File

@@ -1,3 +1,5 @@
version: 1
interactions:
- request:
body: null
headers:

View File

@@ -10,7 +10,7 @@ class MockSerializer(object):
def deserialize(self, cassette_string):
self.serialize_count += 1
self.cassette_string = cassette_string
return []
return {'interactions':[]}
def serialize(self, cassette_dict):
self.deserialize_count += 1

View File

@@ -1,30 +1,12 @@
import tempfile
from .persisters.filesystem import FilesystemPersister
from .serialize import serialize, deserialize
def _check_for_old_cassette(cassette_content):
# crate tmp with cassete content and try to migrate it
with tempfile.NamedTemporaryFile(mode='w+') as f:
f.write(cassette_content)
f.seek(0)
if migration.try_migrate(f.name):
raise ValueError(
"Your cassette files were generated in an older version "
"of VCR. Delete your cassettes or run the migration script."
"See http://git.io/mHhLBg for more details."
)
def load_cassette(cassette_path, serializer):
with open(cassette_path) as f:
cassette_content = f.read()
try:
cassette = deserialize(cassette_content, serializer)
except TypeError:
_check_for_old_cassette(cassette_content)
raise
cassette = deserialize(cassette_content, serializer)
return cassette

View File

@@ -1,6 +1,9 @@
from vcr.serializers import compat
from vcr.request import Request
#version 1 cassettes started with VCR 1.0.x. Before 1.0.x, there was no versioning.
CASSETTE_FORMAT_VERSION = 1
"""
Just a general note on the serialization philosophy here:
I prefer cassettes to be human-readable if possible. Yaml serializes
@@ -16,18 +19,21 @@ Deserializing: string (yaml converts from utf-8) -> bytestring
def deserialize(cassette_string, serializer):
data = serializer.deserialize(cassette_string)
requests = [Request._from_dict(r['request']) for r in data]
responses = [r['response'] for r in data]
responses = [compat.convert_to_bytes(r['response']) for r in data]
requests = [Request._from_dict(r['request']) for r in data['interactions']]
responses = [compat.convert_to_bytes(r['response']) for r in data['interactions']]
return requests, responses
def serialize(cassette_dict, serializer):
data = ([{
interactions = ([{
'request': request._to_dict(),
'response': compat.convert_to_unicode(response),
} for request, response in zip(
cassette_dict['requests'],
cassette_dict['responses'],
)])
data = {
'version': CASSETTE_FORMAT_VERSION,
'interactions': interactions,
}
return serializer.serialize(data)