Esempio n. 1
0
 def test_extract_functions(self):
     self.assertEqual(
         testcase.extract_functions("${func()}"),
         ["func()"]
     )
     self.assertEqual(
         testcase.extract_functions("${func(5)}"),
         ["func(5)"]
     )
     self.assertEqual(
         testcase.extract_functions("${func(a=1, b=2)}"),
         ["func(a=1, b=2)"]
     )
     self.assertEqual(
         testcase.extract_functions("${func(1, $b, c=$x, d=4)}"),
         ["func(1, $b, c=$x, d=4)"]
     )
     self.assertEqual(
         testcase.extract_functions("/api/1000?_t=${get_timestamp()}"),
         ["get_timestamp()"]
     )
     self.assertEqual(
         testcase.extract_functions("/api/${add(1, 2)}"),
         ["add(1, 2)"]
     )
     self.assertEqual(
         testcase.extract_functions("/api/${add(1, 2)}?_t=${get_timestamp()}"),
         ["add(1, 2)", "get_timestamp()"]
     )
     self.assertEqual(
         testcase.extract_functions("abc${func(1, 2, a=3, b=4)}def"),
         ["func(1, 2, a=3, b=4)"]
     )
Esempio n. 2
0
 def test_extract_functions(self):
     self.assertEqual(
         testcase.extract_functions("${func()}"),
         ["func()"]
     )
     self.assertEqual(
         testcase.extract_functions("${func(5)}"),
         ["func(5)"]
     )
     self.assertEqual(
         testcase.extract_functions("${func(a=1, b=2)}"),
         ["func(a=1, b=2)"]
     )
     self.assertEqual(
         testcase.extract_functions("${func(1, $b, c=$x, d=4)}"),
         ["func(1, $b, c=$x, d=4)"]
     )
     self.assertEqual(
         testcase.extract_functions("/api/1000?_t=${get_timestamp()}"),
         ["get_timestamp()"]
     )
     self.assertEqual(
         testcase.extract_functions("/api/${add(1, 2)}"),
         ["add(1, 2)"]
     )
     self.assertEqual(
         testcase.extract_functions("/api/${add(1, 2)}?_t=${get_timestamp()}"),
         ["add(1, 2)", "get_timestamp()"]
     )
     self.assertEqual(
         testcase.extract_functions("abc${func(1, 2, a=3, b=4)}def"),
         ["func(1, 2, a=3, b=4)"]
     )
Esempio n. 3
0
    def _eval_content_with_bindings(self, content):
        functions_list = testcase.extract_functions(content)
        testcase_parser = self.context.testcase_parser
        if (functions_list == []):
            return testcase_parser.eval_content_with_bindings(content)

        for func_content in functions_list:
            function_meta = testcase.parse_function(func_content)
            func_name = function_meta['func_name']
            func = getattr(self, func_name, None)
            #类中没有相应的函数
            if (func == None):
                return testcase_parser.eval_content_with_bindings(content)

            args = function_meta.get('args', [])
            kwargs = function_meta.get('kwargs', {})
            args = self._eval_content_with_bindings(args)
            kwargs = self._eval_content_with_bindings(kwargs)

            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
Esempio n. 4
0
    def eval_check_item(self, validator, resp_obj):
        """ evaluate check item in validator
        @param (dict) validator
            {"check": "status_code", "comparator": "eq", "expect": 201}
            {"check": "$resp_body_success", "comparator": "eq", "expect": True}
        @param (object) resp_obj
        @return (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 testcase.extract_variables(check_item) \
            or testcase.extract_functions(check_item):
            # format 1/2/3
            check_value = self.eval_content(check_item)
        else:
            try:
                # format 4/5
                check_value = resp_obj.extract_field(check_item)
            except exception.ParseResponseError:
                msg = "failed to extract check item from response!\n"
                msg += "response content: {}".format(resp_obj.content)
                raise exception.ParseResponseError(msg)

        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
Esempio n. 5
0
    def eval_check_item(self, validator, resp_obj):
        """ evaluate check item in validator
        @param (dict) validator
            {"check": "status_code", "comparator": "eq", "expect": 201}
            {"check": "$resp_body_success", "comparator": "eq", "expect": True}
        @param (object) resp_obj
        @return (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 testcase.extract_variables(check_item) \
            or testcase.extract_functions(check_item):
            # format 1/2/3
            check_value = self.eval_content(check_item)
        else:
            try:
                # format 4/5
                check_value = resp_obj.extract_field(check_item)
            except exception.ParseResponseError:
                msg = "failed to extract check item from response!\n"
                msg += "response content: {}".format(resp_obj.content)
                raise exception.ParseResponseError(msg)

        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
        return validator