mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 01:03:24 +00:00
use_cassette -> CassetteContextDecorator
This commit is contained in:
2
setup.py
2
setup.py
@@ -41,7 +41,7 @@ setup(
|
|||||||
'vcr.compat': 'vcr/compat',
|
'vcr.compat': 'vcr/compat',
|
||||||
'vcr.persisters': 'vcr/persisters',
|
'vcr.persisters': 'vcr/persisters',
|
||||||
},
|
},
|
||||||
install_requires=['PyYAML', 'contextdecorator', 'six'],
|
install_requires=['PyYAML', 'six'],
|
||||||
license='MIT',
|
license='MIT',
|
||||||
tests_require=['pytest', 'mock', 'pytest-localserver'],
|
tests_require=['pytest', 'mock', 'pytest-localserver'],
|
||||||
cmdclass={'test': PyTest},
|
cmdclass={'test': PyTest},
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
|
from six.moves import http_client as httplib
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import yaml
|
import yaml
|
||||||
import mock
|
import mock
|
||||||
|
|
||||||
from vcr.cassette import Cassette
|
from vcr.cassette import Cassette
|
||||||
from vcr.errors import UnhandledHTTPRequestError
|
from vcr.errors import UnhandledHTTPRequestError
|
||||||
|
|
||||||
@@ -73,8 +76,7 @@ def test_cassette_cant_read_same_request_twice():
|
|||||||
@mock.patch('vcr.cassette.Cassette.can_play_response_for', return_value=True)
|
@mock.patch('vcr.cassette.Cassette.can_play_response_for', return_value=True)
|
||||||
@mock.patch('vcr.stubs.VCRHTTPResponse')
|
@mock.patch('vcr.stubs.VCRHTTPResponse')
|
||||||
def test_function_decorated_with_use_cassette_can_be_invoked_multiple_times(*args):
|
def test_function_decorated_with_use_cassette_can_be_invoked_multiple_times(*args):
|
||||||
from six.moves import http_client as httplib
|
@Cassette.use('test')
|
||||||
@Cassette.use_cassette('test')
|
|
||||||
def decorated_function():
|
def decorated_function():
|
||||||
conn = httplib.HTTPConnection("www.python.org")
|
conn = httplib.HTTPConnection("www.python.org")
|
||||||
conn.request("GET", "/index.html")
|
conn.request("GET", "/index.html")
|
||||||
|
|||||||
16
tests/unit/test_vcr.py
Normal file
16
tests/unit/test_vcr.py
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import mock
|
||||||
|
|
||||||
|
from vcr import VCR
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def test_vcr_use_cassette():
|
||||||
|
filter_headers = mock.Mock()
|
||||||
|
test_vcr = VCR(filter_headers=filter_headers)
|
||||||
|
with mock.patch('vcr.config.Cassette') as mock_cassette_class:
|
||||||
|
@test_vcr.use_cassette('test')
|
||||||
|
def function():
|
||||||
|
pass
|
||||||
|
mock_cassette_class.call_count == 0
|
||||||
|
function()
|
||||||
|
assert mock_cassette_class.use.call_args[1]['filter_headers'] is filter_headers
|
||||||
@@ -14,7 +14,15 @@ from .matchers import requests_match, uri, method
|
|||||||
from .errors import UnhandledHTTPRequestError
|
from .errors import UnhandledHTTPRequestError
|
||||||
|
|
||||||
|
|
||||||
class use_cassette(object):
|
class CassetteContextDecorator(object):
|
||||||
|
"""Context manager/decorator that handles installing the cassette and
|
||||||
|
removing cassettes.
|
||||||
|
|
||||||
|
This class defers the creation of a new cassette instance until the point at
|
||||||
|
which it is installed by context manager or decorator. The fact that a new
|
||||||
|
cassette is used with each application prevents the state of any cassette
|
||||||
|
from interfering with another.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, cls, *args, **kwargs):
|
def __init__(self, cls, *args, **kwargs):
|
||||||
self.args = args
|
self.args = args
|
||||||
@@ -47,8 +55,10 @@ class Cassette(object):
|
|||||||
return new_cassette
|
return new_cassette
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def use_cassette(cls, *args, **kwargs):
|
def use(cls, *args, **kwargs):
|
||||||
return use_cassette(cls, *args, **kwargs)
|
return CassetteContextDecorator(cls, *args, **kwargs)
|
||||||
|
|
||||||
|
use_cassette = use
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
path,
|
path,
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ class VCR(object):
|
|||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
return Cassette.use_cassette(path, **merged_config)
|
return Cassette.use(path, **merged_config)
|
||||||
|
|
||||||
def register_serializer(self, name, serializer):
|
def register_serializer(self, name, serializer):
|
||||||
self.serializers[name] = serializer
|
self.serializers[name] = serializer
|
||||||
|
|||||||
Reference in New Issue
Block a user