/usr/lib/python3/dist-packages/requests_mock/mocker.py is in python3-requests-mock 0.4.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | # Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import functools
import requests
from requests_mock import adapter
from requests_mock import exceptions
class MockerCore(object):
"""A wrapper around common mocking functions.
Automate the process of mocking the requests library. This will keep the
same general options available and prevent repeating code.
"""
_PROXY_FUNCS = set(['last_request',
'register_uri',
'add_matcher',
'request_history'])
def __init__(self, **kwargs):
self._adapter = adapter.Adapter()
self._real_http = kwargs.pop('real_http', False)
self._real_send = None
if kwargs:
raise TypeError('Unexpected Arguments: %s' % ', '.join(kwargs))
def start(self):
"""Start mocking requests.
Install the adapter and the wrappers required to intercept requests.
"""
if self._real_send:
raise RuntimeError('Mocker has already been started')
self._real_send = requests.Session.send
def _fake_get_adapter(session, url):
return self._adapter
def _fake_send(session, request, **kwargs):
real_get_adapter = requests.Session.get_adapter
requests.Session.get_adapter = _fake_get_adapter
try:
return self._real_send(session, request, **kwargs)
except exceptions.NoMockAddress:
if not self._real_http:
raise
finally:
requests.Session.get_adapter = real_get_adapter
return self._real_send(session, request, **kwargs)
requests.Session.send = _fake_send
def stop(self):
"""Stop mocking requests.
This should have no impact if mocking has not been started.
"""
if self._real_send:
requests.Session.send = self._real_send
self._real_send = None
def __getattr__(self, name):
if name in self._PROXY_FUNCS:
try:
return getattr(self._adapter, name)
except AttributeError:
pass
raise AttributeError(name)
class Mocker(MockerCore):
"""The standard entry point for mock Adapter loading.
"""
def __init__(self, **kwargs):
"""Create a new mocker adapter.
:param str kw: Pass the mock object through to the decorated function
as this named keyword argument, rather than a positional argument.
:param bool real_http: True to send the request to the real requested
uri if there is not a mock installed for it. Defaults to False.
"""
self._kw = kwargs.pop('kw', None)
super(Mocker, self).__init__(**kwargs)
def __enter__(self):
self.start()
return self
def __exit__(self, type, value, traceback):
self.stop()
def __call__(self, func):
@functools.wraps(func)
def inner(*args, **kwargs):
with self as m:
if self._kw:
kwargs[self._kw] = m
else:
args = list(args)
args.append(m)
return func(*args, **kwargs)
return inner
|