mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-07 00:17:37 +08:00
validate response: check validators and variables_mapping
This commit is contained in:
+3
-2
@@ -89,13 +89,14 @@ class ResponseObject(object):
|
|||||||
try:
|
try:
|
||||||
value = variables_mapping[validator_key]
|
value = variables_mapping[validator_key]
|
||||||
validator_dict["value"] = value
|
validator_dict["value"] = value
|
||||||
|
expected_value = validator_dict["expected"]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise exception.ParamsError("invalid validator %s" % validator_key)
|
raise exception.ParamsError("invalid validator %s" % validator_key)
|
||||||
|
|
||||||
match_expected = utils.match_expected(
|
match_expected = utils.match_expected(
|
||||||
value,
|
value,
|
||||||
validator_dict["expected"],
|
expected_value,
|
||||||
validator_dict["comparator"]
|
validator_dict.get("comparator", "eq")
|
||||||
)
|
)
|
||||||
|
|
||||||
if not match_expected:
|
if not match_expected:
|
||||||
|
|||||||
@@ -182,3 +182,31 @@ class TestResponse(ApiServerUnittest):
|
|||||||
diff_content_dict = resp_obj.validate(validators, variables_mapping)
|
diff_content_dict = resp_obj.validate(validators, variables_mapping)
|
||||||
self.assertTrue(resp_obj.success)
|
self.assertTrue(resp_obj.success)
|
||||||
self.assertEqual(diff_content_dict, {})
|
self.assertEqual(diff_content_dict, {})
|
||||||
|
|
||||||
|
def test_validate_exception(self):
|
||||||
|
url = "http://127.0.0.1:5000/"
|
||||||
|
resp = requests.get(url)
|
||||||
|
resp_obj = response.ResponseObject(resp)
|
||||||
|
|
||||||
|
# expected value missed in validators
|
||||||
|
validators = {
|
||||||
|
"resp_status_code": {"comparator": "eq", "expected": 201},
|
||||||
|
"resp_body_success": {"comparator": "eq"}
|
||||||
|
}
|
||||||
|
variables_mapping = {
|
||||||
|
"resp_status_code": 200,
|
||||||
|
"resp_body_success": True
|
||||||
|
}
|
||||||
|
with self.assertRaises(exception.ParamsError):
|
||||||
|
resp_obj.validate(validators, variables_mapping)
|
||||||
|
|
||||||
|
# expected value missed in validators
|
||||||
|
validators = {
|
||||||
|
"resp_status_code": {"comparator": "eq", "expected": 201},
|
||||||
|
"resp_body_success": {"comparator": "eq", "expected": True}
|
||||||
|
}
|
||||||
|
variables_mapping = {
|
||||||
|
"resp_status_code": 200
|
||||||
|
}
|
||||||
|
with self.assertRaises(exception.ParamsError):
|
||||||
|
resp_obj.validate(validators, variables_mapping)
|
||||||
|
|||||||
Reference in New Issue
Block a user