API¶
config
¶
- class vcr.config.VCR(path_transformer=None, before_record_request=None, custom_patches=(), filter_query_parameters=(), ignore_hosts=(), record_mode=RecordMode.ONCE, ignore_localhost=False, filter_headers=(), before_record_response=None, filter_post_data_parameters=(), match_on=('method', 'scheme', 'host', 'port', 'path', 'query'), before_record=None, inject_cassette=False, serializer='yaml', cassette_library_dir=None, func_path_generator=None, decode_compressed_response=False, record_on_exception=True)[source]¶
- __init__(path_transformer=None, before_record_request=None, custom_patches=(), filter_query_parameters=(), ignore_hosts=(), record_mode=RecordMode.ONCE, ignore_localhost=False, filter_headers=(), before_record_response=None, filter_post_data_parameters=(), match_on=('method', 'scheme', 'host', 'port', 'path', 'query'), before_record=None, inject_cassette=False, serializer='yaml', cassette_library_dir=None, func_path_generator=None, decode_compressed_response=False, record_on_exception=True)[source]¶
cassette
¶
- class vcr.cassette.Cassette(path, serializer=None, persister=None, record_mode=RecordMode.ONCE, match_on=(<function uri>, <function method>), before_record_request=None, before_record_response=None, custom_patches=(), inject=False, allow_playback_repeats=False)[source]¶
A container for recorded requests and responses
- __init__(path, serializer=None, persister=None, record_mode=RecordMode.ONCE, match_on=(<function uri>, <function method>), before_record_request=None, before_record_response=None, custom_patches=(), inject=False, allow_playback_repeats=False)[source]¶
- property all_played¶
Returns True if all responses have been played, False otherwise.
- 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.
- property play_count¶
- 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
- property requests¶
- property responses¶
- 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.
- property write_protected¶
- 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__
.
matchers
¶
- vcr.matchers.get_assertion_message(assertion_details)[source]¶
Get a detailed message about the failing matcher.
filters
¶
- vcr.filters.decode_response(response)[source]¶
- If the response is compressed with gzip or deflate:
decompress the response body
delete the content-encoding header
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:
A simple replacement string value.
None to remove the given header.
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:
A simple replacement string value.
None to remove the given header.
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.
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