예제 #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 = resp_obj.extract_response(extract_binds_list)
        self.assertEqual(
            extract_binds_dict["resp_content_body"],
            ""
        )

        extract_binds_list = [
            {"resp_content_body": "content.abc"}
        ]
        resp_obj = response.ResponseObject(resp)
        with self.assertRaises(exception.ParamsError):
            resp_obj.extract_response(extract_binds_list)
예제 #2
0
    def test_extract_response_fail(self):
        resp = requests.post(
            url="http://127.0.0.1:3458/anything",
            json={
                'success': False,
                "person": {
                    "name": {
                        "first_name": "Leo",
                        "last_name": "Lee",
                    },
                    "age": 29,
                    "cities": ["Guangzhou", "Shenzhen"]
                }
            }
        )

        extract_binds_list = [
            {"resp_content_dict_key_error": "content.not_exist"}
        ]
        resp_obj = response.ResponseObject(resp)

        with self.assertRaises(exception.ParseResponseError):
            resp_obj.extract_response(extract_binds_list)

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

        with self.assertRaises(exception.ParseResponseError):
            resp_obj.extract_response(extract_binds_list)
예제 #3
0
    def test_extract_response_fail(self):
        resp = requests.post(url="{}/anything".format(HTTPBIN_SERVER),
                             json={
                                 'success': False,
                                 "person": {
                                     "name": {
                                         "first_name": "Leo",
                                         "last_name": "Lee",
                                     },
                                     "age": 29,
                                     "cities": ["Guangzhou", "Shenzhen"]
                                 }
                             })

        extract_binds_list = [{
            "resp_content_dict_key_error":
            "content.not_exist"
        }]
        resp_obj = response.ResponseObject(resp)

        with self.assertRaises(exceptions.ExtractFailure):
            resp_obj.extract_response(extract_binds_list)

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

        with self.assertRaises(exceptions.ExtractFailure):
            resp_obj.extract_response(extract_binds_list)
예제 #4
0
    def test_extract_response_empty(self):
        resp = requests.post(url="http://127.0.0.1:3458/anything", data="abc")

        extract_binds_list = [{"resp_content_body": "content.data"}]
        resp_obj = response.ResponseObject(resp)
        extract_binds_dict = resp_obj.extract_response(extract_binds_list)
        self.assertEqual(extract_binds_dict["resp_content_body"], 'abc')

        extract_binds_list = [{"resp_content_body": "content.data.def"}]
        resp_obj = response.ResponseObject(resp)
        with self.assertRaises(exceptions.ExtractFailure):
            resp_obj.extract_response(extract_binds_list)
예제 #5
0
 def test_extract_text_response_exception(self):
     resp = requests.post(url="http://127.0.0.1:3458/anything",
                          data="LB123abcRB789")
     extract_binds_list = [{"resp_content_key1": "LB123.*RB789"}]
     resp_obj = response.ResponseObject(resp)
     with self.assertRaises(exceptions.ParamsError):
         resp_obj.extract_response(extract_binds_list)
예제 #6
0
    def test_extract_text_response(self):
        resp = requests.post(
            url="http://127.0.0.1:3458/anything",
            data="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"
        )
예제 #7
0
 def test_extract_text_response_exception(self):
     resp = requests.post(url="{}/anything".format(HTTPBIN_SERVER),
                          data="LB123abcRB789")
     extract_binds_list = [{"resp_content_key1": "LB123.*RB789"}]
     resp_obj = response.ResponseObject(resp)
     with self.assertRaises(exceptions.ParamsError):
         resp_obj.extract_response(extract_binds_list)
예제 #8
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",
            "expect": 201
        }, {
            "check": "body_success",
            "comparator": "eq"
        }]
        variables_mapping = {}
        with self.assertRaises(exception.ValidationError):
            resp_obj.validate(validators, variables_mapping)

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

        validators = [
            {"eq": ["$resp_status_code", 201]},
            {"check": "$resp_status_code", "comparator": "eq", "expect": 201},
            {"check": "$resp_body_success", "comparator": "eq", "expect": True},
            {"check": "${is_status_code_200($resp_status_code)}", "comparator": "eq", "expect": False}
        ]
        variables = [
            {"resp_status_code": 200},
            {"resp_body_success": True}
        ]
        self.context.bind_variables(variables)

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

        variables = [
            {"resp_status_code": 201},
            {"resp_body_success": True}
        ]
        self.context.bind_variables(variables)
        from tests.debugtalk import is_status_code_200
        functions = {
            "is_status_code_200": is_status_code_200
        }
        self.context.bind_functions(functions)

        self.assertTrue(self.context.validate(validators, resp_obj))
