Coverage for vcr.cassette : 80%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
"""The container for recorded requests and responses"""
except ImportError: from backport_collections import Counter
# Internal imports
"""Context manager/decorator that handles installing the cassette and removing cassettes.
This class defers the creation of a new cassette instance until the point at 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 from interfering with another. """
def from_args(cls, cassette_class, path, **kwargs): return cls(cassette_class, lambda: (path, kwargs))
# TODO(@IvanMalison): Hmmm. it kind of feels like this should be # somewhere else.
def __call__(self, function, instance, args, kwargs): return function(cassette, *args, **kwargs) else:
"""A container for recorded requests and responses"""
def load(cls, path, **kwargs): """Instantiate and load the cassette stored at the specified path."""
def use_arg_getter(cls, arg_getter):
def use(cls, *args, **kwargs): return CassetteContextDecorator.from_args(cls, *args, **kwargs)
match_on=(uri, method), before_record_request=None, before_record_response=None, custom_patches=(), inject=False):
# self.data is the list of (req, resp) tuples
def play_count(self): return sum(self.play_counts.values())
def all_played(self): """Returns True if all responses have been played, False otherwise.""" return self.play_count == len(self)
def requests(self): return [request for (request, response) in self.data]
def responses(self): return [response for (request, response) in self.data]
def write_protected(self): return self.rewound and self.record_mode == 'once' or \ self.record_mode == 'none'
"""Add a request, response pair to this cassette""" return
return self._before_record_request(request)
""" internal API, returns an iterator with all responses matching the request. """
self.record_mode != 'all' and \ self.rewound
""" Get the response corresponding to a request, but only if it hasn't been played back before, and mark it as played """ # The cassette doesn't contain the request asked for. raise UnhandledHTTPRequestError( "The cassette (%r) doesn't contain the request (%r) asked for" % (self._path, request) )
""" Find the responses corresponding to a request. This function isn't actually used by VCR internally, but is provided as an external API. """ responses = [response for index, response in self._responses(request)]
if responses: return responses # The cassette doesn't contain the request asked for. raise UnhandledHTTPRequestError( "The cassette (%r) doesn't contain the request (%r) asked for" % (self._path, request) )
return {"requests": self.requests, "responses": self.responses}
save_cassette( self._path, self._as_dict(), serializer=self._serializer ) self.dirty = False
self._path, serializer=self._serializer ) except IOError: pass
return "<Cassette containing {0} recorded response(s)>".format( len(self) )
"""Return the number of request,response pairs stored in here""" return len(self.data)
"""Return whether or not a request has been stored""" return False |