def test_error(self): # 设成默认异步请求 http_util.init(repeat_time=3) url = 'http://127.0.0.1:%d/test_error' % self.port global error_times error_times = 0 # 自定义一个出错页面 class _ExceptionHandler(tornado_util.RequestHandler): def get(self): global error_times error_times += 1 raise Exception('出错测试') post = get # 添加到请求地址列表 tornado_util.add_apps(r"/test_error/?", _ExceptionHandler) # GET 请求,返回 None res = http_util.get(url) assert res is None assert error_times == 3 # 请求次数 # POST 请求,返回 None error_times = 0 res = http_util.post(url) assert res is None assert error_times == 3 # 请求次数 # 改回默认值,避免影响其它测试 http_util.init(repeat_time=1)
def test_method(self): url = 'http://127.0.0.1:%d/get_test_method/' % self.port # 定义处理各种请求方式的类 class MethodHandler(tornado_util.RequestHandler): def get(self): return self.finish('get') def post(self): return self.finish('post') def put(self): return self.finish('put') def delete(self): return self.finish('delete') def patch(self): return self.finish('patch') def options(self): return self.finish('options') tornado_util.add_apps(r"/get_test_method/", MethodHandler) # 测试请求 res = http_util.get(url) assert isinstance(res, basestring) and res == "get" res = http_util.post(url) assert isinstance(res, basestring) and res == "post" res = http_util.put(url) assert isinstance(res, basestring) and res == "put" res = http_util.delete(url) assert isinstance(res, basestring) and res == "delete" res = http_util.patch(url) assert isinstance(res, basestring) and res == "patch" res = http_util.options(url) assert isinstance(res, basestring) and res == "options"