fix: escape 5065 in variable value

This commit is contained in:
debugtalk
2020-06-08 18:05:10 +08:00
parent a33f689726
commit 2ebc8a63ce
3 changed files with 50 additions and 8 deletions

View File

@@ -13,6 +13,7 @@
- fix: incorrect summary success when testcase failed
- fix: reload to refresh previously loaded debugtalk module
- fix: escape $$ in variable value
## 3.0.10 (2020-06-07)

View File

@@ -44,11 +44,11 @@ def build_url(base_url, path):
raise exceptions.ParamsError("base url missed!")
def regex_findall_variables(content: Text) -> List[Text]:
def regex_findall_variables(raw_string: Text) -> List[Text]:
""" extract all variable names from content, which is in format $variable
Args:
content (str): string content
raw_string (str): string content
Returns:
list: variables list extracted from string content
@@ -68,14 +68,40 @@ def regex_findall_variables(content: Text) -> List[Text]:
"""
try:
vars_list = []
for var_tuple in variable_regex_compile.findall(content):
vars_list.append(var_tuple[0] or var_tuple[1])
return vars_list
except TypeError as ex:
capture_exception(ex)
match_start_position = raw_string.index("$", 0)
except ValueError:
return []
vars_list = []
while match_start_position < len(raw_string):
# Notice: notation priority
# $$ > $var
# search $$
dollar_match = dolloar_regex_compile.match(raw_string, match_start_position)
if dollar_match:
match_start_position = dollar_match.end()
continue
# search variable like ${var} or $var
var_match = variable_regex_compile.match(raw_string, match_start_position)
if var_match:
var_name = var_match.group(1) or var_match.group(2)
vars_list.append(var_name)
match_start_position = var_match.end()
continue
curr_position = match_start_position
try:
# find next $ location
match_start_position = raw_string.index("$", curr_position + 1)
except ValueError:
# break while loop
break
return vars_list
def regex_findall_functions(content: Text) -> List[Text]:
""" extract all functions from string content, which are in format ${fun()}

View File

@@ -3,6 +3,7 @@ import unittest
from httprunner import parser
from httprunner.exceptions import VariableNotFound, FunctionNotFound
from httprunner.parser import regex_findall_variables
class TestParserBasic(unittest.TestCase):
@@ -25,6 +26,19 @@ class TestParserBasic(unittest.TestCase):
self.assertEqual(parser.parse_string_value("$var"), "$var")
self.assertEqual(parser.parse_string_value("${func}"), "${func}")
def test_regex_findall_variables(self):
self.assertEqual(regex_findall_variables("$variable"), ["variable"])
self.assertEqual(regex_findall_variables("${variable}123"), ["variable"])
self.assertEqual(regex_findall_variables("/blog/$postid"), ["postid"])
self.assertEqual(regex_findall_variables("/$var1/$var2"), ["var1", "var2"])
self.assertEqual(regex_findall_variables("abc"), [])
self.assertEqual(regex_findall_variables("Z:2>1*0*1+1$a"), ["a"])
self.assertEqual(regex_findall_variables("Z:2>1*0*1+1$$a"), [])
self.assertEqual(regex_findall_variables("Z:2>1*0*1+1$$$a"), ["a"])
self.assertEqual(regex_findall_variables("Z:2>1*0*1+1$$$$a"), [])
self.assertEqual(regex_findall_variables("Z:2>1*0*1+1$$a$b"), ["b"])
self.assertEqual(regex_findall_variables("Z:2>1*0*1+1$$a$$b"), [])
def test_extract_variables(self):
self.assertEqual(parser.extract_variables("$var"), {"var"})
self.assertEqual(parser.extract_variables("$var123"), {"var123"})
@@ -40,6 +54,7 @@ class TestParserBasic(unittest.TestCase):
parser.extract_variables("${gen_md5($TOKEN, $data, $random)}"),
{"TOKEN", "data", "random"},
)
self.assertEqual(parser.extract_variables("Z:2>1*0*1+1$$1"), set())
def test_parse_function_params(self):
self.assertEqual(parser.parse_function_params(""), {"args": [], "kwargs": {}})