mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 01:03:24 +00:00
Refactor build_patchers into class. Fix issue with patching non existent attribute with hasattr.
This commit is contained in:
@@ -7,7 +7,7 @@ except ImportError:
|
|||||||
from .compat.counter import Counter
|
from .compat.counter import Counter
|
||||||
|
|
||||||
# Internal imports
|
# Internal imports
|
||||||
from .patch import build_patchers
|
from .patch import PatcherBuilder
|
||||||
from .persist import load_cassette, save_cassette
|
from .persist import load_cassette, save_cassette
|
||||||
from .filters import filter_request
|
from .filters import filter_request
|
||||||
from .serializers import yamlserializer
|
from .serializers import yamlserializer
|
||||||
@@ -36,7 +36,7 @@ class CassetteContextDecorator(object):
|
|||||||
|
|
||||||
def _patch_generator(self, cassette):
|
def _patch_generator(self, cassette):
|
||||||
with contextlib2.ExitStack() as exit_stack:
|
with contextlib2.ExitStack() as exit_stack:
|
||||||
for patcher in build_patchers(cassette):
|
for patcher in PatcherBuilder(cassette).build_patchers():
|
||||||
exit_stack.enter_context(patcher)
|
exit_stack.enter_context(patcher)
|
||||||
yield cassette
|
yield cassette
|
||||||
# TODO(@IvanMalison): Hmmm. it kind of feels like this should be somewhere else.
|
# TODO(@IvanMalison): Hmmm. it kind of feels like this should be somewhere else.
|
||||||
|
|||||||
166
vcr/patch.py
166
vcr/patch.py
@@ -1,4 +1,6 @@
|
|||||||
'''Utilities for patching in cassettes'''
|
'''Utilities for patching in cassettes'''
|
||||||
|
import itertools
|
||||||
|
|
||||||
import contextlib2
|
import contextlib2
|
||||||
import mock
|
import mock
|
||||||
|
|
||||||
@@ -51,89 +53,96 @@ else:
|
|||||||
_CertValidatingHTTPSConnection = boto.https_connection.CertValidatingHTTPSConnection
|
_CertValidatingHTTPSConnection = boto.https_connection.CertValidatingHTTPSConnection
|
||||||
|
|
||||||
|
|
||||||
|
class PatcherBuilder(object):
|
||||||
|
|
||||||
def cassette_subclass(base_class, cassette):
|
def __init__(self, cassette):
|
||||||
bases = (base_class,)
|
self._cassette = cassette
|
||||||
if not issubclass(base_class, object): # Check for old style class
|
self._class_to_cassette_subclass = {}
|
||||||
bases += (object,)
|
|
||||||
return type('{0}{1}'.format(base_class.__name__, cassette._path), bases, dict(cassette=cassette))
|
|
||||||
|
|
||||||
|
def build_patchers(self):
|
||||||
|
patcher_args = itertools.chain(self._httplib(), self._requests(), self._urllib3(),
|
||||||
|
self._httplib2(), self._boto())
|
||||||
|
for args in patcher_args:
|
||||||
|
patcher = self._build_patcher(*args)
|
||||||
|
if patcher:
|
||||||
|
yield patcher
|
||||||
|
|
||||||
def build_patchers(cassette):
|
def _build_patcher(self, obj, patched_attribute, replacement_class):
|
||||||
"""
|
if not hasattr(obj, patched_attribute):
|
||||||
Build patches for all the HTTPConnections references we can find!
|
return
|
||||||
This replaces the actual HTTPConnection with a VCRHTTPConnection
|
|
||||||
object which knows how to save to / read from cassettes
|
|
||||||
"""
|
|
||||||
_VCRHTTPConnection = cassette_subclass(VCRHTTPConnection, cassette)
|
|
||||||
_VCRHTTPSConnection = cassette_subclass(VCRHTTPSConnection, cassette)
|
|
||||||
|
|
||||||
|
if isinstance(replacement_class, dict):
|
||||||
|
for key in replacement_class:
|
||||||
|
replacement_class[key] = self._get_cassette_subclass(replacement_class[key])
|
||||||
|
else:
|
||||||
|
replacement_class = self._get_cassette_subclass(replacement_class)
|
||||||
|
return mock.patch.object(obj, patched_attribute, replacement_class)
|
||||||
|
|
||||||
yield mock.patch.object(httplib, 'HTTPConnection', _VCRHTTPConnection)
|
def _get_cassette_subclass(self, klass):
|
||||||
yield mock.patch.object(httplib, 'HTTPSConnection', _VCRHTTPSConnection)
|
if klass not in self._class_to_cassette_subclass:
|
||||||
|
self._class_to_cassette_subclass[klass] = self._cassette_subclass(klass)
|
||||||
|
return self._class_to_cassette_subclass[klass]
|
||||||
|
|
||||||
# requests
|
def _cassette_subclass(self, base_class):
|
||||||
try:
|
bases = (base_class,)
|
||||||
import requests.packages.urllib3.connectionpool as cpool
|
if not issubclass(base_class, object): # Check for old style class
|
||||||
except ImportError: # pragma: no cover
|
bases += (object,)
|
||||||
pass
|
return type('{0}{1}'.format(base_class.__name__, self._cassette._path),
|
||||||
else:
|
bases, dict(cassette=self._cassette))
|
||||||
from .stubs.requests_stubs import VCRRequestsHTTPConnection, VCRRequestsHTTPSConnection
|
|
||||||
|
|
||||||
# patch requests v1.x
|
def _httplib(self):
|
||||||
yield mock.patch.object(cpool, 'VerifiedHTTPSConnection',
|
yield httplib, 'HTTPConnection', VCRHTTPConnection
|
||||||
cassette_subclass(VCRRequestsHTTPSConnection, cassette))
|
yield httplib, 'HTTPSConnection', VCRHTTPSConnection
|
||||||
yield mock.patch.object(cpool, 'HTTPConnection',
|
|
||||||
cassette_subclass(VCRRequestsHTTPConnection, cassette))
|
|
||||||
yield mock.patch.object(cpool, 'HTTPConnection', _VCRHTTPConnection)
|
|
||||||
|
|
||||||
# patch requests v2.x
|
def _requests(self):
|
||||||
if hasattr(cpool.HTTPConnectionPool, 'ConnectionCls'):
|
try:
|
||||||
yield mock.patch.object(cpool.HTTPConnectionPool, 'ConnectionCls',
|
import requests.packages.urllib3.connectionpool as cpool
|
||||||
cassette_subclass(VCRRequestsHTTPConnection, cassette))
|
except ImportError: # pragma: no cover
|
||||||
yield mock.patch.object(cpool.HTTPSConnectionPool, 'ConnectionCls',
|
pass
|
||||||
cassette_subclass(VCRRequestsHTTPSConnection, cassette))
|
else:
|
||||||
|
from .stubs.requests_stubs import VCRRequestsHTTPConnection, VCRRequestsHTTPSConnection
|
||||||
|
|
||||||
# patch urllib3
|
yield cpool, 'VerifiedHTTPSConnection', VCRRequestsHTTPSConnection
|
||||||
try:
|
yield cpool, 'VerifiedHTTPSConnection', VCRRequestsHTTPSConnection
|
||||||
import urllib3.connectionpool as cpool
|
yield cpool, 'HTTPConnection', VCRRequestsHTTPConnection
|
||||||
except ImportError: # pragma: no cover
|
yield cpool, 'HTTPConnection', VCRHTTPConnection
|
||||||
pass
|
yield cpool.HTTPConnectionPool, 'ConnectionCls', VCRRequestsHTTPConnection
|
||||||
else:
|
yield cpool.HTTPSConnectionPool, 'ConnectionCls', VCRRequestsHTTPSConnection
|
||||||
from .stubs.urllib3_stubs import VCRVerifiedHTTPSConnection
|
|
||||||
yield mock.patch.object(cpool, 'VerifiedHTTPSConnection',
|
|
||||||
cassette_subclass(VCRVerifiedHTTPSConnection, cassette))
|
|
||||||
yield mock.patch.object(cpool, 'HTTPConnection', _VCRHTTPConnection)
|
|
||||||
|
|
||||||
# patch httplib2
|
def _urllib3(self):
|
||||||
try:
|
try:
|
||||||
import httplib2 as cpool
|
import urllib3.connectionpool as cpool
|
||||||
except ImportError: # pragma: no cover
|
except ImportError: # pragma: no cover
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
from .stubs.httplib2_stubs import VCRHTTPConnectionWithTimeout
|
from .stubs.urllib3_stubs import VCRVerifiedHTTPSConnection
|
||||||
from .stubs.httplib2_stubs import VCRHTTPSConnectionWithTimeout
|
|
||||||
_VCRHTTPConnectionWithTimeout = cassette_subclass(VCRHTTPConnectionWithTimeout,
|
yield cpool, 'VerifiedHTTPSConnection', VCRVerifiedHTTPSConnection
|
||||||
cassette)
|
yield cpool, 'HTTPConnection', VCRHTTPConnection
|
||||||
_VCRHTTPSConnectionWithTimeout = cassette_subclass(VCRHTTPSConnectionWithTimeout,
|
|
||||||
cassette)
|
def _httplib2(self):
|
||||||
yield mock.patch.object(cpool, 'HTTPConnectionWithTimeout',
|
try:
|
||||||
_VCRHTTPConnectionWithTimeout)
|
import httplib2 as cpool
|
||||||
yield mock.patch.object(cpool, 'HTTPSConnectionWithTimeout',
|
except ImportError: # pragma: no cover
|
||||||
_VCRHTTPSConnectionWithTimeout)
|
pass
|
||||||
yield mock.patch.object(cpool, 'SCHEME_TO_CONNECTION',
|
else:
|
||||||
{'http': _VCRHTTPConnectionWithTimeout,
|
from .stubs.httplib2_stubs import VCRHTTPConnectionWithTimeout
|
||||||
'https': _VCRHTTPSConnectionWithTimeout})
|
from .stubs.httplib2_stubs import VCRHTTPSConnectionWithTimeout
|
||||||
|
|
||||||
|
yield cpool, 'HTTPConnectionWithTimeout', VCRHTTPConnectionWithTimeout
|
||||||
|
yield cpool, 'HTTPSConnectionWithTimeout', VCRHTTPSConnectionWithTimeout
|
||||||
|
yield cpool, 'SCHEME_TO_CONNECTION', {'http': VCRHTTPConnectionWithTimeout,
|
||||||
|
'https': VCRHTTPSConnectionWithTimeout}
|
||||||
|
|
||||||
|
def _boto(self):
|
||||||
|
try:
|
||||||
|
import boto.https_connection as cpool
|
||||||
|
except ImportError: # pragma: no cover
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
from .stubs.boto_stubs import VCRCertValidatingHTTPSConnection
|
||||||
|
yield cpool, 'CertValidatingHTTPSConnection', VCRCertValidatingHTTPSConnection
|
||||||
|
|
||||||
# patch boto
|
|
||||||
try:
|
|
||||||
import boto.https_connection as cpool
|
|
||||||
except ImportError: # pragma: no cover
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
from .stubs.boto_stubs import VCRCertValidatingHTTPSConnection
|
|
||||||
yield mock.patch.object(cpool, 'CertValidatingHTTPSConnection',
|
|
||||||
cassette_subclass(VCRCertValidatingHTTPSConnection, cassette))
|
|
||||||
|
|
||||||
|
|
||||||
def reset_patchers():
|
def reset_patchers():
|
||||||
@@ -148,11 +157,14 @@ def reset_patchers():
|
|||||||
yield mock.patch.object(cpool, 'VerifiedHTTPSConnection', _VerifiedHTTPSConnection)
|
yield mock.patch.object(cpool, 'VerifiedHTTPSConnection', _VerifiedHTTPSConnection)
|
||||||
yield mock.patch.object(cpool, 'HTTPConnection', _cpoolHTTPConnection)
|
yield mock.patch.object(cpool, 'HTTPConnection', _cpoolHTTPConnection)
|
||||||
# unpatch requests v2.x
|
# unpatch requests v2.x
|
||||||
yield mock.patch.object(cpool.HTTPConnectionPool, 'ConnectionCls',
|
if hasattr(cpool.HTTPConnectionPool, 'ConnectionCls'):
|
||||||
_cpoolHTTPConnection)
|
yield mock.patch.object(cpool.HTTPConnectionPool, 'ConnectionCls',
|
||||||
yield mock.patch.object(cpool, 'HTTPSConnection', _cpoolHTTPSConnection)
|
_cpoolHTTPConnection)
|
||||||
yield mock.patch.object(cpool.HTTPSConnectionPool, 'ConnectionCls',
|
yield mock.patch.object(cpool.HTTPSConnectionPool, 'ConnectionCls',
|
||||||
_cpoolHTTPSConnection)
|
_cpoolHTTPSConnection)
|
||||||
|
|
||||||
|
if hasattr(cpool, 'HTTPSConnection'):
|
||||||
|
yield mock.patch.object(cpool, 'HTTPSConnection', _cpoolHTTPSConnection)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import urllib3.connectionpool as cpool
|
import urllib3.connectionpool as cpool
|
||||||
|
|||||||
Reference in New Issue
Block a user