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)
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)
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)
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)
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)
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" )
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)
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)
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))
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))
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)
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" )
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)
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, [])
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")
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")
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)
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']))
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'])
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)
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)
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)
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)
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")
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)
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)
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)
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)
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")
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")