示例#1
0
    def test_post(self):
        url = 'http://127.0.0.1:%d/test_post' % self.port
        param = {'a':11,'b':'22','c':'哈','d':u'哈','e':None}
        result = '{"use_time": "0.0003", "reason": "\u8bbf\u95ee\u6210\u529f", "version": "2.0.0", "result": 0}'

        @tornado_util.fn(url=r"/test_post/?", method='post')
        def get_test_post(self, **kwargs):
            if self.request.body is None:
                return {"result": -1, "reason":'这是GET请求,请求方式有误!'}
            if (self.get_argument('a', '') == '11' and self.get_argument('b', '') == '22' and self.get_argument('c', '') == '哈' and
                self.get_argument('d', '') == '哈' and self.get_argument('e', '') == ''):
                return result
            else:
                return kwargs

        # 无参数请求,原样返回
        res = http_util.post(url)
        assert isinstance(res, basestring)
        assert res == "{}"

        # 参数转换
        res = http_util.post(url, param)
        assert isinstance(res, basestring)
        assert res == result
        res2 = http_util.post(url, {'b':'22','c':'哈','d':u'哈','e':0})
        assert isinstance(res2, basestring)
        assert str_util.to_json(res2) == {'b':'22','c':'哈','d':u'哈','e':'0'}

        # return_json 返回结果转换
        res = http_util.post(url, param, return_json=True)
        assert isinstance(res, dict)
        assert res == {"use_time": "0.0003", "reason": "访问成功", "version": "2.0.0", "result": 0}
示例#2
0
    def test_files(self):
        url = 'http://127.0.0.1:%d/test_files' % self.port
        result = '{"use_time": "0.0003", "reason": "\u8bbf\u95ee\u6210\u529f", "version": "2.0.0", "result": 0}'

        @tornado_util.fn(url=r"/test_files/?")
        def get_test_files(self, **kwargs):
            files = self.request.files
            for name, file_metas in files.items():
                for meta in file_metas:
                    filename = meta.get('filename', '')
                    if filename not in (__file__, '__init__.pyc'):
                        return {"result": -1, "reason":'文件名有误!', 'file_name':filename}
                    file = open(filename, mode="rb")
                    file_content = file.read()
                    file.close()
                    if meta.get('body', '') != file_content:
                        return {"result": -2, "reason":'文件内容有误!', 'file_name':filename}
            if len(files) != 2:
                return {"result": -3, "reason":'文件数量有误!', 'file_length':len(files)}
            return result

        # 文件上传请求
        res = http_util.post(url, files=[
            {'name':'file1', 'file':__file__},
            {'name':'file2', 'file':'__init__.pyc'},
        ])
        assert isinstance(res, basestring)
        assert res == result
示例#3
0
    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)
示例#4
0
    def test_gzip(self):
        url = 'http://127.0.0.1:%d/test_gzip' % self.port
        result = '{"use_time": "0.0003", "reason": "\u8bbf\u95ee\u6210\u529f", "version": "2.0.0", "result": 0}' * 100

        @tornado_util.fn(url=r"/test_gzip/?", gzip_length=10)
        def get_test_gzip(self, **kwargs):
            if self.request.headers.get("Accept-Encoding",
                                        "") in ('gzip', 'deflate'):
                return result
            else:
                return {"result": -1, "reason": '这是没有压缩的请求,请求方式有误!'}

        # get 请求
        global NOW_LOG_RECORD
        NOW_LOG_RECORD = []
        res = http_util.get(url, gzip=True)
        assert isinstance(res, basestring)
        assert res == result

        assert len(NOW_LOG_RECORD) >= 2
        record = NOW_LOG_RECORD[-2]  # 倒数第二条日志,正是解压日志
        assert record is not None
        assert record.levelno == logging.INFO
        assert '压缩请求' in record.msg
        assert record.method == 'GET'
        assert record.befor_length  # 有写入解压前长度
        assert record.after_length  # 有写入解压后长度
        assert record.after_length == len(result)
        assert record.befor_length < record.after_length  # 解压后长度更长

        # post 请求
        NOW_LOG_RECORD = []
        res = http_util.post(url, gzip=True)
        assert isinstance(res, basestring)
        assert res == result

        assert len(NOW_LOG_RECORD) >= 2
        record = NOW_LOG_RECORD[-2]  # 倒数第二条日志,正是解压日志
        assert record is not None
        assert record.levelno == logging.INFO
        assert '压缩请求' in record.msg
        assert record.method == 'POST'
        assert record.befor_length  # 有写入解压前长度
        assert record.after_length  # 有写入解压后长度
        assert record.after_length == len(result)
        assert record.befor_length < record.after_length  # 解压后长度更长
示例#5
0
    def test_threads(self):
        # 设成默认异步请求
        http_util.init(threads=True)
        url = 'http://127.0.0.1:%d/test_threads' % self.port
        result = '{"use_time": "0.0003", "reason": "\u8bbf\u95ee\u6210\u529f", "version": "2.0.0", "result": 0}'

        @tornado_util.fn(url=r"/test_threads/?")
        def get_test_threads(self, **kwargs):
            return result

        # 异步 GET 请求,返回线程
        global NOW_LOG_RECORD
        NOW_LOG_RECORD = []
        th1 = http_util.get(url)
        assert len(NOW_LOG_RECORD) == 0 # 通过日志查看有没有发启线程,因为发启线程肯定没有这么快打印日志
        assert isinstance(th1, threading.Thread)
        th1.join() # 等待线程返回,以便检查日志
        assert len(NOW_LOG_RECORD) >= 1
        record = NOW_LOG_RECORD[0]
        assert record is not None
        assert record.levelno == logging.INFO
        assert record.method == 'GET'
        log_msg = record.getMessage()
        assert url in log_msg
        assert result in log_msg

        # 异步 POST 请求,返回线程
        NOW_LOG_RECORD = []
        th2 = http_util.post(url)
        assert len(NOW_LOG_RECORD) == 0 # 通过日志查看有没有发启线程,因为发启线程肯定没有这么快打印日志
        assert isinstance(th2, threading.Thread)
        th2.join() # 等待线程返回,以便检查日志
        assert len(NOW_LOG_RECORD) >= 1
        record = NOW_LOG_RECORD[0]
        assert record is not None
        assert record.levelno == logging.INFO
        assert record.method == 'POST'
        log_msg = record.getMessage()
        assert url in log_msg
        assert result in log_msg

        # 改回默认值,避免影响其它测试
        http_util.init(threads=False)