mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 23:51:25 +08:00
simplify jmespath compatibility conversion
This commit is contained in:
+5
-16
@@ -14,7 +14,7 @@ from httprunner.utils import sort_dict_by_custom_order
|
||||
|
||||
|
||||
def convert_variables(
|
||||
raw_variables: Union[Dict, Text], test_path: Text
|
||||
raw_variables: Union[Dict, Text], test_path: Text
|
||||
) -> Dict[Text, Any]:
|
||||
if isinstance(raw_variables, Dict):
|
||||
return raw_variables
|
||||
@@ -54,24 +54,13 @@ def _convert_jmespath(raw: Text) -> Text:
|
||||
elif raw.startswith("json"):
|
||||
raw = f"body{raw[len('json'):]}"
|
||||
|
||||
raw_list = []
|
||||
for item in raw.split("."):
|
||||
raw_list = raw.split(".")
|
||||
for i, item in enumerate(raw_list):
|
||||
item = item.strip('"')
|
||||
if item.lower().startswith("content-") or item.lower() in ["user-agent"]:
|
||||
# add quotes for some field in white list
|
||||
# e.g. headers.Content-Type => headers."Content-Type"
|
||||
item = item.strip('"')
|
||||
raw_list.append(f'"{item}"')
|
||||
elif item.isdigit():
|
||||
# convert lst.0.name to lst[0].name
|
||||
if len(raw_list) == 0:
|
||||
logger.error(f"Invalid jmespath: {raw}")
|
||||
sys.exit(1)
|
||||
|
||||
last_item = raw_list.pop()
|
||||
item = f"{last_item}[{item}]"
|
||||
raw_list.append(item)
|
||||
else:
|
||||
raw_list.append(item)
|
||||
raw_list[i] = f'"{item}"'
|
||||
|
||||
return ".".join(raw_list)
|
||||
|
||||
|
||||
+15
-35
@@ -30,47 +30,33 @@ class TestCompat(unittest.TestCase):
|
||||
request_with_json_body = {
|
||||
"method": "POST",
|
||||
"url": "https://postman-echo.com/post",
|
||||
"headers": {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
"body": {
|
||||
"k1": "v1",
|
||||
"k2": "v2"
|
||||
}
|
||||
"headers": {"Content-Type": "application/json"},
|
||||
"body": {"k1": "v1", "k2": "v2"},
|
||||
}
|
||||
self.assertEqual(
|
||||
compat._convert_request(request_with_json_body),
|
||||
{
|
||||
"method": "POST",
|
||||
"url": "https://postman-echo.com/post",
|
||||
"headers": {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
"json": {
|
||||
"k1": "v1",
|
||||
"k2": "v2"
|
||||
}
|
||||
}
|
||||
"headers": {"Content-Type": "application/json"},
|
||||
"json": {"k1": "v1", "k2": "v2"},
|
||||
},
|
||||
)
|
||||
|
||||
request_with_text_body = {
|
||||
"method": "POST",
|
||||
"url": "https://postman-echo.com/post",
|
||||
"headers": {
|
||||
"Content-Type": "text/plain"
|
||||
},
|
||||
"body": "have a nice day"
|
||||
"headers": {"Content-Type": "text/plain"},
|
||||
"body": "have a nice day",
|
||||
}
|
||||
self.assertEqual(
|
||||
compat._convert_request(request_with_text_body),
|
||||
{
|
||||
"method": "POST",
|
||||
"url": "https://postman-echo.com/post",
|
||||
"headers": {
|
||||
"Content-Type": "text/plain"
|
||||
},
|
||||
"data": "have a nice day"
|
||||
}
|
||||
"headers": {"Content-Type": "text/plain"},
|
||||
"data": "have a nice day",
|
||||
},
|
||||
)
|
||||
|
||||
def test_convert_jmespath(self):
|
||||
@@ -85,10 +71,6 @@ class TestCompat(unittest.TestCase):
|
||||
self.assertEqual(
|
||||
compat._convert_jmespath('headers."Content-Type"'), 'headers."Content-Type"'
|
||||
)
|
||||
self.assertEqual(
|
||||
compat._convert_jmespath("body.data.buildings.0.building_id"),
|
||||
"body.data.buildings[0].building_id",
|
||||
)
|
||||
self.assertEqual(
|
||||
compat._convert_jmespath("body.users[-1]"),
|
||||
"body.users[-1]",
|
||||
@@ -97,8 +79,6 @@ class TestCompat(unittest.TestCase):
|
||||
compat._convert_jmespath("body.result.WorkNode_-1"),
|
||||
"body.result.WorkNode_-1",
|
||||
)
|
||||
with self.assertRaises(SystemExit):
|
||||
compat._convert_jmespath("2.buildings.0.building_id")
|
||||
|
||||
def test_convert_extractors(self):
|
||||
self.assertEqual(
|
||||
@@ -108,11 +88,11 @@ class TestCompat(unittest.TestCase):
|
||||
{"varA": "body.varA", "varB": "body.varB"},
|
||||
)
|
||||
self.assertEqual(
|
||||
compat._convert_extractors([{"varA": "content.0.varA"}]),
|
||||
compat._convert_extractors([{"varA": "content[0].varA"}]),
|
||||
{"varA": "body[0].varA"},
|
||||
)
|
||||
self.assertEqual(
|
||||
compat._convert_extractors({"varA": "content.0.varA"}),
|
||||
compat._convert_extractors({"varA": "content[0].varA"}),
|
||||
{"varA": "body[0].varA"},
|
||||
)
|
||||
|
||||
@@ -128,7 +108,7 @@ class TestCompat(unittest.TestCase):
|
||||
[{"eq": ["body.abc", 201]}],
|
||||
)
|
||||
self.assertEqual(
|
||||
compat._convert_validators([{"eq": ["content.0.name", 201]}]),
|
||||
compat._convert_validators([{"eq": ["content[0].name", 201]}]),
|
||||
[{"eq": ["body[0].name", 201]}],
|
||||
)
|
||||
|
||||
@@ -142,7 +122,7 @@ class TestCompat(unittest.TestCase):
|
||||
"headers": {"User-Agent": "HttpRunner/3.0"},
|
||||
},
|
||||
"extract": [{"varA": "content.varA"}, {"user_agent": "headers.User-Agent"}],
|
||||
"validate": [{"eq": ["content.varB", 200]}, {"lt": ["json.0.varC", 0]}],
|
||||
"validate": [{"eq": ["content.varB", 200]}, {"lt": ["json[0].varC", 0]}],
|
||||
}
|
||||
self.assertEqual(
|
||||
compat.ensure_testcase_v4_api(api_content),
|
||||
@@ -191,7 +171,7 @@ class TestCompat(unittest.TestCase):
|
||||
],
|
||||
"validate": [
|
||||
{"eq": ["content.varB", 200]},
|
||||
{"lt": ["json.0.varC", 0]},
|
||||
{"lt": ["json[0].varC", 0]},
|
||||
],
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user