def response_keys_type_is_right(self, *args, **kwargs): ''' 判断响应的python格式的指定字段格式如target_struct指定的格式. eg. response_keys_type_check("content", "list", 0, target_struct = {"name" : "STRING", "type" :"HASH", "index" : "INT", "verify" : "BOOL"}) #e.g response_keys_type_check(target_struct = {"name" : "STRING", "type" :"HASH", "index" : "INT", "verify" : "BOOL"}) 该结果表示将结果的result['content']['list'][0]同target_struct指定的字段和类型做对比 ''' target_struct = kwargs["target_struct"] content = self.conver_response_body_to_struct() for key in args: content = content[key] for key, value in target_struct.items(): #类型是None,需要特别处理 if value == "NULL": if content[key] is not None: Log.log_error_info( "Verify..., the type of key %s is not None type" % (key)) else: if not isinstance(content[key], HttpHandle.TYPES[value]): Log.log_error_info( "Verify..., the type of key %s is not %s " % (key, value)) Log.log_info("Verify..., response deep key type is ok.") Log.log_step("检查响应结构内容符合指定内容")
def test_get_header(): response = Response() instance = httpHandler.HttpHandle() instance.http_response = response header_dic = {"content-type": "text/html"} instance.set_header(header_dic) TestCase().assertTrue(header_dic == instance.get_header()) Log.log_info("test_get_header is ok")
def test_print_response_body(): content = b'{"content": true, "int":1}' response = Response() setattr(response, '_content', content) instance = httpHandler.HttpHandle() instance.http_response = response instance.print_response_body() Log.log_info("test_print_response_body is ok")
def response_dictionary_should_not_have_key(self, key): ''' 判断响应的格式有指定字段 ''' dic = self.conver_response_body_to_struct() if key in dic: Log.log_error_info("Verify..., response key has key: %s" % (key)) else: Log.log_info("Verify...,ok, response has not key..") Log.log_step("检查响应不含有字段:%s" % (key))
def response_body_should_be_dictionary_struct(self): ''' 判断响应是dictionary格式,否则报错 ''' if not isinstance(self.conver_response_body_to_struct(), HttpHandle.TYPES['HASH']): Log.log_error_info("Verify..., response struct is not dictionary.") else: Log.log_info("Verify..., response struct is ok.") Log.log_step("检查响应是HASH结构")
def response_body_should_be_list_struct(self): ''' 判断响应是list格式,否则报错 ''' if not isinstance(self.conver_response_body_to_struct(), HttpHandle.TYPES['LIST']): Log.log_error_info("status code should be list") else: Log.log_info("Verify..., response struct is ok.") Log.log_step("检查响应是list结构")
def response_string_should_not_include(self, sub_string): ''' 判断响应字符串不包含指定的字符串 ''' if sub_string in self.get_response_body(): Log.log_error_info( "Verify..., response string does inclue string: %s, real response is: %s" % (sub_string, self.get_response_body())) else: Log.log_info("Verify..., response content is ok.") Log.log_step("检查响应不含有字符串:%s" % (sub_string))
def response_code_status_should_be(self, status_code): ''' 判断响应的状态码是否是指定的状态码,不是就报错 ''' if status_code != self.get_response_status_code(): Log.log_error_info( "status code should be %d, but the real value is %d" % (status_code, self.get_response_status_code())) else: Log.log_info("Verify..., status code is ok.") Log.log_step("检查状态码是:%d" % (status_code))
def response_dictionary_should_have_key_value(self, key, value): ''' 判断响应的指定字段的内容是指定的内容 ''' dic = self.conver_response_body_to_struct() if dic.get(key) != value: Log.log_error_info( "Verify..., the response dictionary value is %s, not %s" % (dic.get(key), value)) else: Log.log_info("Verify..., response key,value is ok.") Log.log_step("检查响应含有字段:%s,值为%s" % (key, value))
def response_dictionary_should_have_not_keys(self, key_list=[]): ''' 判断响应的格式有指定的字段集合 ''' dic = self.conver_response_body_to_struct() for key in key_list: if key in dic: Log.log_error_info("Verify..., response key has key: %s" % (key)) else: Log.log_info("Verify..., ok, response has not keys .") Log.log_step("检查响应不含有这些字段:%s" % (key_list))
def test_update_header(): response = Response() instance = httpHandler.HttpHandle() instance.http_response = response header_dic = {"content-type": "text/html"} instance.set_header(header_dic) instance.update_header({"add": "test", "content-type": "test"}) header_dic = {"add": "test", "content-type": "test"} TestCase().assertTrue(header_dic == instance.get_header()) header_dic = {"content-type": "test"} instance.delete_header(['add']) TestCase().assertTrue(header_dic == instance.get_header()) Log.log_info("test_update_header is ok")
def response_key_value_is_right(self, *args, **kwargs): ''' 判断响应的python格式的指定字段格式如target_struct指定的格式. eg. response_deep_key_is_struct("content", "list", 0, target_value = {"name" : "benhuang", "index" : "7", "verify" : "true"}) 该结果表示将结果的result['content']['list'][0]同target_struct指定的字段与响应结果做对比 ''' target_struct = kwargs["target_value"] content = self.conver_response_body_to_struct() for key in args: content = content[key] for key, value in target_struct.items(): if content[key] != value: Log.log_error_info( "Verify..., the value of key %s is not %s,it is %s " % (key, value, content[key])) Log.log_info("Verify..., response deep key,value is ok.") Log.log_step("检查响应结构符合指定格式要求,且内容正确")
def test_get_response_body(): content = b'{"content": true, "int":1}' response = Response() setattr(response, '_content', content) instance = httpHandler.HttpHandle() instance.http_response = response TestCase().assertTrue( '{"content": true, "int":1}' == instance.get_response_body()) content = b'{"content": true, "int":1, "list:[]"}' response = Response() setattr(response, '_content', content) instance = httpHandler.HttpHandle() instance.http_response = response TestCase().assertTrue('{"content": true, "int":1, "list:[]"}' == instance.get_response_body()) Log.log_info("test_get_resonse_body is ok")
def test_replace_paramter_for_url(): origin_url = "http://www.baidu/{0}" instance = httpHandler.HttpHandle() parm0 = "news" new_url = instance.replace_paramter_for_url(origin_url, parm0) TestCase().assertTrue("http://www.baidu/news" == new_url) origin_url = "http://www.baidu/{0}/test/{1}" parm0 = "news" parm1 = "auto" new_url = instance.replace_paramter_for_url(origin_url, parm0, parm1) TestCase().assertTrue("http://www.baidu/news/test/auto" == new_url) origin_url = "http://www.baidu/{0}/test/{1}?name={2}" parm0 = "news" parm1 = "auto" parm2 = "ben" new_url = instance.replace_paramter_for_url(origin_url, parm0, parm1, parm2) TestCase().assertTrue( "http://www.baidu/news/test/auto?name=ben" == new_url) Log.log_info("test_replace_paramter_for_url is ok")
def print_response_python_strcut_body(self): ''' 打印转换成python结构的结果 ''' Log.log_info(str(self.get_response_struct()))
def print_response_body(self): ''' 打印结果 ''' Log.log_info(self.get_response_body())