2018-10-07 07:33:33 +00:00
|
|
|
# encoding: UTF-8
|
|
|
|
|
|
|
|
import json
|
2018-10-31 02:50:26 +00:00
|
|
|
import sys
|
2018-10-07 07:33:33 +00:00
|
|
|
import unittest
|
2018-10-31 02:50:26 +00:00
|
|
|
import uuid
|
2018-10-07 07:33:33 +00:00
|
|
|
|
2018-10-09 10:04:05 +00:00
|
|
|
from simplejson import JSONDecodeError
|
|
|
|
|
2018-10-07 07:33:33 +00:00
|
|
|
from Promise import Promise
|
2018-10-31 02:50:26 +00:00
|
|
|
from vnpy.api.rest.RestClient import Request, RestClient
|
2018-10-09 10:04:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FailedError(RuntimeError):
|
|
|
|
pass
|
2018-10-07 07:33:33 +00:00
|
|
|
|
|
|
|
|
2018-10-10 09:36:00 +00:00
|
|
|
class TestRestClient(RestClient):
|
2018-10-09 10:04:05 +00:00
|
|
|
|
2018-10-07 07:33:33 +00:00
|
|
|
def __init__(self):
|
|
|
|
urlBase = 'https://httpbin.org'
|
2018-10-10 09:36:00 +00:00
|
|
|
super(TestRestClient, self).__init__()
|
2018-10-07 08:19:48 +00:00
|
|
|
self.init(urlBase)
|
2018-10-07 07:33:33 +00:00
|
|
|
|
|
|
|
self.p = Promise()
|
|
|
|
|
2018-10-18 09:22:38 +00:00
|
|
|
def sign(self, req): #type: (Request)->Request
|
2018-10-10 07:57:46 +00:00
|
|
|
req.data = json.dumps(req.data)
|
|
|
|
req.headers = {'Content-Type': 'application/json'}
|
|
|
|
return req
|
2018-10-31 02:50:26 +00:00
|
|
|
|
2018-10-07 07:33:33 +00:00
|
|
|
def onError(self, exceptionType, exceptionValue, tb, req):
|
2018-10-11 02:26:45 +00:00
|
|
|
self.p.set_exception(exceptionType, exceptionValue, tb)
|
2018-10-07 07:33:33 +00:00
|
|
|
|
2018-10-11 02:09:15 +00:00
|
|
|
def onFailed(self, httpStatusCode, req):
|
2018-10-11 02:26:45 +00:00
|
|
|
with self.p.catch():
|
|
|
|
raise FailedError("request failed")
|
2018-10-09 10:04:05 +00:00
|
|
|
|
2018-10-07 07:33:33 +00:00
|
|
|
|
|
|
|
class RestfulClientTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2018-10-10 09:36:00 +00:00
|
|
|
self.c = TestRestClient()
|
2018-10-07 07:33:33 +00:00
|
|
|
self.c.start()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.c.stop()
|
|
|
|
|
|
|
|
def test_addReq_get(self):
|
|
|
|
args = {'user': 'username',
|
|
|
|
'pw': 'password'}
|
2018-10-09 10:04:05 +00:00
|
|
|
|
|
|
|
def callback(data, req):
|
|
|
|
self.c.p.set_result(data['args'])
|
2018-10-07 07:33:33 +00:00
|
|
|
|
2018-10-15 07:11:17 +00:00
|
|
|
self.c.addRequest('GET', '/get', callback, params=args)
|
2018-10-07 07:33:33 +00:00
|
|
|
res = self.c.p.get(3)
|
|
|
|
|
|
|
|
self.assertEqual(args, res)
|
|
|
|
|
|
|
|
def test_addReq_post(self):
|
|
|
|
body = {'user': 'username',
|
|
|
|
'pw': 'password'}
|
|
|
|
|
2018-10-09 10:04:05 +00:00
|
|
|
def callback(data, req):
|
|
|
|
self.c.p.set_result(data['json'])
|
2018-10-07 07:33:33 +00:00
|
|
|
|
2018-10-15 07:11:17 +00:00
|
|
|
self.c.addRequest('POST', '/post', callback, data=body)
|
2018-10-07 07:33:33 +00:00
|
|
|
res = self.c.p.get(3)
|
|
|
|
|
|
|
|
self.assertEqual(body, res)
|
2018-10-09 10:04:05 +00:00
|
|
|
|
|
|
|
def test_addReq_onFailed(self):
|
|
|
|
def callback(data, req):
|
|
|
|
pass
|
|
|
|
|
2018-10-15 07:11:17 +00:00
|
|
|
self.c.addRequest('POST', '/status/401', callback)
|
2018-10-09 10:04:05 +00:00
|
|
|
with self.assertRaises(FailedError):
|
|
|
|
self.c.p.get(3)
|
|
|
|
|
|
|
|
def test_addReq_jsonParseError(self):
|
|
|
|
def callback(data, req):
|
|
|
|
pass
|
|
|
|
|
2018-10-15 07:11:17 +00:00
|
|
|
self.c.addRequest('GET', '/image/svg', callback)
|
2018-10-09 10:04:05 +00:00
|
|
|
with self.assertRaises(JSONDecodeError):
|
|
|
|
self.c.p.get(3)
|
2018-10-31 02:50:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RestfulClientErrorHandleTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.c = TestRestClient()
|
|
|
|
self.c.start()
|
|
|
|
|
|
|
|
self.org_sys_hook = sys.excepthook
|
|
|
|
self.org_sys_stderr_write = sys.stderr.write
|
|
|
|
|
|
|
|
sys.excepthook = self.nop
|
|
|
|
sys.stderr.write = self.nop
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.c.stop()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def nop(*args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_onError(self):
|
|
|
|
"""这个测试保证onError内不会再抛异常"""
|
|
|
|
target = uuid.uuid4()
|
|
|
|
|
|
|
|
def callback(data, req):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onError(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
super(TestRestClient, self.c).onError(*args, **kwargs)
|
|
|
|
self.c.p.set_result(target)
|
|
|
|
except:
|
|
|
|
self.c.p.set_result(False)
|
|
|
|
self.c.onError = onError
|
2018-10-09 10:04:05 +00:00
|
|
|
|
2018-10-31 02:50:26 +00:00
|
|
|
self.c.addRequest('GET', '/image/svg', callback)
|
|
|
|
|
|
|
|
res = self.c.p.get(3)
|
|
|
|
self.assertEqual(target, res)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|