adapter.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 weakref
  13. from requests.adapters import BaseAdapter
  14. from requests.utils import requote_uri
  15. import six
  16. from six.moves.urllib import parse as urlparse
  17. from requests_mock import exceptions
  18. from requests_mock.request import _RequestObjectProxy
  19. from requests_mock.response import _MatcherResponse
  20. import logging
  21. logger = logging.getLogger(__name__)
  22. try:
  23. import purl
  24. purl_types = (purl.URL,)
  25. except ImportError:
  26. purl = None
  27. purl_types = ()
  28. ANY = object()
  29. class _RequestHistoryTracker(object):
  30. def __init__(self):
  31. self.request_history = []
  32. def _add_to_history(self, request):
  33. self.request_history.append(request)
  34. @property
  35. def last_request(self):
  36. """Retrieve the latest request sent"""
  37. try:
  38. return self.request_history[-1]
  39. except IndexError:
  40. return None
  41. @property
  42. def called(self):
  43. return self.call_count > 0
  44. @property
  45. def called_once(self):
  46. return self.call_count == 1
  47. @property
  48. def call_count(self):
  49. return len(self.request_history)
  50. def reset(self):
  51. self.request_history = []
  52. class _RunRealHTTP(Exception):
  53. """A fake exception to jump out of mocking and allow a real request.
  54. This exception is caught at the mocker level and allows it to execute this
  55. request through the real requests mechanism rather than the mocker.
  56. It should never be exposed to a user.
  57. """
  58. class _Matcher(_RequestHistoryTracker):
  59. """Contains all the information about a provided URL to match."""
  60. def __init__(self, method, url, responses, complete_qs, request_headers,
  61. additional_matcher, real_http, case_sensitive):
  62. """
  63. :param bool complete_qs: Match the entire query string. By default URLs
  64. match if all the provided matcher query arguments are matched and
  65. extra query arguments are ignored. Set complete_qs to true to
  66. require that the entire query string needs to match.
  67. """
  68. super(_Matcher, self).__init__()
  69. self._method = method
  70. self._url = url
  71. self._responses = responses
  72. self._complete_qs = complete_qs
  73. self._request_headers = request_headers
  74. self._real_http = real_http
  75. self._additional_matcher = additional_matcher
  76. # url can be a regex object or ANY so don't always run urlparse
  77. if isinstance(url, six.string_types):
  78. url_parts = urlparse.urlparse(url)
  79. self._scheme = url_parts.scheme.lower()
  80. self._netloc = url_parts.netloc.lower()
  81. self._path = requote_uri(url_parts.path or '/')
  82. self._query = url_parts.query
  83. if not case_sensitive:
  84. self._path = self._path.lower()
  85. self._query = self._query.lower()
  86. elif isinstance(url, purl_types):
  87. self._scheme = url.scheme()
  88. self._netloc = url.netloc()
  89. self._path = url.path()
  90. self._query = url.query()
  91. if not case_sensitive:
  92. self._path = self._path.lower()
  93. self._query = self._query.lower()
  94. else:
  95. self._scheme = None
  96. self._netloc = None
  97. self._path = None
  98. self._query = None
  99. def _match_method(self, request):
  100. if self._method is ANY:
  101. return True
  102. if request.method.lower() == self._method.lower():
  103. return True
  104. return False
  105. def _match_url(self, request):
  106. if self._url is ANY:
  107. return True
  108. # regular expression matching
  109. if hasattr(self._url, 'search'):
  110. return self._url.search(request.url) is not None
  111. # scheme is always matched case insensitive
  112. if self._scheme and request.scheme.lower() != self._scheme:
  113. return False
  114. # netloc is always matched case insensitive
  115. if self._netloc and request.netloc.lower() != self._netloc:
  116. return False
  117. if (request.path or '/') != self._path:
  118. return False
  119. # construct our own qs structure as we remove items from it below
  120. request_qs = urlparse.parse_qs(request.query, keep_blank_values=True)
  121. matcher_qs = urlparse.parse_qs(self._query, keep_blank_values=True)
  122. for k, vals in six.iteritems(matcher_qs):
  123. for v in vals:
  124. try:
  125. request_qs.get(k, []).remove(v)
  126. except ValueError:
  127. return False
  128. if self._complete_qs:
  129. for v in six.itervalues(request_qs):
  130. if v:
  131. return False
  132. return True
  133. def _match_headers(self, request):
  134. for k, vals in six.iteritems(self._request_headers):
  135. try:
  136. header = request.headers[k]
  137. except KeyError:
  138. # NOTE(jamielennox): This seems to be a requests 1.2/2
  139. # difference, in 2 they are just whatever the user inputted in
  140. # 1 they are bytes. Let's optionally handle both and look at
  141. # removing this when we depend on requests 2.
  142. if not isinstance(k, six.text_type):
  143. return False
  144. try:
  145. header = request.headers[k.encode('utf-8')]
  146. except KeyError:
  147. return False
  148. if header != vals:
  149. return False
  150. return True
  151. def _match_additional(self, request):
  152. if callable(self._additional_matcher):
  153. return self._additional_matcher(request)
  154. if self._additional_matcher is not None:
  155. raise TypeError("Unexpected format of additional matcher.")
  156. return True
  157. def _match(self, request):
  158. return (self._match_method(request) and
  159. self._match_url(request) and
  160. self._match_headers(request) and
  161. self._match_additional(request))
  162. def __call__(self, request):
  163. if not self._match(request):
  164. return None
  165. # doing this before _add_to_history means real requests are not stored
  166. # in the request history. I'm not sure what is better here.
  167. if self._real_http:
  168. raise _RunRealHTTP()
  169. if len(self._responses) > 1:
  170. response_matcher = self._responses.pop(0)
  171. else:
  172. response_matcher = self._responses[0]
  173. self._add_to_history(request)
  174. return response_matcher.get_response(request)
  175. class Adapter(BaseAdapter, _RequestHistoryTracker):
  176. """A fake adapter than can return predefined responses.
  177. """
  178. def __init__(self, case_sensitive=False):
  179. super(Adapter, self).__init__()
  180. self._case_sensitive = case_sensitive
  181. self._matchers = []
  182. def send(self, request, **kwargs):
  183. request = _RequestObjectProxy(request,
  184. case_sensitive=self._case_sensitive,
  185. **kwargs)
  186. self._add_to_history(request)
  187. for matcher in reversed(self._matchers):
  188. try:
  189. resp = matcher(request)
  190. except Exception:
  191. request._matcher = weakref.ref(matcher)
  192. raise
  193. if resp is not None:
  194. request._matcher = weakref.ref(matcher)
  195. resp.connection = self
  196. logger.debug('{} {} {}'.format(request._request.method,
  197. request._request.url,
  198. resp.status_code))
  199. return resp
  200. raise exceptions.NoMockAddress(request)
  201. def close(self):
  202. pass
  203. def register_uri(self, method, url, response_list=None, **kwargs):
  204. """Register a new URI match and fake response.
  205. :param str method: The HTTP method to match.
  206. :param str url: The URL to match.
  207. """
  208. complete_qs = kwargs.pop('complete_qs', False)
  209. additional_matcher = kwargs.pop('additional_matcher', None)
  210. request_headers = kwargs.pop('request_headers', {})
  211. real_http = kwargs.pop('_real_http', False)
  212. json_encoder = kwargs.pop('json_encoder', None)
  213. if response_list and kwargs:
  214. raise RuntimeError('You should specify either a list of '
  215. 'responses OR response kwargs. Not both.')
  216. elif real_http and (response_list or kwargs):
  217. raise RuntimeError('You should specify either response data '
  218. 'OR real_http. Not both.')
  219. elif not response_list:
  220. if json_encoder is not None:
  221. kwargs['json_encoder'] = json_encoder
  222. response_list = [] if real_http else [kwargs]
  223. # NOTE(jamielennox): case_sensitive is not present as a kwarg because i
  224. # think there would be an edge case where the adapter and register_uri
  225. # had different values.
  226. # Ideally case_sensitive would be a value passed to match() however
  227. # this would change the contract of matchers so we pass ito to the
  228. # proxy and the matcher separately.
  229. responses = [_MatcherResponse(**k) for k in response_list]
  230. matcher = _Matcher(method,
  231. url,
  232. responses,
  233. case_sensitive=self._case_sensitive,
  234. complete_qs=complete_qs,
  235. additional_matcher=additional_matcher,
  236. request_headers=request_headers,
  237. real_http=real_http)
  238. self.add_matcher(matcher)
  239. return matcher
  240. def add_matcher(self, matcher):
  241. """Register a custom matcher.
  242. A matcher is a callable that takes a `requests.Request` and returns a
  243. `requests.Response` if it matches or None if not.
  244. :param callable matcher: The matcher to execute.
  245. """
  246. self._matchers.append(matcher)
  247. def reset(self):
  248. super(Adapter, self).reset()
  249. for matcher in self._matchers:
  250. matcher.reset()
  251. __all__ = ['Adapter']