mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-11 18:06:10 +00:00
clean up readme whitespace
This commit is contained in:
226
README.md
226
README.md
@@ -7,15 +7,15 @@ This is a Python version of [Ruby's VCR library](https://github.com/myronmarston
|
|||||||
[](http://travis-ci.org/kevin1024/vcrpy)
|
[](http://travis-ci.org/kevin1024/vcrpy)
|
||||||
|
|
||||||
## What it does
|
## What it does
|
||||||
Simplify and speed up testing HTTP by recording all HTTP interactions and saving them to
|
Simplify and speed up testing HTTP by recording all HTTP interactions and
|
||||||
"cassette" files, which are yaml files containing the contents of your
|
saving them to "cassette" files, which are yaml files containing the contents
|
||||||
requests and responses. Then when you run your tests again, they all
|
of your requests and responses. Then when you run your tests again, they all
|
||||||
just hit the text files instead of the internet. This speeds up
|
just hit the text files instead of the internet. This speeds up your tests and
|
||||||
your tests and lets you work offline.
|
lets you work offline.
|
||||||
|
|
||||||
If the server you are testing against ever changes its API, all you need
|
If the server you are testing against ever changes its API, all you need to do
|
||||||
to do is delete your existing cassette files, and run your tests again.
|
is delete your existing cassette files, and run your tests again. All of the
|
||||||
All of the mocked responses will be updated with the new API.
|
mocked responses will be updated with the new API.
|
||||||
|
|
||||||
## Compatibility Notes
|
## Compatibility Notes
|
||||||
VCR.py supports Python 2.6 and 2.7, 3.3, 3.4, and [pypy](http://pypy.org).
|
VCR.py supports Python 2.6 and 2.7, 3.3, 3.4, and [pypy](http://pypy.org).
|
||||||
@@ -46,7 +46,8 @@ pass, even if you are offline, or iana.org goes down for maintenance) and
|
|||||||
accurate (the response will contain the same headers and body you get from a
|
accurate (the response will contain the same headers and body you get from a
|
||||||
real request).
|
real request).
|
||||||
|
|
||||||
You can also use VCR.py as a decorator. The same request above would look like this:
|
You can also use VCR.py as a decorator. The same request above would look like
|
||||||
|
this:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@vcr.use_cassette('fixtures/vcr_cassettes/synopsis.yaml'):
|
@vcr.use_cassette('fixtures/vcr_cassettes/synopsis.yaml'):
|
||||||
@@ -55,12 +56,13 @@ def test_iana():
|
|||||||
assert 'Example domains' in response
|
assert 'Example domains' in response
|
||||||
```
|
```
|
||||||
|
|
||||||
All of the parameters and configuration works the same for the decorator version.
|
All of the parameters and configuration works the same for the decorator
|
||||||
|
version.
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
If you don't like VCR's defaults, you can set options by instantiating a
|
If you don't like VCR's defaults, you can set options by instantiating a `VCR`
|
||||||
`VCR` class and setting the options on it.
|
class and setting the options on it.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
|
||||||
@@ -88,10 +90,10 @@ Note: Per-cassette overrides take precedence over the global config.
|
|||||||
|
|
||||||
## Request matching
|
## Request matching
|
||||||
|
|
||||||
Request matching is configurable and allows you to change which requests
|
Request matching is configurable and allows you to change which requests VCR
|
||||||
VCR considers identical. The default behavior is `['url', method']`
|
considers identical. The default behavior is `['url', method']` which means
|
||||||
which means that requests with both the same URL and method (ie POST or
|
that requests with both the same URL and method (ie POST or GET) are considered
|
||||||
GET) are considered identical.
|
identical.
|
||||||
|
|
||||||
This can be configured by changing the `match_on` setting.
|
This can be configured by changing the `match_on` setting.
|
||||||
|
|
||||||
@@ -104,9 +106,8 @@ The following options are available :
|
|||||||
* body (the entire request body)
|
* body (the entire request body)
|
||||||
* headers (the headers of the request)
|
* headers (the headers of the request)
|
||||||
|
|
||||||
If these options don't work for you, you can also register your own
|
If these options don't work for you, you can also register your own request
|
||||||
request matcher. This is described in the Advanced section of this
|
matcher. This is described in the Advanced section of this README.
|
||||||
README.
|
|
||||||
|
|
||||||
## Record Modes
|
## Record Modes
|
||||||
VCR supports 4 record modes (with the same behavior as Ruby's VCR):
|
VCR supports 4 record modes (with the same behavior as Ruby's VCR):
|
||||||
@@ -117,46 +118,41 @@ VCR supports 4 record modes (with the same behavior as Ruby's VCR):
|
|||||||
* Record new interactions if there is no cassette file.
|
* Record new interactions if there is no cassette file.
|
||||||
* Cause an error to be raised for new requests if there is a cassette file.
|
* Cause an error to be raised for new requests if there is a cassette file.
|
||||||
|
|
||||||
It is similar to the new_episodes record mode, but will prevent new,
|
It is similar to the new_episodes record mode, but will prevent new, unexpected
|
||||||
unexpected requests from being made (i.e. because the request URI
|
requests from being made (i.e. because the request URI changed).
|
||||||
changed).
|
|
||||||
|
|
||||||
once is the default record mode, used when you do not set one.
|
once is the default record mode, used when you do not set one.
|
||||||
|
|
||||||
### new_episodes
|
### new_episodes
|
||||||
|
|
||||||
* Record new interactions.
|
* Record new interactions.
|
||||||
* Replay previously recorded interactions.
|
* Replay previously recorded interactions. It is similar to the once record
|
||||||
It is similar to the once record mode, but will always record new
|
mode, but will always record new interactions, even if you have an existing
|
||||||
interactions, even if you have an existing recorded one that is similar,
|
recorded one that is similar, but not identical.
|
||||||
but not identical.
|
|
||||||
|
|
||||||
This was the default behavior in versions < 0.3.0
|
This was the default behavior in versions < 0.3.0
|
||||||
|
|
||||||
### none
|
### none
|
||||||
|
|
||||||
* Replay previously recorded interactions.
|
* Replay previously recorded interactions.
|
||||||
* Cause an error to be raised for any new requests.
|
* Cause an error to be raised for any new requests. This is useful when your
|
||||||
This is useful when your code makes potentially dangerous
|
code makes potentially dangerous HTTP requests. The none record mode
|
||||||
HTTP requests. The none record mode guarantees that no
|
guarantees that no new HTTP requests will be made.
|
||||||
new HTTP requests will be made.
|
|
||||||
|
|
||||||
### all
|
### all
|
||||||
|
|
||||||
* Record new interactions.
|
* Record new interactions.
|
||||||
* Never replay previously recorded interactions.
|
* Never replay previously recorded interactions. This can be temporarily used
|
||||||
This can be temporarily used to force VCR to re-record
|
to force VCR to re-record a cassette (i.e. to ensure the responses are not
|
||||||
a cassette (i.e. to ensure the responses are not out of date)
|
out of date) or can be used when you simply want to log all HTTP requests.
|
||||||
or can be used when you simply want to log all HTTP requests.
|
|
||||||
|
|
||||||
## Advanced Features
|
## Advanced Features
|
||||||
|
|
||||||
If you want, VCR.py can return information about the cassette it is
|
If you want, VCR.py can return information about the cassette it is using to
|
||||||
using to record your requests and responses. This will let you record
|
record your requests and responses. This will let you record your requests and
|
||||||
your requests and responses and make assertions on them, to make sure
|
responses and make assertions on them, to make sure that your code under test
|
||||||
that your code under test is generating the expected requests and
|
is generating the expected requests and responses. This feature is not present
|
||||||
responses. This feature is not present in Ruby's VCR, but I think it is
|
in Ruby's VCR, but I think it is a nice addition. Here's an example:
|
||||||
a nice addition. Here's an example:
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import vcr
|
import vcr
|
||||||
@@ -170,20 +166,20 @@ with vcr.use_cassette('fixtures/vcr_cassettes/synopsis.yaml') as cass:
|
|||||||
assert cass.requests[0].url == 'http://www.zombo.com/'
|
assert cass.requests[0].url == 'http://www.zombo.com/'
|
||||||
```
|
```
|
||||||
|
|
||||||
The `Cassette` object exposes the following properties which I consider
|
The `Cassette` object exposes the following properties which I consider part of
|
||||||
part of the API. The fields are as follows:
|
the API. The fields are as follows:
|
||||||
|
|
||||||
* `requests`: A list of vcr.Request objects containing the requests made
|
* `requests`: A list of vcr.Request objects containing the requests made while
|
||||||
while this cassette was being used, ordered by the order that the
|
this cassette was being used, ordered by the order that the request was made.
|
||||||
request was made.
|
|
||||||
* `responses`: A list of the responses made.
|
* `responses`: A list of the responses made.
|
||||||
* `play_count`: The number of times this cassette has had a response
|
* `play_count`: The number of times this cassette has had a response played
|
||||||
played back
|
back
|
||||||
* `responses_of(request)`: Access the responses that match a given request
|
* `responses_of(request)`: Access the responses that match a given request
|
||||||
|
|
||||||
The `Request` object has the following properties
|
The `Request` object has the following properties
|
||||||
|
|
||||||
* `url`: The full url of the request, including the protocol. Example: "http://www.google.com/"
|
* `url`: The full url of the request, including the protocol. Example:
|
||||||
|
"http://www.google.com/"
|
||||||
* `path`: The path of the request. For example "/" or "/home.html"
|
* `path`: The path of the request. For example "/" or "/home.html"
|
||||||
* `host`: The host of the request, for example "www.google.com"
|
* `host`: The host of the request, for example "www.google.com"
|
||||||
* `port`: The port the request was made on
|
* `port`: The port the request was made on
|
||||||
@@ -193,14 +189,13 @@ The `Request` object has the following properties
|
|||||||
|
|
||||||
## Register your own serializer
|
## Register your own serializer
|
||||||
|
|
||||||
Don't like JSON or YAML? That's OK, VCR.py can serialize to any format
|
Don't like JSON or YAML? That's OK, VCR.py can serialize to any format you
|
||||||
you would like. Create your own module or class instance with 2 methods:
|
would like. Create your own module or class instance with 2 methods:
|
||||||
|
|
||||||
* `def deserialize(cassette_string)`
|
* `def deserialize(cassette_string)`
|
||||||
* `def serialize(cassette_dict)`
|
* `def serialize(cassette_dict)`
|
||||||
|
|
||||||
Finally, register your class with VCR to use your
|
Finally, register your class with VCR to use your new serializer.
|
||||||
new serializer.
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import vcr
|
import vcr
|
||||||
@@ -234,11 +229,10 @@ Create your own method with the following signature
|
|||||||
def my_matcher(r1, r2):
|
def my_matcher(r1, r2):
|
||||||
```
|
```
|
||||||
|
|
||||||
Your method receives the two requests and must return `True` if they
|
Your method receives the two requests and must return `True` if they match,
|
||||||
match, `False` if they don't.
|
`False` if they don't.
|
||||||
|
|
||||||
Finally, register your method with VCR to use your
|
Finally, register your method with VCR to use your new request matcher.
|
||||||
new request matcher.
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import vcr
|
import vcr
|
||||||
@@ -263,63 +257,109 @@ with my_vcr.use_cassette('test.yml'):
|
|||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
VCR.py is a package on PyPI, so you can `pip install vcrpy` (first you may need to `brew install libyaml` [[Homebrew](http://mxcl.github.com/homebrew/)])
|
VCR.py is a package on PyPI, so you can `pip install vcrpy` (first you may need
|
||||||
|
to `brew install libyaml` [[Homebrew](http://mxcl.github.com/homebrew/)])
|
||||||
|
|
||||||
## Ruby VCR compatibility
|
## Ruby VCR compatibility
|
||||||
I'm not trying to match the format of the Ruby VCR YAML files. Cassettes generated by
|
|
||||||
Ruby's VCR are not compatible with VCR.py.
|
I'm not trying to match the format of the Ruby VCR YAML files. Cassettes
|
||||||
|
generated by Ruby's VCR are not compatible with VCR.py.
|
||||||
|
|
||||||
## Running VCR's test suite
|
## Running VCR's test suite
|
||||||
|
|
||||||
The tests are all run automatically on [Travis CI](https://travis-ci.org/kevin1024/vcrpy), but you can also run them yourself using [py.test](http://pytest.org/) and [Tox](http://tox.testrun.org/). Tox will automatically run them in all environments VCR.py supports. The test suite is pretty big and slow, but you can tell tox to only run specific tests like this:
|
The tests are all run automatically on [Travis
|
||||||
|
CI](https://travis-ci.org/kevin1024/vcrpy), but you can also run them yourself
|
||||||
|
using [py.test](http://pytest.org/) and [Tox](http://tox.testrun.org/). Tox
|
||||||
|
will automatically run them in all environments VCR.py supports. The test
|
||||||
|
suite is pretty big and slow, but you can tell tox to only run specific tests
|
||||||
|
like this:
|
||||||
|
|
||||||
`tox -e py27requests -- -v -k "'test_status_code or test_gzip'"`
|
`tox -e py27requests -- -v -k "'test_status_code or test_gzip'"`
|
||||||
|
|
||||||
This will run only tests that look like `test_status_code` or `test_gzip` in the test suite, and only in the python 2.7 environment that has `requests` installed.
|
This will run only tests that look like `test_status_code` or `test_gzip` in
|
||||||
|
the test suite, and only in the python 2.7 environment that has `requests`
|
||||||
|
installed.
|
||||||
|
|
||||||
Also, in order for the boto tests to run, you will need an AWS key. Refer to the [boto documentation](http://boto.readthedocs.org/en/latest/getting_started.html) for how to set this up. I have marked the boto tests as optional in Travis so you don't have to worry about them failing if you submit a pull request.
|
Also, in order for the boto tests to run, you will need an AWS key. Refer to
|
||||||
|
the [boto
|
||||||
|
documentation](http://boto.readthedocs.org/en/latest/getting_started.html) for
|
||||||
|
how to set this up. I have marked the boto tests as optional in Travis so you
|
||||||
|
don't have to worry about them failing if you submit a pull request.
|
||||||
|
|
||||||
|
|
||||||
## Changelog
|
## Changelog
|
||||||
* 1.0.0 (in development) - Bump supported Python3 version to 3.4, fix some bugs with Boto support (thanks @marusich), fix error with URL field capitalization in README (thanks @simon-weber)
|
* 1.0.0 (in development) - Bump supported Python3 version to 3.4, fix some
|
||||||
* 0.7.0: VCR.py now supports Python 3! (thanks @asundg) Also I refactored the stub connections quite a bit to add support for the putrequest and putheader calls. This version also adds support for httplib2 (thanks @nilp0inter). I have added a couple tests for bobo since it is an http client in its own right. Finally, this version includes a fix for a bug where requests wasn't being patched properly (thanks @msabramo).
|
bugs with Boto support (thanks @marusich), fix error with URL field
|
||||||
* 0.6.0: Store response headers as a list since a HTTP response can have the same header twice (happens with set-cookie sometimes). This has the added benefit of preserving the order of headers. Thanks @smallcode for the bug report leading to this change. I have made an effort to ensure backwards compatibility with the old cassettes' header storage mechanism, but if you want to upgrade to the new header storage, you should delete your cassettes and re-record them. Also this release adds better error messages (thanks @msabramo) and adds support for using VCR as a decorator (thanks @smallcode for the motivation)
|
capitalization in README (thanks @simon-weber)
|
||||||
* 0.5.0: Change the `response_of` method to `responses_of` since cassettes can now contain more than one response for a request. Since this changes the API, I'm bumping the version. Also includes 2 bugfixes: a better error message when attempting to overwrite a cassette file, and a fix for a bug with requests sessions (thanks @msabramo)
|
* 0.7.0: VCR.py now supports Python 3! (thanks @asundg) Also I refactored
|
||||||
* 0.4.0: Change default request recording behavior for multiple requests. If you make the same request multiple times to the same URL, the response might be different each time (maybe the response has a timestamp in it or something), so this will make the same request multiple times and save them all. Then, when you are replaying the cassette, the responses will be played back in the same order in which they were received. If you were making multiple requests to the same URL in a cassette before version 0.4.0, you might need to regenerate your cassette files. Also, removes support for the cassette.play_count counter API, since individual requests aren't unique anymore. A cassette might contain the same request several times. Also removes secure overwrite feature since that was breaking overwriting files in Windows, and fixes a bug preventing request's automatic body decompression from working.
|
the stub connections quite a bit to add support for the putrequest and
|
||||||
|
putheader calls. This version also adds support for httplib2 (thanks
|
||||||
|
@nilp0inter). I have added a couple tests for boto since it is an http
|
||||||
|
client in its own right. Finally, this version includes a fix for a bug
|
||||||
|
where requests wasn't being patched properly (thanks @msabramo).
|
||||||
|
* 0.6.0: Store response headers as a list since a HTTP response can have the
|
||||||
|
same header twice (happens with set-cookie sometimes). This has the added
|
||||||
|
benefit of preserving the order of headers. Thanks @smallcode for the bug
|
||||||
|
report leading to this change. I have made an effort to ensure backwards
|
||||||
|
compatibility with the old cassettes' header storage mechanism, but if you
|
||||||
|
want to upgrade to the new header storage, you should delete your
|
||||||
|
cassettes and re-record them. Also this release adds better error messages
|
||||||
|
(thanks @msabramo) and adds support for using VCR as a decorator (thanks
|
||||||
|
@smallcode for the motivation)
|
||||||
|
* 0.5.0: Change the `response_of` method to `responses_of` since cassettes
|
||||||
|
can now contain more than one response for a request. Since this changes
|
||||||
|
the API, I'm bumping the version. Also includes 2 bugfixes: a better error
|
||||||
|
message when attempting to overwrite a cassette file, and a fix for a bug
|
||||||
|
with requests sessions (thanks @msabramo)
|
||||||
|
* 0.4.0: Change default request recording behavior for multiple requests. If
|
||||||
|
you make the same request multiple times to the same URL, the response
|
||||||
|
might be different each time (maybe the response has a timestamp in it or
|
||||||
|
something), so this will make the same request multiple times and save them
|
||||||
|
all. Then, when you are replaying the cassette, the responses will be
|
||||||
|
played back in the same order in which they were received. If you were
|
||||||
|
making multiple requests to the same URL in a cassette before version
|
||||||
|
0.4.0, you might need to regenerate your cassette files. Also, removes
|
||||||
|
support for the cassette.play_count counter API, since individual requests
|
||||||
|
aren't unique anymore. A cassette might contain the same request several
|
||||||
|
times. Also removes secure overwrite feature since that was breaking
|
||||||
|
overwriting files in Windows, and fixes a bug preventing request's
|
||||||
|
automatic body decompression from working.
|
||||||
* 0.3.5: Fix compatibility with requests 2.x
|
* 0.3.5: Fix compatibility with requests 2.x
|
||||||
* 0.3.4: Bugfix: close file before renaming it. This fixes an issue on Windows. Thanks @smallcode for the fix.
|
* 0.3.4: Bugfix: close file before renaming it. This fixes an issue on
|
||||||
* 0.3.3: Bugfix for error message when an unreigstered custom matcher
|
Windows. Thanks @smallcode for the fix.
|
||||||
was used
|
* 0.3.3: Bugfix for error message when an unreigstered custom matcher was
|
||||||
|
used
|
||||||
* 0.3.2: Fix issue with new config syntax and the `match_on` parameter.
|
* 0.3.2: Fix issue with new config syntax and the `match_on` parameter.
|
||||||
Thanks, @chromy!
|
Thanks, @chromy!
|
||||||
* 0.3.1: Fix issue causing full paths to be sent on the HTTP request
|
* 0.3.1: Fix issue causing full paths to be sent on the HTTP request line.
|
||||||
line.
|
* 0.3.0: *Backwards incompatible release* - Added support for record modes,
|
||||||
* 0.3.0: *Backwards incompatible release* - Added support for record
|
and changed the default recording behavior to the "once" record mode.
|
||||||
modes, and changed the default recording behavior to the "once" record
|
Please see the documentation on record modes for more. Added support for
|
||||||
mode. Please see the documentation on record modes for more. Added
|
custom request matching, and changed the default request matching behavior to
|
||||||
support for custom request matching, and changed the default request
|
match only on the URL and method. Also, improved the httplib mocking to add
|
||||||
matching behavior to match only on the URL and method. Also,
|
support for the `HTTPConnection.send()` method. This means that requests won't
|
||||||
improved the httplib mocking to add support for the `HTTPConnection.send()`
|
actually be sent until the response is read, since I need to record the entire
|
||||||
method. This means that requests won't actually be sent until the
|
request in order to match up the appropriate response. I don't think this
|
||||||
response is read, since I need to record the entire request in order
|
should cause any issues unless you are sending requests without ever loading
|
||||||
to match up the appropriate response. I don't think this should cause
|
the response (which none of the standard httplib wrappers do, as far as I know.
|
||||||
any issues unless you are sending requests without ever loading the
|
Thanks to @fatuhoku for some of the ideas and the motivation behind this
|
||||||
response (which none of the standard httplib wrappers do, as far as I
|
release.
|
||||||
know. Thanks to @fatuhoku for some of the ideas and the motivation
|
|
||||||
behind this release.
|
|
||||||
* 0.2.1: Fixed missing modules in setup.py
|
* 0.2.1: Fixed missing modules in setup.py
|
||||||
* 0.2.0: Added configuration API, which lets you configure some settings
|
* 0.2.0: Added configuration API, which lets you configure some settings on
|
||||||
on VCR (see the README). Also, VCR no longer saves cassettes if they
|
VCR (see the README). Also, VCR no longer saves cassettes if they haven't
|
||||||
haven't changed at all and supports JSON as well as YAML
|
changed at all and supports JSON as well as YAML (thanks @sirpengi). Added
|
||||||
(thanks @sirpengi). Added amazing new skeumorphic logo, thanks @hairarrow.
|
amazing new skeumorphic logo, thanks @hairarrow.
|
||||||
* 0.1.0: *backwards incompatible release - delete your old cassette files*:
|
* 0.1.0: *backwards incompatible release - delete your old cassette files*:
|
||||||
This release adds the ability to access the cassette to make assertions
|
This release adds the ability to access the cassette to make assertions on
|
||||||
on it, as well as a major code refactor thanks to @dlecocq. It also
|
it, as well as a major code refactor thanks to @dlecocq. It also fixes a
|
||||||
fixes a couple longstanding bugs with redirects and HTTPS. [#3 and #4]
|
couple longstanding bugs with redirects and HTTPS. [#3 and #4]
|
||||||
* 0.0.4: If you have libyaml installed, vcrpy will use the c bindings
|
* 0.0.4: If you have libyaml installed, vcrpy will use the c bindings
|
||||||
instead. Speed up your tests! Thanks @dlecocq
|
instead. Speed up your tests! Thanks @dlecocq
|
||||||
* 0.0.3: Add support for requests 1.2.3. Support for older versions of requests dropped (thanks @vitormazzi and @bryanhelmig)
|
* 0.0.3: Add support for requests 1.2.3. Support for older versions of
|
||||||
|
requests dropped (thanks @vitormazzi and @bryanhelmig)
|
||||||
* 0.0.2: Add support for requests / urllib3
|
* 0.0.2: Add support for requests / urllib3
|
||||||
* 0.0.1: Initial Release
|
* 0.0.1: Initial Release
|
||||||
|
|
||||||
# License
|
# License
|
||||||
|
|
||||||
This library uses the MIT license. See [LICENSE.txt](LICENSE.txt) for more details
|
This library uses the MIT license. See [LICENSE.txt](LICENSE.txt) for more details
|
||||||
|
|||||||
Reference in New Issue
Block a user