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=(), filter_nested_post_data_parameters=False, 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, drop_unused_requests=False, filter_sensitive_data=(), before_save_interaction=None, after_load_interaction=None, skip_patchers=())[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=(), filter_nested_post_data_parameters=False, 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, drop_unused_requests=False, filter_sensitive_data=(), before_save_interaction=None, after_load_interaction=None, skip_patchers=())[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, drop_unused_requests=False, filter_sensitive_data=(), before_save_interaction=None, after_load_interaction=None, skip_patchers=())[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, drop_unused_requests=False, filter_sensitive_data=(), before_save_interaction=None, after_load_interaction=None, skip_patchers=())[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.
- get_metadata(key, default)[source]¶
Return the value stored in the cassette under key.
If the cassette has no such value, store default and return it, or raise MissingMetadataError if the record mode doesn’t allow changing the cassette.
- property path¶
The path the cassette is saved to and loaded from.
- 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
CassetteContextDecoratorinstances 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 any supported compression (gzip, deflate, br if available):
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_body_parameters(request, replacements, recursive=False)[source]¶
Replace body parameters 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.
If recursive is true, keys are also replaced in objects nested in JSON and dict bodies, including objects in lists.
- 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, recursive=False)[source]¶
Wrap replace_body_parameters() for API backward compatibility: only filters POST requests.
- 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.
- vcr.filters.replace_text(obj, text, replacement)[source]¶
Return a copy of obj with every occurrence of text replaced by replacement.
Replaces in strings and bytes (encoded as UTF-8), and in the keys and values of dicts and the items of lists and tuples, at any depth. Anything else is returned unchanged.
- vcr.filters.sensitive_data_hooks(sensitive_data)[source]¶
Build the interaction hooks that implement
filter_sensitive_data.sensitive_datamaps placeholders to the values they stand for, as a dict or as (placeholder, value) pairs. A value can be a function of the interaction’s request and response. Returns(hide, restore):hidereplaces values with their placeholders, forbefore_save_interaction, andrestoreputs the values back, forafter_load_interaction.
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.
- property body¶
- property body_bytes¶
The body materialized as a single
bytesobject (orNone).Used when forwarding to a real server so
http.clientsets aContent-Lengthinstead of chunk-encoding a file-like/iterator body (which some servers, including the wsgiref-based test server, reject).
- property headers¶
- property host¶
- property path¶
- property port¶
- property protocol¶
- property query¶
- property scheme¶
- property uri¶
- property url¶
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