Ejemplo n.º 1
0
    def test_extract_response_empty(self):
        resp = requests.post(
            url="http://127.0.0.1:5000/customize-response",
            json={
                'headers': {
                    'Content-Type': "application/json"
                },
                'body': ""
            }
        )

        extract_binds_list = [
            {"resp_content_body": "content"}
        ]
        resp_obj = response.ResponseObject(resp)
        extract_binds_dict_list = resp_obj.extract_response(extract_binds_list)
        self.assertEqual(
            extract_binds_dict_list[0]["resp_content_body"],
            ""
        )

        extract_binds_list = [
            {"resp_content_body": "content.abc"}
        ]
        resp_obj = response.ResponseObject(resp)
        with self.assertRaises(exception.ResponseError):
            resp_obj.extract_response(extract_binds_list)
Ejemplo n.º 2
0
    def test_extract_response_fail(self):
        resp = requests.post(url="http://127.0.0.1:5000/customize-response",
                             json={
                                 'headers': {
                                     'Content-Type': "application/json"
                                 },
                                 'body': {
                                     'success': False,
                                     "person": {
                                         "name": {
                                             "first_name": "Leo",
                                             "last_name": "Lee",
                                         },
                                         "age": 29,
                                         "cities": ["Guangzhou", "Shenzhen"]
                                     }
                                 }
                             })

        extract_binds = {"resp_content_dict_key_error": "content.not_exist"}
        resp_obj = response.ResponseObject(resp)

        with self.assertRaises(exception.ParamsError):
            resp_obj.extract_response(extract_binds)

        extract_binds = {
            "resp_content_list_index_error": "content.person.cities.3"
        }
        resp_obj = response.ResponseObject(resp)

        with self.assertRaises(exception.ParamsError):
            resp_obj.extract_response(extract_binds)
Ejemplo n.º 3
0
    def test_validate_exception(self):
        url = "http://127.0.0.1:5000/"
        resp = requests.get(url)
        resp_obj = response.ResponseObject(resp)

        # expected value missed in validators
        validators = [{
            "check": "status_code",
            "comparator": "eq",
            "expected": 201
        }, {
            "check": "body_success",
            "comparator": "eq"
        }]
        variables_mapping = {}
        with self.assertRaises(exception.ParamsError):
            resp_obj.validate(validators, variables_mapping)

        # expected value missed in variables mapping
        validators = [{
            "check": "resp_status_code",
            "comparator": "eq",
            "expected": 201
        }, {
            "check": "body_success",
            "comparator": "eq"
        }]
        variables_mapping = {"resp_status_code": 200}
        with self.assertRaises(exception.ParamsError):
            resp_obj.validate(validators, variables_mapping)
Ejemplo n.º 4
0
    def test_validate(self):
        url = "http://127.0.0.1:5000/"
        resp = requests.get(url)
        resp_obj = response.ResponseObject(resp)

        validators = [
            {"check": "resp_status_code", "comparator": "eq", "expected": 201},
            {"check": "resp_body_success", "comparator": "eq", "expected": True}
        ]
        variables_mapping = {
            "resp_status_code": 200,
            "resp_body_success": True
        }

        with self.assertRaises(exception.ValidationError):
            resp_obj.validate(validators, variables_mapping)

        validators = [
            {"check": "resp_status_code", "comparator": "eq", "expected": 201},
            {"check": "resp_body_success", "comparator": "eq", "expected": True}
        ]
        variables_mapping = {
            "resp_status_code": 201,
            "resp_body_success": True
        }

        self.assertTrue(resp_obj.validate(validators, variables_mapping))
Ejemplo n.º 5
0
    def test_extract_text_response(self):
        resp = requests.post(
            url="http://127.0.0.1:5000/customize-response",
            json={
                'headers': {
                    'Content-Type': "application/json"
                },
                'body': "LB123abcRB789"
            }
        )

        extract_binds_list = [
            {"resp_content_key1": "LB123(.*)RB789"},
            {"resp_content_key2": "LB[\d]*(.*)RB[\d]*"},
            {"resp_content_key3": "LB[\d]*(.*)9"}
        ]
        resp_obj = response.ResponseObject(resp)

        extract_binds_dict = resp_obj.extract_response(extract_binds_list)
        self.assertEqual(
            extract_binds_dict["resp_content_key1"],
            "abc"
        )
        self.assertEqual(
            extract_binds_dict["resp_content_key2"],
            "abc"
        )
        self.assertEqual(
            extract_binds_dict["resp_content_key3"],
            "abcRB78"
        )
