Skip to content

Commit

Permalink
feat(core): add base matcher with string friendly output
Browse files Browse the repository at this point in the history
  • Loading branch information
h2non committed Oct 14, 2016
1 parent 48c6444 commit 31612e7
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 14 deletions.
4 changes: 4 additions & 0 deletions pook/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ def match(self, req):
if not matcher.match(req):
return False
return True

def __repr__(self):
matchers = [matcher.__repr__() for matcher in self]
return '[{}]'.format(', '.join(matchers))
5 changes: 4 additions & 1 deletion pook/matchers/body.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
class BodyMatcher(object):
from .base import BaseMatcher


class BodyMatcher(BaseMatcher):
def match(self, req):
return True
7 changes: 5 additions & 2 deletions pook/matchers/headers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
class HeadersMatcher(object):
from .base import BaseMatcher


class HeadersMatcher(BaseMatcher):
def __init__(self, headers):
if not isinstance(headers, dict):
raise TypeError('headers must be a dictionary')
self.headers = headers
self.expectation = headers

def match(self, req):
return True
9 changes: 9 additions & 0 deletions pook/matchers/json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from .base import BaseMatcher


class JSONBodyMatcher(BaseMatcher):
def __init__(self, headers):
self.expectation = headers

def match(self, req):
return True
9 changes: 6 additions & 3 deletions pook/matchers/method.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
class MethodMatcher(object):
from .base import BaseMatcher


class MethodMatcher(BaseMatcher):
def __init__(self, method):
self.method = method
self.expectation = method

def match(self, req):
return self.method == '*' or req.method == self.method
return self.expectation == '*' or req.expectation == self.method
7 changes: 5 additions & 2 deletions pook/matchers/query.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
class QueryMatcher(object):
from .base import BaseMatcher


class QueryMatcher(BaseMatcher):
def __init__(self, params):
self.params = params
self.expectation = params

def match(self, req):
return True
7 changes: 5 additions & 2 deletions pook/matchers/url.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
class URLMatcher(object):
from .base import BaseMatcher


class URLMatcher(BaseMatcher):
def __init__(self, expression):
self.expression = expression
self.expectation = expression

def match(self, req):
return True
10 changes: 6 additions & 4 deletions pook/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from .matchers import * # noqa
from .decorators import fluent, expectation
from .response import Response
from .request import Request
# from .request import Request
from .matcher import MatcherEngine
from .utils import trigger_methods
from .exceptions import PookExpiredMock
Expand All @@ -26,7 +26,7 @@ def __init__(self,
self.response = response or Response()
self.matchers = MatcherEngine()
self.expectations = {}

# Triggers instance methods based on argument names
trigger_methods(self, args)

Expand Down Expand Up @@ -97,10 +97,12 @@ def params(self, params):
self.add_matcher(QueryMatcher(params))

@fluent
@expectation
def json(self, body):
self.add_matcher(JSONMatcher(data))

@fluent
@expectation
def json_schema(self, schema):
self.add_matcher(JSONSchemaMatcher(schema))

Expand Down Expand Up @@ -130,7 +132,7 @@ def reply(self, status=200, **args):

def match(self, req):
if self._times <= 0:
raise PockExpiredMock('Mock has expired')
raise PookExpiredMock('Mock has expired')

# Match incoming request against registered mock matchers
matches = self.matchers.match(req)
Expand All @@ -147,4 +149,4 @@ def match(self, req):
return True

def __repr__(self):
return 'Mock({})'.format(self.expectations)
return 'Mock({})'.format(self.matchers)

0 comments on commit 31612e7

Please sign in to comment.