Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from gabbi import simple_wsgi
TESTS_DIR = 'gabbits_intercept'
class TestFixtureOne(fixture.GabbiFixture):
"""Drive the fixture testing weakly."""
pass
class TestFixtureTwo(fixture.GabbiFixture):
"""Drive the fixture testing weakly."""
pass
class TestResponseHandler(handlers.ResponseHandler):
"""A sample response handler just to test."""
test_key_suffix = 'test'
test_key_value = []
def preprocess(self, test):
"""Add some data if the data is a string."""
try:
test.output = test.output + '\nAnother line'
except TypeError:
pass
def action(self, test, expected, value=None):
expected = expected.replace('COW', '', 1)
test.assertIn(expected, test.output)
def _register(self, test_class):
"""Register this handler on the provided test class."""
test_class.base_test[self._key] = self.test_key_value
if self not in test_class.response_handlers:
test_class.response_handlers.append(self)
def __eq__(self, other):
if isinstance(other, ResponseHandler):
return self.__class__ == other.__class__
return False
def __ne__(self, other):
return not self.__eq__(other)
class StringResponseHandler(ResponseHandler):
"""Test for matching strings in the the response body."""
test_key_suffix = 'strings'
test_key_value = []
def action(self, test, expected, value=None):
expected = test.replace_template(expected)
test.assert_in_or_print_output(expected, test.output)
class JSONResponseHandler(ResponseHandler):
"""Test for matching json paths in the json_data."""
test_key_suffix = 'json_paths'
test_key_value = {}
def __eq__(self, other):
if isinstance(other, ResponseHandler):
return self.__class__ == other.__class__
return False
def __ne__(self, other):
return not self.__eq__(other)
class StringResponseHandler(ResponseHandler):
"""Test for matching strings in the the response body."""
test_key_suffix = 'strings'
test_key_value = []
def action(self, test, expected, value=None):
expected = test.replace_template(expected)
test.assert_in_or_print_output(expected, test.output)
class JSONResponseHandler(ResponseHandler):
"""Test for matching json paths in the json_data."""
test_key_suffix = 'json_paths'
test_key_value = {}
def action(self, test, path, value=None):
"""Test json_paths against json data."""
# NOTE: This process has some advantages over other process that
# might come along because the JSON data has already been
# processed (to provided for the magic template replacing).
# Other handlers that want access to data structures will need
# to do their own processing.
try:
match = test.extract_json_path_value(test.json_data, path)
except AttributeError:
raise AssertionError('unable to extract JSON from test results')
class ForbiddenHeadersResponseHandler(ResponseHandler):
"""Test that listed headers are not in the response."""
test_key_suffix = 'forbidden_headers'
test_key_value = []
def action(self, test, forbidden, value=None):
# normalize forbidden header to lower case
forbidden = test.replace_template(forbidden).lower()
test.assertNotIn(forbidden, test.response,
'Forbidden header %s found in response' % forbidden)
class HeadersResponseHandler(ResponseHandler):
"""Compare expected headers with actual headers.
If a header value is wrapped in ``/`` it is treated as a raw
regular expression.
Headers values are always treated as strings.
"""
test_key_suffix = 'headers'
test_key_value = {}
def action(self, test, header, value=None):
header = header.lower() # case-insensitive comparison
response = test.response
header_value = test.replace_template(str(value))
if (hasattr(expected, 'startswith') and expected.startswith('/')
and expected.endswith('/')):
expected = expected.strip('/').rstrip('/')
# match may be a number so stringify
match = str(match)
test.assertRegexpMatches(
match, expected,
'Expect jsonpath %s to match /%s/, got %s' %
(path, expected, match))
else:
test.assertEqual(expected, match,
'Unable to match %s as %s, got %s' %
(path, expected, match))
class ForbiddenHeadersResponseHandler(ResponseHandler):
"""Test that listed headers are not in the response."""
test_key_suffix = 'forbidden_headers'
test_key_value = []
def action(self, test, forbidden, value=None):
# normalize forbidden header to lower case
forbidden = test.replace_template(forbidden).lower()
test.assertNotIn(forbidden, test.response,
'Forbidden header %s found in response' % forbidden)
class HeadersResponseHandler(ResponseHandler):
"""Compare expected headers with actual headers.
If a header value is wrapped in ``/`` it is treated as a raw