def test_extract_functions(self): self.assertEqual( parser.extract_functions("${func()}"), ["func()"] ) self.assertEqual( parser.extract_functions("${func(5)}"), ["func(5)"] ) self.assertEqual( parser.extract_functions("${func(a=1, b=2)}"), ["func(a=1, b=2)"] ) self.assertEqual( parser.extract_functions("${func(1, $b, c=$x, d=4)}"), ["func(1, $b, c=$x, d=4)"] ) self.assertEqual( parser.extract_functions("/api/1000?_t=${get_timestamp()}"), ["get_timestamp()"] ) self.assertEqual( parser.extract_functions("/api/${add(1, 2)}"), ["add(1, 2)"] ) self.assertEqual( parser.extract_functions("/api/${add(1, 2)}?_t=${get_timestamp()}"), ["add(1, 2)", "get_timestamp()"] ) self.assertEqual( parser.extract_functions("abc${func(1, 2, a=3, b=4)}def"), ["func(1, 2, a=3, b=4)"] )
def _eval_content_functions(self, content): functions_list = parser.extract_functions(content) for func_content in functions_list: function_meta = parser.parse_function(func_content) func_name = function_meta['func_name'] args = function_meta.get('args', []) kwargs = function_meta.get('kwargs', {}) args = self.eval_content_with_bindings(args) kwargs = self.eval_content_with_bindings(kwargs) if func_name in ["parameterize", "P"]: eval_value = self.load_csv_list(*args, **kwargs) else: func = self.get_bind_function(func_name) eval_value = func(*args, **kwargs) func_content = "${" + func_content + "}" if func_content == content: # content is a variable content = eval_value else: # content contains one or many variables content = content.replace(func_content, str(eval_value), 1) return content
def __eval_check_item(self, validator, resp_obj): """ evaluate check item in validator. Args: validator (dict): validator {"check": "status_code", "comparator": "eq", "expect": 201} {"check": "$resp_body_success", "comparator": "eq", "expect": True} resp_obj (object): requests.Response() object Returns: dict: validator info { "check": "status_code", "check_value": 200, "expect": 201, "comparator": "eq" } """ check_item = validator["check"] # check_item should only be the following 5 formats: # 1, variable reference, e.g. $token # 2, function reference, e.g. ${is_status_code_200($status_code)} # 3, dict or list, maybe containing variable/function reference, e.g. {"var": "$abc"} # 4, string joined by delimiter. e.g. "status_code", "headers.content-type" # 5, regex string, e.g. "LB[\d]*(.*)RB[\d]*" if isinstance(check_item, (dict, list)) \ or parser.extract_variables(check_item) \ or parser.extract_functions(check_item): # format 1/2/3 check_value = self.eval_content(check_item) else: # format 4/5 check_value = resp_obj.extract_field(check_item) validator["check_value"] = check_value # expect_value should only be in 2 types: # 1, variable reference, e.g. $expect_status_code # 2, actual value, e.g. 200 expect_value = self.eval_content(validator["expect"]) validator["expect"] = expect_value validator["check_result"] = "unchecked" return validator
def __eval_check_item(self, validator, resp_obj): ''' evaludate check item in validator. Args: validator (dict): validator {'check':'status_code','comparator':'eq','expect':200} {'check':'$resp_body_success','comparator':'eq','expect':True} Returns: dict: validator info { 'check':'status_code', 'check_value':200, 'expect':201, 'comparator':'eq' } ''' check_item = validator['check'] # check_item should only be the following 5 formats: # 1. variable reference, e.g. $token # 2. function reference, e.g. ${is_status_code_200($status_code)} # 3. dict or list, may containing variable/function reference, e.g. {'var':'$abc'} # 4. string joined by delimiter. e.g. 'status_code', 'headers.content-type' # 5. regex string e.g. 'LB[\d]*(.*)RB[\d]*' if isinstance(check_item, (dict, list)) \ or parser.extract_variables(check_item) \ or parser.extract_functions(check_item): # format 1/2/3 check_value = self.eval_content(check_item) else: # format 4/5 check_value = resp_obj.extract_field(check_item) validator['check_value'] = check_value # expect_value should only be in 2 types: # 1. variable reference, e.g. $expect_status_code # 2. actual value e.g. 200 expect_value = self.eval_content(validator['expect']) validator['expect'] = expect_value validator['check_result'] = 'unchecked' return validator
def test_extract_functions(self): assert parser.extract_functions("${func()}") == ["func()"] assert parser.extract_functions("${func(5)}") == ["func(5)"] assert parser.extract_functions("${func(a=1, b=2)}") == [ "func(a=1, b=2)" ] assert parser.extract_functions("${func(1, $b, c=$x, d=4)}") == [ "func(1, $b, c=$x, d=4)" ] assert parser.extract_functions( "$/api/1000?_t=${get_timestamp()}") == ["get_timestamp()"] assert parser.extract_functions("${/api/${add(1, 2)}}") == [ "add(1, 2)" ] assert parser.extract_functions( "$/api/${add(1, 2)}?_t=${get_timestamp()}") == [ "add(1, 2)", "get_timestamp()" ] assert parser.extract_functions("abc${func(1, 2, a=3, b=4)}def") == [ "func(1, 2, a=3, b=4)" ]