[Mod] 改名:将restful改名为network

This commit is contained in:
nanoric 2018-10-09 06:04:05 -04:00
parent 7d65170e53
commit f3f2598c44
4 changed files with 35 additions and 17 deletions

View File

@ -1,8 +1,8 @@
import unittest import unittest
# noinspection PyUnresolvedReferences # noinspection PyUnresolvedReferences
from tests.restful.RestfulClientTest import * from tests.network.RestfulClientTest import *
from tests.restful.WebSocketClientTest import * from tests.network.WebSocketClientTest import *
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -1,11 +1,16 @@
# encoding: UTF-8 # encoding: UTF-8
import json import json
import traceback
import unittest import unittest
from simplejson import JSONDecodeError
from Promise import Promise from Promise import Promise
from vnpy.network.HttpClient import HttpClient, requestsSessionProvider from vnpy.network.HttpClient import HttpClient
class FailedError(RuntimeError):
pass
class TestHttpClient(HttpClient): class TestHttpClient(HttpClient):
@ -22,9 +27,11 @@ class TestHttpClient(HttpClient):
return method, path, params, data, {'Content-Type': 'application/json'} return method, path, params, data, {'Content-Type': 'application/json'}
def onError(self, exceptionType, exceptionValue, tb, req): def onError(self, exceptionType, exceptionValue, tb, req):
traceback.print_exception(exceptionType, exceptionValue, tb)
self.p.set_exception(exceptionValue) self.p.set_exception(exceptionValue)
def onFailed(self, httpStatusCode, data, req):
self.p.set_exception(FailedError("request failed"))
class RestfulClientTest(unittest.TestCase): class RestfulClientTest(unittest.TestCase):
@ -39,11 +46,8 @@ class RestfulClientTest(unittest.TestCase):
args = {'user': 'username', args = {'user': 'username',
'pw': 'password'} 'pw': 'password'}
def callback(code, data, req): def callback(data, req):
if code == 200: self.c.p.set_result(data['args'])
self.c.p.set_result(data['args'])
return
self.c.p.set_result(False)
self.c.addReq('GET', '/get', callback, params=args) self.c.addReq('GET', '/get', callback, params=args)
res = self.c.p.get(3) res = self.c.p.get(3)
@ -54,13 +58,27 @@ class RestfulClientTest(unittest.TestCase):
body = {'user': 'username', body = {'user': 'username',
'pw': 'password'} 'pw': 'password'}
def callback(code, data, req): def callback(data, req):
if code == 200: self.c.p.set_result(data['json'])
self.c.p.set_result(data['json'])
return
self.c.p.set_result(False)
self.c.addReq('POST', '/post', callback, data=body) self.c.addReq('POST', '/post', callback, data=body)
res = self.c.p.get(3) res = self.c.p.get(3)
self.assertEqual(body, res) self.assertEqual(body, res)
def test_addReq_onFailed(self):
def callback(data, req):
pass
self.c.addReq('POST', '/status/201', callback)
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)