Ejemplo n.º 6
0
    def run_test(self, testcase):
        """ run single testcase.
        @param (dict) testcase
            {
                "name": "testcase description",
                "times": 3,
                "requires": [],  # optional, override
                "function_binds": {}, # optional, override
                "variable_binds": {}, # optional, override
                "request": {
                    "url": "http://127.0.0.1:5000/api/users/1000",
                    "method": "POST",
                    "headers": {
                        "Content-Type": "application/json",
                        "authorization": "$authorization",
                        "random": "$random"
                    },
                    "body": '{"name": "user", "password": "******"}'
                },
                "extract_binds": [], # optional
                "validators": [],    # optional
                "setup": [],         # optional
                "teardown": []       # optional
            }
        @return True or raise exception during test
        """
        self.init_config(testcase, level="testcase")
        parsed_request = self.context.get_parsed_request()

        try:
            url = parsed_request.pop('url')
            method = parsed_request.pop('method')
        except KeyError:
            raise exception.ParamsError("URL or METHOD missed!")

        run_times = int(testcase.get("times", 1))
        extract_binds = testcase.get("extract_binds", [])
        validators = testcase.get("validators", [])
        setup_actions = testcase.get("setup", [])
        teardown_actions = testcase.get("teardown", [])

        def setup_teardown(actions):
            for action in actions:
                self.context.exec_content_functions(action)

        for _ in range(run_times):
            setup_teardown(setup_actions)

            resp = self.http_client_session.request(url=url, method=method, **parsed_request)
            resp_obj = response.ResponseObject(resp)

            extracted_variables_mapping_list = resp_obj.extract_response(extract_binds)
            self.context.bind_variables(extracted_variables_mapping_list, level="testset")

            resp_obj.validate(validators, self.context.get_testcase_variables_mapping())

            setup_teardown(teardown_actions)

        return True
Ejemplo n.º 7
0
    def test_extract_response_json(self):
        resp = requests.post(url="http://127.0.0.1:5000/customize-response",
                             json={
                                 'headers': {
                                     'Content-Type': "application/json"
                                 },
                                 'body': {
                                     'success': False,
                                     "person": {
                                         "name": {
                                             "first_name": "Leo",
                                             "last_name": "Lee",
                                         },
                                         "age": 29,
                                         "cities": ["Guangzhou", "Shenzhen"]
                                     }
                                 }
                             })

        extract_binds_list = [{
            "resp_status_code": "status_code"
        }, {
            "resp_headers_content_type":
            "headers.content-type"
        }, {
            "resp_content_body_success": "body.success"
        }, {
            "resp_content_content_success": "content.success"
        }, {
            "resp_content_text_success": "text.success"
        }, {
            "resp_content_person_first_name":
            "content.person.name.first_name"
        }, {
            "resp_content_cities_1":
            "content.person.cities.1"
        }]
        resp_obj = response.ResponseObject(resp)
        extract_binds_dict_list = resp_obj.extract_response(extract_binds_list)

        self.assertEqual(extract_binds_dict_list[0]["resp_status_code"], 200)
        self.assertEqual(
            extract_binds_dict_list[1]["resp_headers_content_type"],
            "application/json")
        self.assertEqual(
            extract_binds_dict_list[2]["resp_content_body_success"], False)
        self.assertEqual(
            extract_binds_dict_list[3]["resp_content_content_success"], False)
        self.assertEqual(
            extract_binds_dict_list[4]["resp_content_text_success"], False)
        self.assertEqual(
            extract_binds_dict_list[5]["resp_content_person_first_name"],
            "Leo")
        self.assertEqual(extract_binds_dict_list[6]["resp_content_cities_1"],
                         "Shenzhen")
Ejemplo n.º 8
0
 def test_parse_response_object_json(self):
     url = "http://127.0.0.1:5000/api/users"
     resp = requests.get(url)
     resp_obj = response.ResponseObject(resp)
     parsed_dict = resp_obj.parsed_dict()
     self.assertIn('status_code', parsed_dict)
     self.assertIn('headers', parsed_dict)
     self.assertIn('body', parsed_dict)
     self.assertIn('Content-Type', parsed_dict['headers'])
     self.assertIn('Content-Length', parsed_dict['headers'])
     self.assertIn('success', parsed_dict['body'])
