Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@BaseMatcher.matcher
def match(self, req):
url = self.expectation
# Match as regex
if self.regex:
return self.compare(url, req.url.geturl(), regex_expr=True)
# Match URL
return all([
self.compare(url.scheme, req.url.scheme),
self.compare(url.hostname, req.url.hostname),
self.compare(url.port or req.url.port, req.url.port),
self.match_path(req),
self.match_query(req)
])
@BaseMatcher.matcher
def match(self, req):
for key in self.expectation:
# Retrieve value to match
value = self.expectation[key]
# Retrieve header value by key
header = req.headers.get(key)
# Compare header value
print('Match:', self.compare(value, header, regex_expr=True))
if not self.compare(value, header, regex_expr=True):
return False
return True
@BaseMatcher.matcher
def match(self, req):
data = req.body
if not isinstance(data, str):
return False
return self.compare(data)
@BaseMatcher.matcher
def match(self, req):
body = req.body
if isinstance(body, str):
try:
body = json.loads(body)
except Exception:
return False
x = json.dumps(self.expectation, sort_keys=True, indent=4)
y = json.dumps(body, sort_keys=True, indent=4)
return self.compare(x, y)
@BaseMatcher.matcher
def match(self, req):
expectation = self.expectation
# Decode bytes input
if isinstance(expectation, bytes):
expectation = expectation.decode('utf-8')
return self.compare(self.expectation, req.body)
@BaseMatcher.matcher
def match(self, req):
body = req.body
if isinstance(body, str):
try:
body = json.loads(body)
except Exception:
return False
if not body:
return False
try:
validate(body, self.expectation)
except Exception:
return False
@BaseMatcher.matcher
def match(self, req):
return (self.expectation == '*' or
self.compare(req.method.lower(), self.expectation.lower()))
@BaseMatcher.matcher
def match(self, req):
query = self.expectation
# Parse and assert type
if isinstance(query, str):
query = parse_qs(self.expectation)
# Validate query params
if not isinstance(query, dict):
raise ValueError('query params must be a str or dict')
# Parse request URL query
req_query = parse_qs(req.url.query)
# Match query params
return self.match_query(query, req_query)
@BaseMatcher.matcher
def match(self, req):
return self.compare(self.expectation, req.url.path)