]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
Add Decorator Support
authorKevin McCarthy <me@kevinmccarthy.org>
Mon, 16 Dec 2013 02:43:11 +0000 (16:43 -1000)
committerKevin McCarthy <me@kevinmccarthy.org>
Mon, 16 Dec 2013 02:56:25 +0000 (16:56 -1000)
README.md
setup.py
tests/integration/test_urllib2.py
vcr/cassette.py

index 76fbaeb3147b1ace1a7b26dd8fe22a1162f136a0..cad24fe13d50f05bda49121540dacd37ad9efcec 100644 (file)
--- a/README.md
+++ b/README.md
@@ -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.
 
-##How to use it
+##Usage
 ```python
 import vcr
 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
 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
 
 If you don't like VCR's defaults, you can set options by instantiating a
index 697a6af285b7964f0bcc4bbc4e029a0c5e5f2f95..eb7bb415ca3fc64f1398d9a2a330fc19a9a51094 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -37,7 +37,7 @@ setup(name='vcrpy',
         'vcr.compat': 'vcr/compat',
         'vcr.persisters': 'vcr/persisters',
       },
-      install_requires=['PyYAML'],
+      install_requires=['PyYAML','contextdecorator'],
       license='MIT',
       tests_require=['pytest','mock'],
       cmdclass={'test': PyTest},
index d0e66c023c9f7a3c7c5d577d8904afb42d2e1144..5db1b63d5f8f9a4089ee298b45092ba1559e2d21 100644 (file)
@@ -114,3 +114,17 @@ def test_cross_scheme(tmpdir):
         urllib2.urlopen('http://httpbin.org/')
         assert len(cass) == 2
         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()
index e47808220c3692287bc1b32c1f7b58560cfb5a04..37dedfb57a03622adf4e8910acc6470d961168b3 100644 (file)
@@ -6,6 +6,8 @@ except ImportError:
     from .compat.counter import Counter
     from .compat.ordereddict import OrderedDict
 
+from contextdecorator import ContextDecorator
+
 # Internal imports
 from .patch import install, reset
 from .persist import load_cassette, save_cassette
@@ -13,7 +15,7 @@ from .serializers import yamlserializer
 from .matchers import requests_match, url, method
 
 
-class Cassette(object):
+class Cassette(ContextDecorator):
     '''A container for recorded requests and responses'''
 
     @classmethod