API

cassette

class vcr.cassette.Cassette(path, serializer=<module 'vcr.serializers.yamlserializer' from '/home/docs/checkouts/readthedocs.org/user_builds/vcrpy/envs/v1.8.0/lib/python3.4/site-packages/vcrpy-1.8.0-py3.4.egg/vcr/serializers/yamlserializer.py'>, record_mode='once', match_on=(<function uri at 0x7f0bf9e23510>, <function method at 0x7f0bf9e730d0>), before_record_request=None, before_record_response=None, custom_patches=(), inject=False)

A container for recorded requests and responses

all_played

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

append(request, response)

Add a request, response pair to this cassette

classmethod load(**kwargs)

Instantiate and load the cassette stored at the specified path.

play_response(request)

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)

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)

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__.

filters

vcr.filters.decode_response(response)
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)

Wrap replace_headers() for API backward compatibility.

vcr.filters.remove_post_data_parameters(request, post_data_parameters_to_remove)

Wrap replace_post_data_parameters() for API backward compatibility.

vcr.filters.remove_query_parameters(request, query_parameters_to_remove)

Wrap replace_query_parameters() for API backward compatibility.

vcr.filters.replace_headers(request, replacements)

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)

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)

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)

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.

Futhermore, 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)

VCR’s representation of a request.

patch

Utilities for patching in cassettes