API

cassette module

cassette.eject(exc_type=None, exc_value=None, tb=None)[source]

Remove cassette, unpatching HTTP requests.

cassette.insert(filename, file_format='')[source]

Setup cassette.

Parameters:filename – path to where requests and responses will be stored.
cassette.play(*args, **kwds)[source]

Use cassette.

cassette_library module

class cassette.cassette_library.CassetteLibrary(filename, encoder, config=None)[source]

The CassetteLibrary holds the stored requests and manage them.

This is an abstract class that needs to have several methods implemented. In addition, subclasses must store request/response pairs as a keys and values as a dictionary in the property self.data

Parameters:
  • filename (str) – filename to use when storing and replaying requests.
  • encoder (Encoder) – the instantiated encodeder to use
add_response(cassette_name, response)[source]

Add a new response to the mocked response.

Parameters:
  • cassette_name (str) –
  • response
cassette_name_for_httplib_connection(host, port, method, url, body, headers)[source]

Create a cassette name from an httplib request.

classmethod create_new_cassette_library(path, file_format, config=None)[source]

Return an instantiated CassetteLibrary.

Use this method to create new a CassetteLibrary. It will automatically determine if it should use a file or directory to back the cassette based on the filename. The method assumes that all file names with an extension (e.g. /file.json) are files, and all file names without extensions are directories (e.g. /requests).

Parameters:
  • path (str) – filename of file or directory for storing requests
  • file_format (str) – the file_format to use for storing requests
  • config (dict) – configuration
log_cassette_used(path)[source]

Log that a path was used.

report_unused_cassettes(output=<open file '<stdout>', mode 'w'>)[source]

Report unused path to a file.

rewind()[source]

Restore all responses to a re-seekable state.

save_to_cache(file_hash, data)[source]

Save a decoded data object into cache.

write_to_file()[source]

Write the response data to file.

class cassette.cassette_library.CassetteName[source]

A CassetteName represents an unique way to retrieve the cassette from the library.

classmethod from_httplib_connection(host, port, method, url, body, headers, will_hash_body=False)[source]

Create an object from an httplib request.

class cassette.cassette_library.DirectoryCassetteLibrary(*args, **kwargs)[source]

A CassetteLibrary that stores and manages requests with directory.

cassette_name_for_httplib_connection(host, port, method, url, body, headers)[source]

Create a cassette name from an httplib request.

generate_filename(cassette_name)[source]

Generate the filename for a given cassette name.

generate_path_from_cassette_name(cassette_name)[source]

Generate the full path to cassette file.

get_all_available()[source]

Return all available cassette.

write_to_file()[source]

Write mocked response to a directory of files.

class cassette.cassette_library.FileCassetteLibrary(filename, encoder, config=None)[source]

Store and manage requests with a single file.

data

Lazily loaded data.

get_all_available()[source]

Return all available cassette.

load_file()[source]

Load MockedResponses from YAML file.

write_to_file()[source]

Write mocked responses to file.

patcher module

cassette.patcher.patch(cassette_library)[source]

Replace standard library.

cassette.patcher.unpatch()[source]

Unpatch standard library.

unpatched module

cassette.unpatched.unpatched_httplib_context(*args, **kwds)[source]

Create a context in which httplib is unpatched.