Ejemplo n.º 9
0
 def test_parse_response_object_text(self):
     url = "http://127.0.0.1:5000/"
     resp = requests.get(url)
     resp_obj = response.ResponseObject(resp)
     parsed_dict = resp_obj.parsed_dict()
     self.assertIn('status_code', parsed_dict)
     self.assertIn('headers', parsed_dict)
     self.assertIn('body', parsed_dict)
     self.assertIn('Content-Type', parsed_dict['headers'])
     self.assertIn('Content-Length', parsed_dict['headers'])
     self.assertTrue(str, type(parsed_dict['body']))
Ejemplo n.º 10
0
    def run_test(self, testcase):
        """ run single testcase.
        @param (dict) testcase
            {
                "name": "testcase description",
                "requires": [],  # optional, override
                "function_binds": {}, # optional, override
                "variable_binds": {}, # optional, override
                "request": {
                    "url": "http://127.0.0.1:5000/api/users/1000",
                    "method": "POST",
                    "headers": {
                        "Content-Type": "application/json",
                        "authorization": "${authorization}",
                        "random": "${random}"
                    },
                    "body": '{"name": "user", "password": "******"}'
                },
                "extract_binds": {},
                "validators": []
            }
        @return (tuple) test result of single testcase
            (success, diff_content_list)
        """
        self.update_context(testcase)

        # each testcase shall inherit from testset request configs,
        # but can not override testset configs,
        # that's why we use copy.deepcopy here.
        testcase_request = copy.deepcopy(self.testset_req_overall_configs)
        testcase_request.update(testcase["request"])

        parsed_request = parse_template(testcase_request,
                                        self.context.variables)
        try:
            url = parsed_request.pop('url')
            method = parsed_request.pop('method')
        except KeyError:
            raise exception.ParamsError("URL or METHOD missed!")

        resp = self.client.request(url=url, method=method, **parsed_request)
        resp_obj = response.ResponseObject(resp)

        extract_binds = testcase.get("extract_binds", {})
        extracted_variables_mapping = resp_obj.extract_response(extract_binds)
        self.context.update_variables(extracted_variables_mapping)

        validators = testcase.get("validators", [])
        diff_content_list = resp_obj.validate(validators,
                                              self.context.variables)

        return resp_obj.success, diff_content_list
Ejemplo n.º 11
0
    def test_extract_response_json_string(self):
        resp = requests.post(url="http://127.0.0.1:5000/customize-response",
                             json={
                                 'headers': {
                                     'Content-Type': "application/json"
                                 },
                                 'body': "abc"
                             })

        extract_binds = {"resp_content_body": "content"}
        resp_obj = response.ResponseObject(resp)

        extract_binds_dict = resp_obj.extract_response(extract_binds)
        self.assertEqual(extract_binds_dict["resp_content_body"], "abc")
Ejemplo n.º 12
0
    def test_validate(self):
        url = "http://127.0.0.1:5000/"
        resp = requests.get(url)
        resp_obj = response.ResponseObject(resp)

        validators = {
            "resp_status_code": {
                "comparator": "eq",
                "expected": 201
            },
            "resp_body_success": {
                "comparator": "eq",
                "expected": True
            }
        }
        variables_mapping = {
            "resp_status_code": 200,
            "resp_body_success": True
        }

        diff_content_dict = resp_obj.validate(validators, variables_mapping)
        self.assertFalse(resp_obj.success)
        self.assertEqual(
            diff_content_dict, {
                "resp_status_code": {
                    "comparator": "eq",
                    "expected": 201,
                    "value": 200
                }
            })

        validators = {
            "resp_status_code": {
                "comparator": "eq",
                "expected": 201
            },
            "resp_body_success": {
                "comparator": "eq",
                "expected": True
            }
        }
        variables_mapping = {
            "resp_status_code": 201,
            "resp_body_success": True
        }

        diff_content_dict = resp_obj.validate(validators, variables_mapping)
        self.assertTrue(resp_obj.success)
        self.assertEqual(diff_content_dict, {})
