feat: response extract expresion support variable and function

This commit is contained in:
lihuacai
2021-09-01 15:29:23 +08:00
parent 5467326f8e
commit 0bad623780
3 changed files with 27 additions and 3 deletions

View File

@@ -167,12 +167,21 @@ class ResponseObject(object):
return check_value
def extract(self, extractors: Dict[Text, Text]) -> Dict[Text, Any]:
def extract(self,
extractors: Dict[Text, Text],
variables_mapping: VariablesMapping = None,
functions_mapping: FunctionsMapping = None,
) -> Dict[Text, Any]:
if not extractors:
return {}
extract_mapping = {}
for key, field in extractors.items():
if '$' in field:
# field contains variable or function
field = parse_data(
field, variables_mapping, functions_mapping
)
field_value = self._search_jmespath(field)
extract_mapping[key] = field_value

View File

@@ -195,7 +195,7 @@ class HttpRunner(object):
# extract
extractors = step.extract
extract_mapping = resp_obj.extract(extractors)
extract_mapping = resp_obj.extract(extractors, step.variables, self.__project_meta.functions)
step_data.export_vars = extract_mapping
variables_mapping = step.variables

View File

@@ -21,11 +21,26 @@ class TestResponse(unittest.TestCase):
self.resp_obj = ResponseObject(resp)
def test_extract(self):
variables_mapping = {
'body': 'body'
}
functions_mapping = {
'get_name': lambda: 'name',
}
extract_mapping = self.resp_obj.extract(
{"var_1": "body.json.locations[0]", "var_2": "body.json.locations[3].name"}
{
"var_1": "body.json.locations[0]",
"var_2": "body.json.locations[3].name",
"var_3": "$body.json.locations[3].name",
"var_4": "$body.json.locations[3].${get_name()}",
},
variables_mapping=variables_mapping,
functions_mapping=functions_mapping,
)
self.assertEqual(extract_mapping["var_1"], {"name": "Seattle", "state": "WA"})
self.assertEqual(extract_mapping["var_2"], "Olympia")
self.assertEqual(extract_mapping["var_3"], "Olympia")
self.assertEqual(extract_mapping["var_4"], "Olympia")
def test_validate(self):
self.resp_obj.validate(