1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-10 17:45:35 +00:00

Add Decorator Support

This commit is contained in:
Kevin McCarthy
2013-12-15 16:43:11 -10:00
parent 0d08157e5d
commit 144d25bc66
4 changed files with 30 additions and 3 deletions

View File

@@ -22,7 +22,7 @@ This should work with Python 2.6 and 2.7, and [pypy](http://pypy.org).
Currently I've only tested this with urllib2, urllib3, and requests. It's known to *NOT WORK* with urllib. Currently I've only tested this with urllib2, urllib3, and requests. It's known to *NOT WORK* with urllib.
##How to use it ##Usage
```python ```python
import vcr import vcr
import urllib2 import urllib2
@@ -40,6 +40,17 @@ 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:
```
@vcr.use_cassette('fixtures/vcr_cassettes/synopsis.yaml'):
def test_iana():
response = urllib2.urlopen('http://www.iana.org/domains/reserved').read()
assert 'Example domains' in response
```
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

View File

@@ -37,7 +37,7 @@ setup(name='vcrpy',
'vcr.compat': 'vcr/compat', 'vcr.compat': 'vcr/compat',
'vcr.persisters': 'vcr/persisters', 'vcr.persisters': 'vcr/persisters',
}, },
install_requires=['PyYAML'], install_requires=['PyYAML','contextdecorator'],
license='MIT', license='MIT',
tests_require=['pytest','mock'], tests_require=['pytest','mock'],
cmdclass={'test': PyTest}, cmdclass={'test': PyTest},

View File

@@ -114,3 +114,17 @@ def test_cross_scheme(tmpdir):
urllib2.urlopen('http://httpbin.org/') urllib2.urlopen('http://httpbin.org/')
assert len(cass) == 2 assert len(cass) == 2
assert cass.play_count == 0 assert cass.play_count == 0
def test_decorator(scheme, tmpdir):
'''Test the decorator version of VCR.py'''
url = scheme + '://httpbin.org/'
@vcr.use_cassette(str(tmpdir.join('atts.yaml')))
def inner1():
return urllib2.urlopen(url).getcode()
@vcr.use_cassette(str(tmpdir.join('atts.yaml')))
def inner2():
return urllib2.urlopen(url).getcode()
assert inner1() == inner2()

View File

@@ -6,6 +6,8 @@ except ImportError:
from .compat.counter import Counter from .compat.counter import Counter
from .compat.ordereddict import OrderedDict from .compat.ordereddict import OrderedDict
from contextdecorator import ContextDecorator
# Internal imports # Internal imports
from .patch import install, reset from .patch import install, reset
from .persist import load_cassette, save_cassette from .persist import load_cassette, save_cassette
@@ -13,7 +15,7 @@ from .serializers import yamlserializer
from .matchers import requests_match, url, method from .matchers import requests_match, url, method
class Cassette(object): class Cassette(ContextDecorator):
'''A container for recorded requests and responses''' '''A container for recorded requests and responses'''
@classmethod @classmethod