def testMatchBad(self): data = ( # Objects ("/3", '{"1": "foo", "2": "bar"}', False), ("/a", '{"1": "foo", "2": "bar"}', False), ("/-", '{"1": "foo", "2": "bar"}', False), ("/1/3", '{"1": {"1.1": "foo", "1.2": "bar"}}', False), ("/1/a", '{"1": {"1.1": "foo", "1.2": "bar"}}', False), ("/1/-", '{"1": {"1.1": "foo", "1.2": "bar"}}', False), # Arrays ("/2", '["1", "2"]', False), ("/0/2", '[["1", "2"]]', False), # Both ("/1/3", '{"1": ["foo", "bar"]}', False), ("/0/3", '[{"1.1": "foo", "1.2": "bar"}]', False), ("/0/a", '[{"1.1": "foo", "1.2": "bar"}]', False), ("/0/-", '[{"1.1": "foo", "1.2": "bar"}]', False), # Wrong Object ("/1/0", '{"1": "foo"}', True), ("/1/0", '{"1": 1}', True), ("/1/0", '{"1": true}', True), ("/1/0", '{"1": null}', True), ) for pointer, jobj, willRaise in data: j = JSONMatcher(pointer) if willRaise: self.assertRaises(JSONPointerMatchError, j.matchs, jobj) else: self.assertEqual(j.matchs(jobj), [], "Failed test: %s" % (pointer,))
def testMatchingOK(self): data = ( ( "/", '{"1":"foo", "2": "bar"}', [{"1": "foo", "2": "bar"}, ], ), ( "/.", '{"1":"foo", "2": "bar"}', ["foo", "bar", ], ), ( "/./0", '{"1":["foo1", "foo2"], "2": ["bar1", "bar2"]}', ["foo1", "bar1", ], ), ( "/./1", '{"1":["foo1", "foo2"], "2": ["bar1", "bar2"]}', ["foo2", "bar2", ], ), ( "/./-", '{"1":["foo1", "foo2"], "2": ["bar1", "bar2"]}', ["foo2", "bar2", ], ), ( "/./2", '{"1":["foo1", "foo2"], "2": ["bar1", "bar2"]}', [], ), ( "/./foo1", '{"1":{"foo1": "bar1", "foo2": "bar2"}, "2": {"foo1": "bar3", "foo4": "bar4"}}', ["bar1", "bar3", ], ), ( "/./foo2", '{"1":{"foo1": "bar1", "foo2": "bar2"}, "2": {"foo1": "bar3", "foo4": "bar4"}}', ["bar2", ], ), ( "/./foo4", '{"1":{"foo1": "bar1", "foo2": "bar2"}, "2": {"foo1": "bar3", "foo4": "bar4"}}', ["bar4", ], ), ( "/./foo3", '{"1":{"foo1": "bar1", "foo2": "bar2"}, "2": {"foo1": "bar3", "foo4": "bar4"}}', [], ), ) for pointer, jobj, result in data: j = JSONMatcher(pointer) self.assertEqual(j.matchs(jobj), result, "Failed test: %s" % (pointer,))
def extractPointer(self, pointer, respdata): jp = JSONMatcher(pointer) try: j = json.loads(respdata) except: return None return jp.match(j)
def testMatchOK(self): data = ( # Objects ("/", '{"1": "foo"}', [{"1": "foo"}]), ("/1", '{"1": "foo", "2": "bar"}', ["foo"]), ("/2", '{"1": "foo", "2": "bar"}', ["bar"]), ("/1", '{"1": {"1.1": "foo"}}', [{"1.1": "foo"}]), ("/1/1.1", '{"1": {"1.1": "foo", "1.2": "bar"}}', ["foo"]), ("/1/1.2", '{"1": {"1.1": "foo", "1.2": "bar"}}', ["bar"]), # Arrays ("/", '["1", "2"]', [["1", "2"]]), ("/0", '["1", "2"]', ["1"]), ("/1", '["1", "2"]', ["2"]), ("/-", '["1", "2"]', ["2"]), ("/0", '[["1", "2"]]', [["1", "2"]]), ("/-", '[["1", "2"]]', [["1", "2"]]), ("/0/0", '[["1", "2"]]', ["1"]), ("/0/1", '[["1", "2"]]', ["2"]), ("/0/-", '[["1", "2"]]', ["2"]), # Both ("/", '{"1": ["foo", "bar"]}', [{"1": ["foo", "bar"]}]), ("/1", '{"1": ["foo", "bar"]}', [["foo", "bar"]]), ("/1/0", '{"1": ["foo", "bar"]}', ["foo"]), ("/1/1", '{"1": ["foo", "bar"]}', ["bar"]), ("/1/-", '{"1": ["foo", "bar"]}', ["bar"]), ("/", '[{"1.1": "foo", "1.2": "bar"}]', [[{"1.1": "foo", "1.2": "bar"}]]), ("/0", '[{"1.1": "foo", "1.2": "bar"}]', [{"1.1": "foo", "1.2": "bar"}]), ("/-", '[{"1.1": "foo", "1.2": "bar"}]', [{"1.1": "foo", "1.2": "bar"}]), ("/0/1.1", '[{"1.1": "foo", "1.2": "bar"}]', ["foo"]), ("/0/1.2", '[{"1.1": "foo", "1.2": "bar"}]', ["bar"]), ) for pointer, jobj, result in data: j = JSONMatcher(pointer) self.assertEqual(j.matchs(jobj), result, "Failed test: %s" % (pointer,))
class Verifier(object): class JsonNull(object): pass null = JsonNull() def verify(self, manager, uri, response, respdata, args): # @UnusedVariable # Get arguments statusCodes = args.get("status", [ "200", ]) exists = args.get("exists", []) notexists = args.get("notexists", []) # status code must match if str(response.status) not in statusCodes: return False, " HTTP Status Code Wrong: %d" % ( response.status, ) # look for response data if not respdata: return False, " No response body" # Must be application/json ct = response.msg.getheaders("content-type")[0].split(";")[0] if ct != "application/json" and not ct.endswith("+json"): return False, " Wrong Content-Type: %s" % (ct, ) # Read in json try: j = json.loads(respdata) except Exception, e: return False, " Response data is not JSON data: %s" % (e, ) def _splitPathTests(path): if '[' in path: return path.split('[', 1) else: return path, None result = True resulttxt = "" for jpath in exists: if jpath.find("~$") != -1: path, value = jpath.split("~$") elif jpath.find("~~") != -1: path, value = jpath.split("~~")[0], self.null else: path, value = jpath, None try: jp = JSONMatcher(path) except Exception: result = False resulttxt += " Invalid JSON pointer for %s\n" % (path, ) else: try: jobjs = jp.match(j) if not jobjs: result = False resulttxt += " Items not returned in JSON for %s\n" % ( path, ) if value and value not in map( lambda x: self.null if x is None else str(x), jobjs): result = False resulttxt += " Item values not returned in JSON for %s\n" % ( jpath, ) except JSONPointerMatchError: result = False resulttxt += " Items not returned in JSON for %s\n" % ( path, ) for jpath in notexists: if jpath.find("~$") != -1: path, value = jpath.split("~$") else: path, value = jpath, None try: jp = JSONMatcher(path) except Exception: result = False resulttxt += " Invalid JSON pointer for %s\n" % ( jpath, ) else: try: jobjs = jp.match(j) except JSONPointerMatchError: pass else: if len(jobjs): resulttxt += " Items returned in JSON for %s\n" % ( jpath, ) result = False return result, resulttxt