Cast json match to string when doing regex match (#167)
Otherwise a TypeError can happen. Tests are added to insure it works as expected. Fixes #166
This commit is contained in:
@@ -110,6 +110,8 @@ class JSONResponseHandler(ResponseHandler):
|
||||
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' %
|
||||
|
||||
@@ -14,6 +14,8 @@ tests:
|
||||
data:
|
||||
alpha: cow
|
||||
beta: pig
|
||||
gamma: 1
|
||||
response_json_paths:
|
||||
$.alpha: /ow$/
|
||||
$.beta: /(?!cow).*/
|
||||
$.gamma: /\d+/
|
||||
|
||||
@@ -128,6 +128,34 @@ class HandlersTest(unittest.TestCase):
|
||||
with self.assertRaises(AssertionError):
|
||||
self._assert_handler(handler)
|
||||
|
||||
def test_response_json_paths_regex(self):
|
||||
handler = handlers.JSONResponseHandler(self.test_class)
|
||||
self.test.content_type = "application/json"
|
||||
self.test.test_data = {'response_json_paths': {
|
||||
'$.objects[0].name': '/ow/',
|
||||
}}
|
||||
self.test.json_data = {
|
||||
'objects': [{'name': 'cow',
|
||||
'location': 'barn'},
|
||||
{'name': 'chris',
|
||||
'location': 'house'}]
|
||||
}
|
||||
self._assert_handler(handler)
|
||||
|
||||
def test_response_json_paths_regex_number(self):
|
||||
handler = handlers.JSONResponseHandler(self.test_class)
|
||||
self.test.content_type = "application/json"
|
||||
self.test.test_data = {'response_json_paths': {
|
||||
'$.objects[0].name': '/\d+/',
|
||||
}}
|
||||
self.test.json_data = {
|
||||
'objects': [{'name': 99,
|
||||
'location': 'barn'},
|
||||
{'name': 'chris',
|
||||
'location': 'house'}]
|
||||
}
|
||||
self._assert_handler(handler)
|
||||
|
||||
def test_response_headers(self):
|
||||
handler = handlers.HeadersResponseHandler(self.test_class)
|
||||
self.test.response = {'content-type': 'text/plain'}
|
||||
|
||||
Reference in New Issue
Block a user