diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index a74f69de..3be4849b 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -2,7 +2,10 @@ ## 3.1.8 (2022-03-22) +- feat: add `--profile` flag for har2case to support overwrite headers/cookies with specified yaml/json configuration file +- feat: support variable and function in response extract expression - fix: keep negative index in jmespath unchanged when converting pytest files, e.g. body.users[-1] +- fix: variable should not start with digit - change: load yaml file with FullLoader ## 3.1.7 (2022-03-22) diff --git a/httprunner/parser.py b/httprunner/parser.py index a6e03f2d..457d8806 100644 --- a/httprunner/parser.py +++ b/httprunner/parser.py @@ -15,9 +15,10 @@ absolute_http_url_regexp = re.compile(r"^https?://", re.I) # use $$ to escape $ notation dolloar_regex_compile = re.compile(r"\$\$") # variable notation, e.g. ${var} or $var -variable_regex_compile = re.compile(r"\$\{(\w+)\}|\$(\w+)") +# variable should start with a-zA-Z_ +variable_regex_compile = re.compile(r"\$\{([a-zA-Z_]\w*)\}|\$([a-zA-Z_]\w*)") # function notation, e.g. ${func1($var_1, $var_3)} -function_regex_compile = re.compile(r"\$\{(\w+)\(([\$\w\.\-/\s=,]*)\)\}") +function_regex_compile = re.compile(r"\$\{([a-zA-Z_]\w*)\(([\$\w\.\-/\s=,]*)\)\}") def parse_string_value(str_value: Text) -> Any: diff --git a/tests/parser_test.py b/tests/parser_test.py index e1eef7fa..2a77af59 100644 --- a/tests/parser_test.py +++ b/tests/parser_test.py @@ -41,6 +41,9 @@ class TestParserBasic(unittest.TestCase): self.assertEqual(parser.regex_findall_variables("Z:2>1*0*1+1$$$$a"), []) self.assertEqual(parser.regex_findall_variables("Z:2>1*0*1+1$$a$b"), ["b"]) self.assertEqual(parser.regex_findall_variables("Z:2>1*0*1+1$$a$$b"), []) + # variable should not start with digit + self.assertEqual(parser.regex_findall_variables("$1a"), []) + self.assertEqual(parser.regex_findall_variables("${1a}"), []) def test_extract_variables(self): self.assertEqual(parser.extract_variables("$var"), {"var"}) @@ -230,7 +233,8 @@ class TestParserBasic(unittest.TestCase): ) def test_parse_data_string_with_functions(self): - import random, string + import random + import string functions_mapping = { "gen_random_string": lambda str_len: "".join( @@ -243,8 +247,7 @@ class TestParserBasic(unittest.TestCase): ) self.assertEqual(len(result), 5) - add_two_nums = lambda a, b=1: a + b - functions_mapping["add_two_nums"] = add_two_nums + functions_mapping["add_two_nums"] = lambda a, b=1: a + b self.assertEqual( parser.parse_data( "${add_two_nums(1)}", functions_mapping=functions_mapping