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:
Chris Dent
2016-09-06 21:46:05 +01:00
committed by GitHub
parent 3d559ab0b7
commit 448a1c3fa0
3 changed files with 32 additions and 0 deletions

View File

@@ -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' %

View File

@@ -14,6 +14,8 @@ tests:
data:
alpha: cow
beta: pig
gamma: 1
response_json_paths:
$.alpha: /ow$/
$.beta: /(?!cow).*/
$.gamma: /\d+/

View File

@@ -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'}