Coverage for vcr.patch : 77%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
'''Utilities for patching in cassettes'''
# Save some of the original types for the purposes of unpatching
# Try to save the original types for requests except ImportError: # pragma: no cover pass else:
# Try to save the original types for urllib3 except ImportError: # pragma: no cover pass else: _VerifiedHTTPSConnection = urllib3.connectionpool.VerifiedHTTPSConnection
# Try to save the original types for httplib2 except ImportError: # pragma: no cover pass else: _HTTPConnectionWithTimeout = httplib2.HTTPConnectionWithTimeout _HTTPSConnectionWithTimeout = httplib2.HTTPSConnectionWithTimeout _SCHEME_TO_CONNECTION = httplib2.SCHEME_TO_CONNECTION
# Try to save the original types for boto except ImportError: # pragma: no cover pass else: _CertValidatingHTTPSConnection = boto.https_connection.CertValidatingHTTPSConnection
def wrapped(self, *args, **kwargs): function(self, *args, **kwargs) )
self._httplib(), self._requests(), self._urllib3(), self._httplib2(), self._boto(), self._build_patchers_from_mock_triples( self._cassette.custom_patches ) )
return
self._recursively_apply_get_cassette_subclass( replacement_class))
"""One of the subtleties of this class is that it does not directly replace HTTPSConnection with `VCRRequestsHTTPSConnection`, but a subclass of the aforementioned class that has the `cassette` class attribute assigned to `self._cassette`. This behavior is necessary to properly support nested cassette contexts.
This function exists to ensure that we use the same class object (reference) to patch everything that replaces VCRRequestHTTP[S]Connection, but that we can talk about patching them with the raw references instead, and without worrying about exactly where the subclass with the relevant value for `cassette` is first created.
The function is recursive because it looks in to dictionaries and replaces class values at any depth with the subclass described in the previous paragraph. """ for key, replacement_obj in replacement_dict_or_obj.items(): replacement_obj = self._recursively_apply_get_cassette_subclass( replacement_obj) replacement_dict_or_obj[key] = replacement_obj return replacement_dict_or_obj replacement_dict_or_obj)
bases += (object,) bases, dict(cassette=self._cassette))
def _httplib(self):
except ImportError: # pragma: no cover return ()
else connection_class_getter() # We need to make sure that we are actually providing a # patched version of the connection class. This might not # always be the case because the pool keeps previously # used connections (which might actually be of a different # class) around. This while loop will terminate because # eventually the pool will run out of connections. connection = get_conn(pool, timeout)
def patched_new_conn(pool):
except ImportError: # pragma: no cover return () from .stubs import urllib3_stubs return self._urllib3_patchers(cpool, urllib3_stubs)
def _httplib2(self): except ImportError: # pragma: no cover pass else: from .stubs.httplib2_stubs import VCRHTTPConnectionWithTimeout 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): except ImportError: # pragma: no cover pass else: from .stubs.boto_stubs import VCRCertValidatingHTTPSConnection yield cpool, 'CertValidatingHTTPSConnection', VCRCertValidatingHTTPSConnection
self._get_cassette_subclass(stubs.VCRRequestsHTTPConnection) ) self._get_cassette_subclass(stubs.VCRRequestsHTTPSConnection) ) (cpool, 'VerifiedHTTPSConnection', stubs.VCRRequestsHTTPSConnection), (cpool, 'VerifiedHTTPSConnection', stubs.VCRRequestsHTTPSConnection), (cpool, 'HTTPConnection', stubs.VCRRequestsHTTPConnection), (cpool, 'HTTPSConnection', stubs.VCRRequestsHTTPSConnection), (cpool, 'is_connection_dropped', mock.Mock(return_value=False)), # Needed on Windows only (cpool.HTTPConnectionPool, 'ConnectionCls', stubs.VCRRequestsHTTPConnection), (cpool.HTTPSConnectionPool, 'ConnectionCls', stubs.VCRRequestsHTTPSConnection), ) # These handle making sure that sessions only use the # connections of the appropriate type. self._patched_get_conn(cpool.HTTPConnectionPool, lambda : cpool.HTTPConnection)), (cpool.HTTPSConnectionPool, '_get_conn', self._patched_get_conn(cpool.HTTPSConnectionPool, lambda : cpool.HTTPSConnection)), (cpool.HTTPConnectionPool, '_new_conn', self._patched_new_conn(cpool.HTTPConnectionPool, http_connection_remover)), (cpool.HTTPSConnectionPool, '_new_conn', self._patched_new_conn(cpool.HTTPSConnectionPool, https_connection_remover)))
(http_connection_remover, https_connection_remover))
if isinstance(connection, self._connection_class): self._connection_pool_to_connections[self._connection_class].remove(connection)
connection = pool.pool.get() if isinstance(connection, self._connection_class): connections.remove(connection) else: readd_connections.append(connection) pool._put_conn(connection)
except ImportError: # pragma: no cover pass else: # unpatch requests v1.x # unpatch requests v2.x _cpoolHTTPConnection) _cpoolHTTPSConnection)
except ImportError: # pragma: no cover pass else: yield mock.patch.object(cpool, 'VerifiedHTTPSConnection', _VerifiedHTTPSConnection) yield mock.patch.object(cpool, 'HTTPConnection', _HTTPConnection) yield mock.patch.object(cpool, 'HTTPSConnection', _HTTPSConnection) if hasattr(cpool.HTTPConnectionPool, 'ConnectionCls'): yield mock.patch.object(cpool.HTTPConnectionPool, 'ConnectionCls', _HTTPConnection) yield mock.patch.object(cpool.HTTPSConnectionPool, 'ConnectionCls', _HTTPSConnection)
except ImportError: # pragma: no cover pass else: yield mock.patch.object(cpool, 'HTTPConnectionWithTimeout', _HTTPConnectionWithTimeout) yield mock.patch.object(cpool, 'HTTPSConnectionWithTimeout', _HTTPSConnectionWithTimeout) yield mock.patch.object(cpool, 'SCHEME_TO_CONNECTION', _SCHEME_TO_CONNECTION)
except ImportError: # pragma: no cover pass else: yield mock.patch.object(cpool, 'CertValidatingHTTPSConnection', _CertValidatingHTTPSConnection)
def force_reset(): |