예제 #10
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))
예제 #11
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 = [{
            "eq": ["$resp_status_code", 201]
        }, {
            "check": "$resp_status_code",
            "comparator": "eq",
            "expect": 201
        }, {
            "check": "$resp_body_success",
            "comparator": "eq",
            "expect": True
        }]
        variables = []
        self.context.bind_variables(variables)

        with self.assertRaises(exception.ParamsError):
            self.context.validate(validators, resp_obj)

        # expected value missed in variables mapping
        variables = [{"resp_status_code": 200}]
        self.context.bind_variables(variables)

        with self.assertRaises(exception.ValidationError):
            self.context.validate(validators, resp_obj)
예제 #12
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"
        )
예제 #13
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 = [{
            "eq": ["$resp_status_code", 201]
        }, {
            "check": "$resp_status_code",
            "comparator": "eq",
            "expect": 201
        }]
        variables = []
        self.context.update_context_variables(variables, "teststep")

        with self.assertRaises(exceptions.VariableNotFound):
            self.context.validate(validators, resp_obj)

        # expected value missed in variables mapping
        variables = [{"resp_status_code": 200}]
        self.context.update_context_variables(variables, "teststep")

        with self.assertRaises(exceptions.ValidationFailure):
            self.context.validate(validators, resp_obj)
예제 #14
0
    def test_validate(self):
        url = "http://127.0.0.1:5000/"
        resp = requests.get(url)
        resp_obj = response.ResponseObject(resp)

        validators = [
            {"eq": ["$resp_status_code", 201]},
            {"check": "$resp_status_code", "comparator": "eq", "expect": 201},
            {"check": "$resp_body_success", "comparator": "eq", "expect": True}
        ]
        variables = {
            "resp_status_code": 200,
            "resp_body_success": True
        }

        self.context.init_test_variables(variables)

        with self.assertRaises(exceptions.ValidationFailure):
            self.context.validate(validators, resp_obj)

        validators = [
            {"eq": ["$resp_status_code", 201]},
            {"check": "$resp_status_code", "comparator": "eq", "expect": 201},
            {"check": "$resp_body_success", "comparator": "eq", "expect": True},
            {"check": "${is_status_code_200($resp_status_code)}", "comparator": "eq", "expect": False}
        ]
        variables = [
            {"resp_status_code": 201},
            {"resp_body_success": True}
        ]
        self.context.init_test_variables(variables)
        self.context.validate(validators, resp_obj)

        self.context.validate([], resp_obj)
        self.assertEqual(self.context.validation_results, [])
예제 #15
0
    def test_extract_response_json_string(self):
        resp = requests.post(url="http://127.0.0.1:3458/anything", data="abc")

        extract_binds_list = [{"resp_content_body": "content.data"}]
        resp_obj = response.ResponseObject(resp)

        extract_binds_dict = resp_obj.extract_response(extract_binds_list)
        self.assertEqual(extract_binds_dict["resp_content_body"], "abc")
예제 #16
0
    def test_extract_response_json_string(self):
        resp = requests.post(url="{}/anything".format(HTTPBIN_SERVER),
                             data="abc")

        extract_binds_list = [{"resp_content_body": "content.data"}]
        resp_obj = response.ResponseObject(resp)

        extract_binds_dict = resp_obj.extract_response(extract_binds_list)
        self.assertEqual(extract_binds_dict["resp_content_body"], "abc")
예제 #17
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)
     self.assertTrue(hasattr(resp_obj, 'status_code'))
     self.assertTrue(hasattr(resp_obj, 'headers'))
     self.assertTrue(hasattr(resp_obj, 'content'))
     self.assertIn('Content-Type', resp_obj.headers)
     self.assertIn('Content-Length', resp_obj.headers)
     self.assertIn('success', resp_obj.json)
예제 #18
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']))
예제 #19
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'])
예제 #20
0
    def test_extract_response_others(self):
        resp = requests.get(url="http://127.0.0.1:3458/status/200")
        resp_obj = response.ResponseObject(resp)

        extract_binds_list = [{
            "resp_others_encoding": "encoding"
        }, {
            "resp_others_history": "history"
        }]
        with self.assertRaises(exceptions.ParamsError):
            resp_obj.extract_response(extract_binds_list)
