mocker.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  2. # not use this file except in compliance with the License. You may obtain
  3. # a copy of the License at
  4. #
  5. # https://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. # License for the specific language governing permissions and limitations
  11. # under the License.
  12. import contextlib
  13. import functools
  14. import sys
  15. import threading
  16. import types
  17. import requests
  18. import six
  19. from requests_mock import adapter
  20. from requests_mock import exceptions
  21. DELETE = 'DELETE'
  22. GET = 'GET'
  23. HEAD = 'HEAD'
  24. OPTIONS = 'OPTIONS'
  25. PATCH = 'PATCH'
  26. POST = 'POST'
  27. PUT = 'PUT'
  28. _original_send = requests.Session.send
  29. # NOTE(phodge): we need to use an RLock (reentrant lock) here because
  30. # requests.Session.send() is reentrant. See further comments where we
  31. # monkeypatch get_adapter()
  32. _send_lock = threading.RLock()
  33. @contextlib.contextmanager
  34. def threading_rlock(timeout):
  35. kwargs = {}
  36. if sys.version_info.major >= 3:
  37. # python2 doesn't support the timeout argument
  38. kwargs['timeout'] = timeout
  39. if not _send_lock.acquire(**kwargs):
  40. m = "Could not acquire threading lock - possible deadlock scenario"
  41. raise Exception(m)
  42. try:
  43. yield
  44. finally:
  45. _send_lock.release()
  46. def _is_bound_method(method):
  47. """
  48. bound_method 's self is a obj
  49. unbound_method 's self is None
  50. """
  51. if isinstance(method, types.MethodType) and six.get_method_self(method):
  52. return True
  53. return False
  54. def _set_method(target, name, method):
  55. """ Set a mocked method onto the target.
  56. Target may be either an instance of a Session object of the
  57. requests.Session class. First we Bind the method if it's an instance.
  58. If method is a bound_method, can direct setattr
  59. """
  60. if not isinstance(target, type) and not _is_bound_method(method):
  61. method = six.create_bound_method(method, target)
  62. setattr(target, name, method)
  63. class MockerCore(object):
  64. """A wrapper around common mocking functions.
  65. Automate the process of mocking the requests library. This will keep the
  66. same general options available and prevent repeating code.
  67. """
  68. _PROXY_FUNCS = {
  69. 'last_request',
  70. 'add_matcher',
  71. 'request_history',
  72. 'called',
  73. 'called_once',
  74. 'call_count',
  75. 'reset',
  76. }
  77. case_sensitive = False
  78. """case_sensitive handles a backwards incompatible bug. The URL used to
  79. match against our matches and that is saved in request_history is always
  80. lowercased. This is incorrect as it reports incorrect history to the user
  81. and doesn't allow case sensitive path matching.
  82. Unfortunately fixing this change is backwards incompatible in the 1.X
  83. series as people may rely on this behaviour. To work around this you can
  84. globally set:
  85. requests_mock.mock.case_sensitive = True
  86. or for pytest set in your configuration:
  87. [pytest]
  88. requests_mock_case_sensitive = True
  89. which will prevent the lowercase being executed and return case sensitive
  90. url and query information.
  91. This will become the default in a 2.X release. See bug: #1584008.
  92. """
  93. def __init__(self, session=None, **kwargs):
  94. if session and not isinstance(session, requests.Session):
  95. raise TypeError("Only a requests.Session object can be mocked")
  96. self._mock_target = session or requests.Session
  97. self.case_sensitive = kwargs.pop('case_sensitive', self.case_sensitive)
  98. self._adapter = (
  99. kwargs.pop('adapter', None) or
  100. adapter.Adapter(case_sensitive=self.case_sensitive)
  101. )
  102. self._json_encoder = kwargs.pop('json_encoder', None)
  103. self.real_http = kwargs.pop('real_http', False)
  104. self._last_send = None
  105. if kwargs:
  106. raise TypeError('Unexpected Arguments: %s' % ', '.join(kwargs))
  107. def start(self):
  108. """Start mocking requests.
  109. Install the adapter and the wrappers required to intercept requests.
  110. """
  111. if self._last_send:
  112. raise RuntimeError('Mocker has already been started')
  113. # backup last `send` for restoration on `self.stop`
  114. self._last_send = self._mock_target.send
  115. self._last_get_adapter = self._mock_target.get_adapter
  116. def _fake_get_adapter(session, url):
  117. return self._adapter
  118. def _fake_send(session, request, **kwargs):
  119. # NOTE(phodge): we need to use a threading lock here in case there
  120. # are multiple threads running - one thread could restore the
  121. # original get_adapter() just as a second thread is about to
  122. # execute _original_send() below
  123. with threading_rlock(timeout=10):
  124. # mock get_adapter
  125. #
  126. # NOTE(phodge): requests.Session.send() is actually
  127. # reentrant due to how it resolves redirects with nested
  128. # calls to send(), however the reentry occurs _after_ the
  129. # call to self.get_adapter(), so it doesn't matter that we
  130. # will restore _last_get_adapter before a nested send() has
  131. # completed as long as we monkeypatch get_adapter() each
  132. # time immediately before calling original send() like we
  133. # are doing here.
  134. _set_method(session, "get_adapter", _fake_get_adapter)
  135. # NOTE(jamielennox): self._last_send vs _original_send. Whilst
  136. # it seems like here we would use _last_send there is the
  137. # possibility that the user has messed up and is somehow
  138. # nesting their mockers. If we call last_send at this point
  139. # then we end up calling this function again and the outer
  140. # level adapter ends up winning. All we really care about here
  141. # is that our adapter is in place before calling send so we
  142. # always jump directly to the real function so that our most
  143. # recently patched send call ends up putting in the most recent
  144. # adapter. It feels funny, but it works.
  145. try:
  146. return _original_send(session, request, **kwargs)
  147. except exceptions.NoMockAddress:
  148. if not self.real_http:
  149. raise
  150. except adapter._RunRealHTTP:
  151. # this mocker wants you to run the request through the real
  152. # requests library rather than the mocking. Let it.
  153. pass
  154. finally:
  155. # restore get_adapter
  156. _set_method(session, "get_adapter", self._last_get_adapter)
  157. # if we are here it means we must run the real http request
  158. # Or, with nested mocks, to the parent mock, that is why we use
  159. # _last_send here instead of _original_send
  160. if isinstance(self._mock_target, type):
  161. return self._last_send(session, request, **kwargs)
  162. else:
  163. return self._last_send(request, **kwargs)
  164. _set_method(self._mock_target, "send", _fake_send)
  165. def stop(self):
  166. """Stop mocking requests.
  167. This should have no impact if mocking has not been started.
  168. When nesting mockers, make sure to stop the innermost first.
  169. """
  170. if self._last_send:
  171. self._mock_target.send = self._last_send
  172. self._last_send = None
  173. # for familiarity with MagicMock
  174. def reset_mock(self):
  175. self.reset()
  176. def __getattr__(self, name):
  177. if name in self._PROXY_FUNCS:
  178. try:
  179. return getattr(self._adapter, name)
  180. except AttributeError:
  181. pass
  182. raise AttributeError(name)
  183. def register_uri(self, *args, **kwargs):
  184. # you can pass real_http here, but it's private to pass direct to the
  185. # adapter, because if you pass direct to the adapter you'll see the exc
  186. kwargs['_real_http'] = kwargs.pop('real_http', False)
  187. kwargs.setdefault('json_encoder', self._json_encoder)
  188. return self._adapter.register_uri(*args, **kwargs)
  189. def request(self, *args, **kwargs):
  190. return self.register_uri(*args, **kwargs)
  191. def get(self, *args, **kwargs):
  192. return self.request(GET, *args, **kwargs)
  193. def options(self, *args, **kwargs):
  194. return self.request(OPTIONS, *args, **kwargs)
  195. def head(self, *args, **kwargs):
  196. return self.request(HEAD, *args, **kwargs)
  197. def post(self, *args, **kwargs):
  198. return self.request(POST, *args, **kwargs)
  199. def put(self, *args, **kwargs):
  200. return self.request(PUT, *args, **kwargs)
  201. def patch(self, *args, **kwargs):
  202. return self.request(PATCH, *args, **kwargs)
  203. def delete(self, *args, **kwargs):
  204. return self.request(DELETE, *args, **kwargs)
  205. class Mocker(MockerCore):
  206. """The standard entry point for mock Adapter loading.
  207. """
  208. #: Defines with what should method name begin to be patched
  209. TEST_PREFIX = 'test'
  210. def __init__(self, **kwargs):
  211. """Create a new mocker adapter.
  212. :param str kw: Pass the mock object through to the decorated function
  213. as this named keyword argument, rather than a positional argument.
  214. :param bool real_http: True to send the request to the real requested
  215. uri if there is not a mock installed for it. Defaults to False.
  216. """
  217. self._kw = kwargs.pop('kw', None)
  218. super(Mocker, self).__init__(**kwargs)
  219. def __enter__(self):
  220. self.start()
  221. return self
  222. def __exit__(self, type, value, traceback):
  223. self.stop()
  224. def __call__(self, obj):
  225. if isinstance(obj, type):
  226. return self.decorate_class(obj)
  227. return self.decorate_callable(obj)
  228. def copy(self):
  229. """Returns an exact copy of current mock
  230. """
  231. m = type(self)(
  232. kw=self._kw,
  233. real_http=self.real_http,
  234. case_sensitive=self.case_sensitive
  235. )
  236. return m
  237. def decorate_callable(self, func):
  238. """Decorates a callable
  239. :param callable func: callable to decorate
  240. """
  241. @functools.wraps(func)
  242. def inner(*args, **kwargs):
  243. with self.copy() as m:
  244. if self._kw:
  245. kwargs[self._kw] = m
  246. else:
  247. args = list(args)
  248. args.append(m)
  249. return func(*args, **kwargs)
  250. return inner
  251. def decorate_class(self, klass):
  252. """Decorates methods in a class with request_mock
  253. Method will be decorated only if it name begins with `TEST_PREFIX`
  254. :param object klass: class which methods will be decorated
  255. """
  256. for attr_name in dir(klass):
  257. if not attr_name.startswith(self.TEST_PREFIX):
  258. continue
  259. attr = getattr(klass, attr_name)
  260. if not hasattr(attr, '__call__'):
  261. continue
  262. m = self.copy()
  263. setattr(klass, attr_name, m(attr))
  264. return klass
  265. mock = Mocker