fix #1103: variable should not start with digit

This commit is contained in:
debugtalk
2022-03-22 17:36:08 +08:00
parent 4963206442
commit 452a1a7536
3 changed files with 12 additions and 5 deletions

View File

@@ -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)

View File

@@ -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:

View File

@@ -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