예제 #21
0
    def test_extract_response_others(self):
        resp = requests.get(url="{}/status/200".format(HTTPBIN_SERVER))
        resp_obj = response.ResponseObject(resp)

        extract_binds_list = [{
            "resp_others_encoding": "encoding"
        }, {
            "resp_others_history": "history"
        }]
        with self.assertRaises(exceptions.ParamsError):
            resp_obj.extract_response(extract_binds_list)
예제 #22
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)
예제 #23
0
    def test_extract_response_status_code(self):
        resp = requests.get(url="{}/status/200".format(HTTPBIN_SERVER))
        resp_obj = response.ResponseObject(resp)

        extract_binds_list = [{"resp_status_code": "status_code"}]
        extract_binds_dict = resp_obj.extract_response(extract_binds_list)

        self.assertEqual(extract_binds_dict["resp_status_code"], 200)

        extract_binds_list = [{"resp_status_code": "status_code.xx"}]
        with self.assertRaises(exceptions.ParamsError):
            resp_obj.extract_response(extract_binds_list)
예제 #24
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 = resp_obj.extract_response(extract_binds_list)

        self.assertEqual(extract_binds_dict["resp_status_code"], 200)
        self.assertEqual(extract_binds_dict["resp_headers_content_type"],
                         "application/json")
        self.assertEqual(extract_binds_dict["resp_content_body_success"],
                         False)
        self.assertEqual(extract_binds_dict["resp_content_content_success"],
                         False)
        self.assertEqual(extract_binds_dict["resp_content_text_success"],
                         False)
        self.assertEqual(extract_binds_dict["resp_content_person_first_name"],
                         "Leo")
        self.assertEqual(extract_binds_dict["resp_content_cities_1"],
                         "Shenzhen")
예제 #25
0
    def test_extract_response_cookies(self):
        resp = requests.get(url="{}/cookies".format(HTTPBIN_SERVER),
                            headers={"accept": "application/json"})
        resp_obj = response.ResponseObject(resp)

        extract_binds_list = [{"resp_cookies": "cookies"}]
        extract_binds_dict = resp_obj.extract_response(extract_binds_list)
        self.assertEqual(extract_binds_dict["resp_cookies"], {})

        extract_binds_list = [{"resp_cookies": "cookies.xx"}]
        with self.assertRaises(exceptions.ExtractFailure):
            resp_obj.extract_response(extract_binds_list)
예제 #26
0
    def test_extract_response_status_code(self):
        resp = requests.get(url="http://127.0.0.1:3458/status/200")
        resp_obj = response.ResponseObject(resp)

        extract_binds_list = [{"resp_status_code": "status_code"}]
        extract_binds_dict = resp_obj.extract_response(extract_binds_list)

        self.assertEqual(extract_binds_dict["resp_status_code"], 200)

        extract_binds_list = [{"resp_status_code": "status_code.xx"}]
        with self.assertRaises(exceptions.ParamsError):
            resp_obj.extract_response(extract_binds_list)
예제 #27
0
    def test_extract_response_body_html(self):
        resp = requests.get(url=HTTPBIN_SERVER)
        resp_obj = response.ResponseObject(resp)

        extract_binds_list = [{"resp_content": "content"}]
        extract_binds_dict = resp_obj.extract_response(extract_binds_list)

        self.assertIsInstance(extract_binds_dict["resp_content"], str)
        self.assertIn("httpbin.org", extract_binds_dict["resp_content"])

        extract_binds_list = [{"resp_content": "content.xxx"}]
        with self.assertRaises(exceptions.ExtractFailure):
            resp_obj.extract_response(extract_binds_list)
예제 #28
0
    def test_extract_response_body_html(self):
        resp = requests.get(url="http://127.0.0.1:3458/")
        resp_obj = response.ResponseObject(resp)

        extract_binds_list = [{"resp_content": "content"}]
        extract_binds_dict = resp_obj.extract_response(extract_binds_list)

        self.assertIsInstance(extract_binds_dict["resp_content"], str)
        self.assertIn("python-requests.org",
                      extract_binds_dict["resp_content"])

        extract_binds_list = [{"resp_content": "content.xxx"}]
        with self.assertRaises(exceptions.ExtractFailure):
            resp_obj.extract_response(extract_binds_list)
예제 #29
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_list = [{"resp_content_body": "content"}]
        resp_obj = response.ResponseObject(resp)

        extract_binds_dict = resp_obj.extract_response(extract_binds_list)
        self.assertEqual(extract_binds_dict["resp_content_body"], "abc")
예제 #30
0
    def test_extract_text_response(self):
        resp = requests.post(url="{}/anything".format(HTTPBIN_SERVER),
                             data="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")