Ejemplo n.º 13
0
    def run_test(self, testcase):
        """ run single testcase.
        @param (dict) testcase
            {
                "name": "testcase description",
                "requires": [],  # optional, override
                "function_binds": {}, # optional, override
                "variable_binds": {}, # optional, override
                "request": {
                    "url": "http://127.0.0.1:5000/api/users/1000",
                    "method": "POST",
                    "headers": {
                        "Content-Type": "application/json",
                        "authorization": "$authorization",
                        "random": "$random"
                    },
                    "body": '{"name": "user", "password": "******"}'
                },
                "extract_binds": {}, # optional
                "validators": []     # optional
            }
        @return (tuple) test result of single testcase
            (success, diff_content_list)
        """
        self.init_config(testcase, level="testcase")
        parsed_request = self.context.get_parsed_request()

        try:
            url = parsed_request.pop('url')
            method = parsed_request.pop('method')
        except KeyError:
            raise exception.ParamsError("URL or METHOD missed!")

        resp = self.http_client_session.request(url=url,
                                                method=method,
                                                **parsed_request)
        resp_obj = response.ResponseObject(resp)

        extract_binds = testcase.get("extract_binds", {})
        extracted_variables_mapping_list = resp_obj.extract_response(
            extract_binds)
        self.context.bind_variables(extracted_variables_mapping_list,
                                    level="testset")

        validators = testcase.get("validators", [])
        diff_content_list = resp_obj.validate(
            validators, self.context.get_testcase_variables_mapping())

        return resp_obj.success, diff_content_list
Ejemplo n.º 14
0
 def test_extract_text_response_exception(self):
     resp = requests.post(
         url="http://127.0.0.1:5000/customize-response",
         json={
             'headers': {
                 'Content-Type': "application/json"
             },
             'body': "LB123abcRB789"
         }
     )
     extract_binds_list = [
         {"resp_content_key1": "LB123.*RB789"}
     ]
     resp_obj = response.ResponseObject(resp)
     with self.assertRaises(exception.ParamsError):
         resp_obj.extract_response(extract_binds_list)
Ejemplo n.º 15
0
    def _run_test(self, testcase):
        """ run single testcase.
        @param (dict) testcase
            {
                "name": "testcase description",
                "times": 3,
                "requires": [],         # optional, override
                "function_binds": {},   # optional, override
                "variables": [],        # optional, override
                "request": {
                    "url": "http://127.0.0.1:5000/api/users/1000",
                    "method": "POST",
                    "headers": {
                        "Content-Type": "application/json",
                        "authorization": "$authorization",
                        "random": "$random"
                    },
                    "body": '{"name": "user", "password": "******"}'
                },
                "extract": [], # optional
                "validate": [],      # optional
                "setup": [],         # optional
                "teardown": []       # optional
            }
        @return True or raise exception during test
        """
        parsed_request = self.init_config(testcase, level="testcase")

        try:
            url = parsed_request.pop('url')
            method = parsed_request.pop('method')
            group_name = parsed_request.pop("group", None)
        except KeyError:
            raise exception.ParamsError("URL or METHOD missed!")

        run_times = int(testcase.get("times", 1))
        extractors = testcase.get("extract") \
            or testcase.get("extractors") \
            or testcase.get("extract_binds", [])
        validators = testcase.get("validate") \
            or testcase.get("validators", [])
        setup_actions = testcase.get("setup", [])
        teardown_actions = testcase.get("teardown", [])

        def setup_teardown(actions):
            for action in actions:
                self.context.exec_content_functions(action)

        for _ in range(run_times):
            setup_teardown(setup_actions)

            resp = self.http_client_session.request(method,
                                                    url,
                                                    name=group_name,
                                                    **parsed_request)
            resp_obj = response.ResponseObject(resp)

            extracted_variables_mapping = resp_obj.extract_response(extractors)
            self.context.bind_extracted_variables(extracted_variables_mapping)

            try:
                resp_obj.validate(
                    validators, self.context.get_testcase_variables_mapping())
            except (exception.ParamsError, exception.ResponseError,
                    exception.ValidationError):
                logging.error("Exception occured.")
                logging.error(
                    "HTTP request kwargs: \n{}".format(parsed_request))
                logging.error("HTTP response content: \n{}".format(resp.text))
                raise

            setup_teardown(teardown_actions)

        return True