def test_json2str(self): # json to str 测试,要求保持 list 顺序,中文兼容,双引号括起字符串(字符串里面的双引号前面有斜杠转移) value = { 'a': [1, 2, '呵xx呵', u'哈"哈', str_util.to_str('智汇云', encode='GBK')] } json_value = '{"a": [1, 2, "\\u5475xx\\u5475", "\\u54c8\\"\\u54c8", "\\u667a\\u6c47\\u4e91"]}' assert str_util.json2str(value) == json_value assert str_util.to_json(json_value) == str_util.deep_str(value) # 加入时间测试 value2 = { 'a': [ 1, 2, '呵xx呵', u'哈"哈', str_util.to_str('智汇云', encode='GBK'), time.strptime('2014/03/25 19:05:33', '%Y/%m/%d %H:%M:%S') ], u'eff_time': datetime.datetime(2015, 6, 28, 14, 19, 41), } value3 = { 'a': [ 1, 2, '呵xx呵', u'哈"哈', str_util.to_str('智汇云', encode='GBK'), "2014-03-25 19:05:33" ], u'eff_time': "2015-06-28 14:19:41" } json_value2 = '{"a": [1, 2, "\\u5475xx\\u5475", "\\u54c8\\"\\u54c8", "\\u667a\\u6c47\\u4e91", "2014-03-25 19:05:33"], "eff_time": "2015-06-28 14:19:41"}' assert str_util.json2str(value2) == json_value2 assert str_util.to_json(json_value2) == str_util.deep_str(value3) # eval 能将非标准json的内容转换过来 json_value = u"{'a': [1, 2, '\\u5475xx\\u5475', '\\xE5\\x8C\\x85']}" assert str_util.to_json(json_value) == eval(json_value)
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}
def test_put_patch(self): url = 'http://127.0.0.1:%d/test_put_patch' % 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}' methods = ['put', 'patch'] @tornado_util.fn(url=r"/test_put_patch/?", method=methods) def test_put_patch(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', '') == u'哈' and self.get_argument('d', '') == u'哈' and self.get_argument('e', '') == ''): return result else: return kwargs for method in methods: fun = getattr(http_util, method) # 无参数请求,原样返回 res = fun(url) assert isinstance(res, basestring) assert res == "{}" # 参数转换 res = fun(url, param) assert isinstance(res, basestring) assert res == result res2 = fun(url, {'b': '22', 'c': '哈', 'd': u'哈', 'e': 0}) assert isinstance(res2, basestring) assert str_util.to_json(res2) == { 'b': '22', 'c': u'哈', 'd': u'哈', 'e': '0' } # return_json 返回结果转换 res = fun(url, param, return_json=True) assert isinstance(res, dict) assert res == { "use_time": "0.0003", "reason": u"访问成功", "version": "2.0.0", "result": 0 } # get 请求,访问不了 res = http_util.get(url) assert isinstance(res, basestring) assert res == "<html><title>405: Method Not Allowed</title><body>405: Method Not Allowed</body></html>" # post 请求,访问不了 res = http_util.post(url) assert isinstance(res, basestring) assert res == "<html><title>405: Method Not Allowed</title><body>405: Method Not Allowed</body></html>"
def test_get(self): url = 'http://127.0.0.1:%d/test_get' % self.port param_url = url + '?a=11&b=22&c=%E5%93%88&d=%E5%93%88&e=' result = '{"use_time": "0.0003", "reason": "\u8bbf\u95ee\u6210\u529f", "version": "2.0.0", "result": 0}' @tornado_util.fn(url=r"/test_get/?", method='get') def get_test_get(self, **kwargs): if self.request.body: return {"result": -1, "reason":'这是POST请求,请求方式有误!'} if (self.get_argument('a', '') == '11' and self.get_argument('b', '') == '22' and self.get_argument('c', '') == u'哈' and self.get_argument('d', '') == u'哈' and self.get_argument('e', '') == ''): return result else: return kwargs # 普通请求,原样返回 res = http_util.get(param_url) assert isinstance(res, basestring) assert res == result # 参数转换 res = http_util.get(url, {'a':11,'b':'22','c':'哈','d':u'哈','e':None}) assert isinstance(res, basestring) assert res == result res2 = http_util.get(url, {'b':'22','c':'哈','d':u'哈','e':0}) assert isinstance(res2, basestring) assert str_util.to_json(res2) == {'b':'22','c':u'哈','d':u'哈','e':'0'} # return_json 返回结果转换 res = http_util.get(param_url, return_json=True) assert isinstance(res, dict) assert res == {"use_time": "0.0003", "reason": u"访问成功", "version": "2.0.0", "result": 0}
def test_now_page(self): logging.info(u'参数接收测试') response = self.client.get('/now/?t=0.0001') self.failUnlessEqual(response.status_code, 200) self.failUnlessEqual(str_util.to_json(response.content), { 't': '0.0001', 't2': '0.0001' })
def test_send_json(self): url = 'http://127.0.0.1:%d/test_send_json' % self.port result = '{"use_time": "0.0003", "reason": "\u8bbf\u95ee\u6210\u529f", "version": "2.0.0", "result": 0}' @tornado_util.fn(url=r"/test_send_json/?", method='post') def test_send_json(self, **kwargs): if self.request.body is None: return {"result": -1, "reason": '这是GET请求,请求方式有误!'} return str_util.to_json(self.request.body) # 提交 json param = {'a': 11, 'b': '22', 'c': '哈', 'd': u'哈', 'e': None} res = http_util.post(url, param, send_json=True) assert isinstance(res, basestring) assert str_util.to_json(res) == param param2 = {'b': '22', 'c': '哈', 'd': u'哈', 'e': 0} res2 = http_util.post(url, param2, send_json=True) assert isinstance(res2, basestring) assert str_util.to_json(res2) == param2
def test_warn_time(self): logging.info(u'延迟到警告时间的访问测试') django_api.init(warn_time=-1) # 设置过期时间为必然过期的 response = self.client.get('/now/?t=0.0001') self.failUnlessEqual(response.status_code, 200) self.failUnlessEqual(str_util.to_json(response.content), { 't': '0.0001', 't2': '0.0001' }) global NOW_LOG_RECORD record = NOW_LOG_RECORD assert record is not None assert record.levelno == logging.WARNING assert u'接口耗时太长' in record.msg
def test_send_json(self, **kwargs): if self.request.body is None: return {"result": -1, "reason": '这是GET请求,请求方式有误!'} return str_util.to_json(self.request.body)