1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-09 01:03:24 +00:00

Random tweaks.

This commit is contained in:
Ivan Malison
2014-10-05 18:37:01 -07:00
parent e9f35db405
commit a9a68ba44b
3 changed files with 25 additions and 19 deletions

View File

@@ -1,4 +1,4 @@
'''The container for recorded requests and responses''' """The container for recorded requests and responses"""
import logging import logging
import contextlib2 import contextlib2
@@ -24,7 +24,7 @@ class CassetteContextDecorator(object):
removing cassettes. removing cassettes.
This class defers the creation of a new cassette instance until the point at This class defers the creation of a new cassette instance until the point at
which it is installned by context manager or decorator. The fact that a new which it is installed by context manager or decorator. The fact that a new
cassette is used with each application prevents the state of any cassette cassette is used with each application prevents the state of any cassette
from interfering with another. from interfering with another.
""" """
@@ -45,7 +45,8 @@ class CassetteContextDecorator(object):
log.debug('Entered context for cassette at {0}.'.format(cassette._path)) log.debug('Entered context for cassette at {0}.'.format(cassette._path))
yield cassette yield cassette
log.debug('Exiting context for cassette at {0}.'.format(cassette._path)) log.debug('Exiting context for cassette at {0}.'.format(cassette._path))
# TODO(@IvanMalison): Hmmm. it kind of feels like this should be somewhere else. # TODO(@IvanMalison): Hmmm. it kind of feels like this should be
# somewhere else.
cassette._save() cassette._save()
def __enter__(self): def __enter__(self):
@@ -65,11 +66,11 @@ class CassetteContextDecorator(object):
class Cassette(object): class Cassette(object):
'''A container for recorded requests and responses''' """A container for recorded requests and responses"""
@classmethod @classmethod
def load(cls, path, **kwargs): def load(cls, path, **kwargs):
'''Instantiate and load the cassette stored at the specified path.''' """Instantiate and load the cassette stored at the specified path."""
new_cassette = cls(path, **kwargs) new_cassette = cls(path, **kwargs)
new_cassette._load() new_cassette._load()
return new_cassette return new_cassette
@@ -85,7 +86,8 @@ class Cassette(object):
def __init__(self, path, serializer=yamlserializer, record_mode='once', def __init__(self, path, serializer=yamlserializer, record_mode='once',
match_on=(uri, method), filter_headers=(), match_on=(uri, method), filter_headers=(),
filter_query_parameters=(), before_record_request=None, filter_query_parameters=(), before_record_request=None,
before_record_response=None, ignore_hosts=(), ignore_localhost=()): before_record_response=None, ignore_hosts=(),
ignore_localhost=()):
self._path = path self._path = path
self._serializer = serializer self._serializer = serializer
self._match_on = match_on self._match_on = match_on
@@ -105,9 +107,7 @@ class Cassette(object):
@property @property
def all_played(self): def all_played(self):
""" """Returns True if all responses have been played, False otherwise."""
Returns True if all responses have been played, False otherwise.
"""
return self.play_count == len(self) return self.play_count == len(self)
@property @property
@@ -124,7 +124,7 @@ class Cassette(object):
self.record_mode == 'none' self.record_mode == 'none'
def append(self, request, response): def append(self, request, response):
'''Add a request, response pair to this cassette''' """Add a request, response pair to this cassette"""
request = self._before_record_request(request) request = self._before_record_request(request)
if not request: if not request:
return return
@@ -153,10 +153,10 @@ class Cassette(object):
self.rewound self.rewound
def play_response(self, request): def play_response(self, request):
''' """
Get the response corresponding to a request, but only if it Get the response corresponding to a request, but only if it
hasn't been played back before, and mark it as played hasn't been played back before, and mark it as played
''' """
for index, response in self._responses(request): for index, response in self._responses(request):
if self.play_counts[index] == 0: if self.play_counts[index] == 0:
self.play_counts[index] += 1 self.play_counts[index] += 1
@@ -168,11 +168,11 @@ class Cassette(object):
) )
def responses_of(self, request): def responses_of(self, request):
''' """
Find the responses corresponding to a request. Find the responses corresponding to a request.
This function isn't actually used by VCR internally, but is This function isn't actually used by VCR internally, but is
provided as an external API. provided as an external API.
''' """
responses = [response for index, response in self._responses(request)] responses = [response for index, response in self._responses(request)]
if responses: if responses:
@@ -214,11 +214,11 @@ class Cassette(object):
) )
def __len__(self): def __len__(self):
'''Return the number of request,response pairs stored in here''' """Return the number of request,response pairs stored in here"""
return len(self.data) return len(self.data)
def __contains__(self, request): def __contains__(self, request):
'''Return whether or not a request has been stored''' """Return whether or not a request has been stored"""
for response in self._responses(request): for response in self._responses(request):
return True return True
return False return False

View File

@@ -59,7 +59,9 @@ class CassettePatcherBuilder(object):
def _build_patchers_from_mock_triples_decorator(function): def _build_patchers_from_mock_triples_decorator(function):
@functools.wraps(function) @functools.wraps(function)
def wrapped(self, *args, **kwargs): def wrapped(self, *args, **kwargs):
return self._build_patchers_from_mock_triples(function(self, *args, **kwargs)) return self._build_patchers_from_mock_triples(
function(self, *args, **kwargs)
)
return wrapped return wrapped
def __init__(self, cassette): def __init__(self, cassette):

View File

@@ -217,11 +217,15 @@ class VCRConnection(object):
response = self.cassette.play_response(self._vcr_request) response = self.cassette.play_response(self._vcr_request)
return VCRHTTPResponse(response) return VCRHTTPResponse(response)
else: else:
if self.cassette.write_protected and self.cassette.filter_request(self._vcr_request): if self.cassette.write_protected and self.cassette.filter_request(
self._vcr_request
):
raise CannotOverwriteExistingCassetteException( raise CannotOverwriteExistingCassetteException(
"No match for the request (%r) was found. "
"Can't overwrite existing cassette (%r) in " "Can't overwrite existing cassette (%r) in "
"your current record mode (%r)." "your current record mode (%r)."
% (self.cassette._path, self.cassette.record_mode) % (self._vcr_request, self.cassette._path,
self.cassette.record_mode)
) )
# Otherwise, we should send the request, then get the response # Otherwise, we should send the request, then get the response