2018-10-07 07:33:33 +00:00
|
|
|
# encoding: UTF-8
|
|
|
|
|
|
|
|
import json
|
|
|
|
import unittest
|
|
|
|
|
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-10 09:36:00 +00:00
|
|
|
from vnpy.network.RestClient import RestClient, Request
|
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-10 07:57:46 +00:00
|
|
|
def beforeRequest(self, req): #type: (Request)->Request
|
|
|
|
req.data = json.dumps(req.data)
|
|
|
|
req.headers = {'Content-Type': 'application/json'}
|
|
|
|
return req
|
2018-10-07 07:33:33 +00:00
|
|
|
|
|
|
|
def onError(self, exceptionType, exceptionValue, tb, req):
|
|
|
|
self.p.set_exception(exceptionValue)
|
|
|
|
|
2018-10-11 02:09:15 +00:00
|
|
|
def onFailed(self, httpStatusCode, req):
|
2018-10-09 10:04:05 +00:00
|
|
|
self.p.set_exception(FailedError("request failed"))
|
|
|
|
|
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
|
|
|
|
|
|
|
self.c.addReq('GET', '/get', callback, params=args)
|
|
|
|
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
|
|
|
|
|
|
|
self.c.addReq('POST', '/post', callback, data=body)
|
|
|
|
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-11 02:09:15 +00:00
|
|
|
self.c.addReq('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
|
|
|
|
|
|
|
|
self.c.addReq('GET', '/image/svg', callback)
|
|
|
|
with self.assertRaises(JSONDecodeError):
|
|
|
|
self.c.p.get(3)
|
|
|
|
|