Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import sys
from .base import BaseMatcher
if sys.version_info < (3,): # Python 2
from urlparse import parse_qs
else: # Python 3
from urllib.parse import parse_qs
class QueryMatcher(BaseMatcher):
"""
QueryMatcher implements an URL query params matcher.
"""
def match_query(self, query, req_query):
def test(key, param):
match = req_query.get(key)
if match is None:
return False
# Normalize param value
param = [param] if not isinstance(param, list) else param
# Compare query params
[[self.compare(value, expect)
for expect in match] for value in param]
from .base import BaseMatcher
class HeadersMatcher(BaseMatcher):
"""
Headers HTTP request matcher.
"""
def __init__(self, headers):
if not isinstance(headers, dict):
raise TypeError('headers must be a dictionary')
BaseMatcher.__init__(self, headers)
@BaseMatcher.matcher
def match(self, req):
for key in self.expectation:
# Retrieve value to match
value = self.expectation[key]
# Retrieve header value by key
from .base import BaseMatcher
class PathMatcher(BaseMatcher):
"""
PathMatcher implements an URL path matcher.
"""
@BaseMatcher.matcher
def match(self, req):
return self.compare(self.expectation, req.url.path)
import json
from .base import BaseMatcher
class JSONMatcher(BaseMatcher):
"""
JSONMatcher implements a JSON body matcher supporting strict structure
and regular expression based comparisons.
"""
def __init__(self, data):
BaseMatcher.__init__(self, data)
if isinstance(data, str):
self.expectation = json.loads(data)
@BaseMatcher.matcher
def match(self, req):
body = req.body
if isinstance(body, str):