betamax

Betamax is a VCR imitation for requests. This will make mocking out requests
much easier. It is tested on Travis CI.

Put in a more humorous way: “Betamax records your HTTP interactions so the NSA
does not have to.”

Example Use

frombetamaximportBetamaxfromrequestsimportSessionfromunittestimportTestCasewithBetamax.configure()asconfig:config.cassette_library_dir='tests/fixtures/cassettes'classTestGitHubAPI(TestCase):defsetUp(self):self.session=Session()self.headers.update(...)# Set the cassette in a line other than the context declarationdeftest_user(self):withBetamax(self.session)asvcr:vcr.use_cassette('user')resp=self.session.get('https://api.github.com/user',auth=('user','pass'))assertresp.json()['login']isnotNone# Set the cassette in line with the context declarationdeftest_repo(self):withBetamax(self.session).use_cassette('repo'):resp=self.session.get('https://api.github.com/repos/sigmavirus24/github3.py')assertresp.json()['owner']!={}

What does it even do?

If you are unfamiliar with VCR, you might need a better explanation of what
Betamax does.

Betamax intercepts every request you make and attempts to find a matching
request that has already been intercepted and recorded. Two things can then
happen:

If there is a matching request, it will return the response that is
associated with it.

If there is not a matching request and it is allowed to record new
responses, it will make the request, record the response and return the
response.

Recorded requests and corresponding responses - also known as interactions -
are stored in files called cassettes. (An example cassette can be seen in
the examples section of the documentation.) The directory you store your
cassettes in is called your library, or your cassette library.

VCR Cassette Compatibility

Betamax can use any VCR-recorded cassette as of this point in time. The only
caveat is that python-requests returns a URL on each response. VCR does not
store that in a cassette now but we will. Any VCR-recorded cassette used to
playback a response will unfortunately not have a URL attribute on responses
that are returned. This is a minor annoyance but not something that can be
fixed.

Contributing

You can check out the project board on waffle.io to see what the status of
each issue is.

History

0.8.0 - 2016-08-16

Add betamax_parametrized_recorder and betamax_parametrized_session
to our list of pytest fixtures so that users will have parametrized cassette
names when writing parametrized tests with our fixtures. (I wonder if I can
mention parametrization a bunch more times so I can say parametrize a lot in
this bullet note.)

Add ValidationError and a set of subclasses for each possible validation
error.

Raise a subclass of ValidationError when a particular cassette option is
invalid, rather than silently ignoring the validation failure.

0.7.2 - 2016-08-04

Fix bug with query string matcher where query-strings without values (e.g.,
?foo&bar as opposed to ?foo=1&bar=2) were treated as if there were
no query string.

0.7.1 - 2016-06-14

Fix issue #108 by effectively copying the items in the match_requests_on
list into the match_options set on a Cassette instance

0.7.0 - 2016-04-29

Add before_record and before_playback hooks

Allow per-cassette placeholders to be merged and override global
placeholders

Fix bug where the QueryMatcher failed matching on high Unicode points

0.6.0 - 2016-04-12

Add betamax_recorder pytest fixture

Change default behaviour to allow duplicate interactions to be recorded in
single cassette

Add allow_playback_repeats to allow an interaction to be used more than
once from a single cassette

Always return a new Response object from an Interaction to allow for a
streaming response to be usable multiple times

Remove CI support for Pythons 2.6 and 3.2

0.5.1 - 2015-10-24

Fix bugs with requests 2.8.x integration

Fix bugs with older versions of requests that were missing an HTTPHeaderDict
implementation

0.5.0 - 2015-07-15

Add unittest integration in betamax.fixtures.unittest

Add pytest integration in betamax.fixtures.pytest

Add a decorator as a short cut for use_cassette

Fix bug where body bytes were not always encoded on Python 3.2+

Fixed by @bboe

0.4.2 - 2015-04-18

Fix issue #58 reported by @bboe

Multiple cookies were not being properly stored or replayed after being
recorded.

@leighlondon converted __all__ to a tuple

0.4.1 - 2014-09-24

Fix issue #39 reported by @buttscicles

This bug did not properly parse the Set-Cookie header with multiple cookies
when replaying a recorded response.

0.4.0 - 2014-07-29

Allow the user to pass placeholders to Betamax#use_cassette.

Include Betamax’s version number in cassettes

0.3.2 - 2014-06-05

Fix request and response bodies courtesy of @dgouldin

0.3.1 - 2014-05-28

Fix GitHub Issue #35 - Placeholders were not being properly applied to
request bodies. This release fixes that so placeholders are now behave as
expected with recorded request bodies.

0.3.0 - 2014-05-23

Add Betamax#start and Betamax#stop to allow users to start recording
and stop without using a context-manager.

Add digest-auth matcher to help users match the right request when using
requests’ HTTPDigestAuth.

Reorganize and refactor the cassettes, matchers, and serializers modules.

Refactor some portions of code a bit.

Cassette.cassette_name no longer is the relative path to the file in
which the cassette is saved. To access that information use
Cassette.cassette_path. The cassette_name attribute is now the name
that you pass to Betamax#use_cassette.

0.2.0 - 2014-04-12

Fix bug where new interactions recorded under new_episodes or all
were not actually saved to disk.

Match URIs in a far more intelligent way.

Use the Session’s original adapters when making new requests

In the event the Session has a custom adapter mounted, e.g., the SSLAdapter
in requests-toolbelt, then we should probably use that.