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