API

cassette

class vcr.cassette.Cassette(path, serializer=None, persister=None, record_mode='once', match_on=(<function uri>, <function method>), before_record_request=None, before_record_response=None, custom_patches=(), inject=False)[source]

A container for recorded requests and responses

__init__(path, serializer=None, persister=None, record_mode='once', match_on=(<function uri>, <function method>), before_record_request=None, before_record_response=None, custom_patches=(), inject=False)[source]

Initialize self. See help(type(self)) for accurate signature.

all_played

Returns True if all responses have been played, False otherwise.

append(request, response)[source]

Add a request, response pair to this cassette

find_requests_with_most_matches(request)[source]

Get the most similar request(s) stored in the cassette of a given request as a list of tuples like this: - the request object - the successful matchers as string - the failed matchers and the related assertion message with the difference details as strings tuple

This is useful when a request failed to be found, we can get the similar request(s) in order to know what have changed in the request parts.

classmethod load(**kwargs)[source]

Instantiate and load the cassette stored at the specified path.

play_response(request)[source]

Get the response corresponding to a request, but only if it hasn’t been played back before, and mark it as played

responses_of(request)[source]

Find the responses corresponding to a request. This function isn’t actually used by VCR internally, but is provided as an external API.

class vcr.cassette.CassetteContextDecorator(cls, args_getter)[source]

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.

Instances of this class are NOT reentrant as context managers. However, functions that are decorated by CassetteContextDecorator instances ARE reentrant. See the implementation of __call__ on this class for more details. There is also a guard against attempts to reenter instances of this class as a context manager in __exit__.

__init__(cls, args_getter)[source]

Initialize self. See help(type(self)) for accurate signature.

matchers

vcr.matchers.get_assertion_message(assertion_details)[source]

Get a detailed message about the failing matcher.

vcr.matchers.get_matchers_results(r1, r2, matchers)[source]

Get the comparison results of two requests as two list. The first returned list represents the matchers names that passed. The second list is the failed matchers as a string with failed assertion details if any.

filters

vcr.filters.decode_response(response)[source]
If the response is compressed with gzip or deflate:
  1. decompress the response body
  2. delete the content-encoding header
  3. update content-length header to decompressed length
vcr.filters.remove_headers(request, headers_to_remove)[source]

Wrap replace_headers() for API backward compatibility.

vcr.filters.remove_post_data_parameters(request, post_data_parameters_to_remove)[source]

Wrap replace_post_data_parameters() for API backward compatibility.

vcr.filters.remove_query_parameters(request, query_parameters_to_remove)[source]

Wrap replace_query_parameters() for API backward compatibility.

vcr.filters.replace_headers(request, replacements)[source]

Replace headers in request according to replacements. The replacements should be a list of (key, value) pairs where the value can be any of:

  1. A simple replacement string value.
  2. None to remove the given header.
  3. A callable which accepts (key, value, request) and returns a string value or None.
vcr.filters.replace_post_data_parameters(request, replacements)[source]

Replace post data in request–either form data or json–according to replacements. The replacements should be a list of (key, value) pairs where the value can be any of:

  1. A simple replacement string value.
  2. None to remove the given header.
  3. A callable which accepts (key, value, request) and returns a string value or None.
vcr.filters.replace_query_parameters(request, replacements)[source]

Replace query parameters in request according to replacements. The replacements should be a list of (key, value) pairs where the value can be any of:

  1. A simple replacement string value.
  2. None to remove the given header.
  3. A callable which accepts (key, value, request) and returns a string value or None.

request

class vcr.request.HeadersDict(data=None, **kwargs)[source]

There is a weird quirk in HTTP. You can send the same header twice. For this reason, headers are represented by a dict, with lists as the values. However, it appears that HTTPlib is completely incapable of sending the same header twice. This puts me in a weird position: I want to be able to accurately represent HTTP headers in cassettes, but I don’t want the extra step of always having to do [0] in the general case, i.e. request.headers[‘key’][0]

In addition, some servers sometimes send the same header more than once, and httplib can deal with this situation.

Furthermore, I wanted to keep the request and response cassette format as similar as possible.

For this reason, in cassettes I keep a dict with lists as keys, but once deserialized into VCR, I keep them as plain, naked dicts.

class vcr.request.Request(method, uri, body, headers)[source]

VCR’s representation of a request.

__init__(method, uri, body, headers)[source]

Initialize self. See help(type(self)) for accurate signature.

serialize

vcr.serialize.CASSETTE_FORMAT_VERSION = 1

Just a general note on the serialization philosophy here: I prefer cassettes to be human-readable if possible. Yaml serializes bytestrings to !!binary, which isn’t readable, so I would like to serialize to strings and from strings, which yaml will encode as utf-8 automatically. All the internal HTTP stuff expects bytestrings, so this whole serialization process feels backwards.

Serializing: bytestring -> string (yaml persists to utf-8) Deserializing: string (yaml converts from utf-8) -> bytestring

patch

Utilities for patching in cassettes