Test matcher by __call__

Rather than a two part match and create_response, just issue a __call__
to the matcher which will return None if there is no valid response.

This will pave the way for having external matchers that response to a
__call__ (i.e. add a function to the matcher list).
This commit is contained in:
Jamie Lennox 2014-06-26 10:41:05 +10:00
parent d1ecb8d242
commit ce5b885e45

View File

@ -218,6 +218,10 @@ class _Matcher(object):
self._match_url(request) and
self._match_headers(request))
def __call__(self, request):
if self.match(request):
return self.create_response(request)
class Adapter(BaseAdapter):
"""A fake adapter than can return predefined responses.
@ -230,9 +234,10 @@ class Adapter(BaseAdapter):
def send(self, request, **kwargs):
for matcher in reversed(self._matchers):
if matcher.match(request):
response = matcher(request)
if response is not None:
self.request_history.append(request)
return matcher.create_response(request)
return response
raise exceptions.NoMockAddress(request)