From f3f2598c440b25d9ce69f1e33762f5f5c1fb0328 Mon Sep 17 00:00:00 2001 From: nanoric Date: Tue, 9 Oct 2018 06:04:05 -0400 Subject: [PATCH] =?UTF-8?q?[Mod]=20=E6=94=B9=E5=90=8D=EF=BC=9A=E5=B0=86res?= =?UTF-8?q?tful=E6=94=B9=E5=90=8D=E4=B8=BAnetwork?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/all_test.py | 4 +- .../{restful => network}/RestfulClientTest.py | 48 +++++++++++++------ .../WebSocketClientTest.py | 0 tests/{restful => network}/__init__.py | 0 4 files changed, 35 insertions(+), 17 deletions(-) rename tests/{restful => network}/RestfulClientTest.py (59%) rename tests/{restful => network}/WebSocketClientTest.py (100%) rename tests/{restful => network}/__init__.py (100%) diff --git a/tests/all_test.py b/tests/all_test.py index ff37afc2..d8480b5f 100644 --- a/tests/all_test.py +++ b/tests/all_test.py @@ -1,8 +1,8 @@ import unittest # noinspection PyUnresolvedReferences -from tests.restful.RestfulClientTest import * -from tests.restful.WebSocketClientTest import * +from tests.network.RestfulClientTest import * +from tests.network.WebSocketClientTest import * if __name__ == "__main__": unittest.main() diff --git a/tests/restful/RestfulClientTest.py b/tests/network/RestfulClientTest.py similarity index 59% rename from tests/restful/RestfulClientTest.py rename to tests/network/RestfulClientTest.py index 4a35072e..8cf8eaaf 100644 --- a/tests/restful/RestfulClientTest.py +++ b/tests/network/RestfulClientTest.py @@ -1,15 +1,20 @@ # encoding: UTF-8 import json -import traceback import unittest +from simplejson import JSONDecodeError + from Promise import Promise -from vnpy.network.HttpClient import HttpClient, requestsSessionProvider +from vnpy.network.HttpClient import HttpClient + + +class FailedError(RuntimeError): + pass class TestHttpClient(HttpClient): - + def __init__(self): urlBase = 'https://httpbin.org' super(TestHttpClient, self).__init__() @@ -22,9 +27,11 @@ class TestHttpClient(HttpClient): return method, path, params, data, {'Content-Type': 'application/json'} def onError(self, exceptionType, exceptionValue, tb, req): - traceback.print_exception(exceptionType, exceptionValue, tb) self.p.set_exception(exceptionValue) + def onFailed(self, httpStatusCode, data, req): + self.p.set_exception(FailedError("request failed")) + class RestfulClientTest(unittest.TestCase): @@ -38,12 +45,9 @@ class RestfulClientTest(unittest.TestCase): def test_addReq_get(self): args = {'user': 'username', 'pw': 'password'} - - def callback(code, data, req): - if code == 200: - self.c.p.set_result(data['args']) - return - self.c.p.set_result(False) + + def callback(data, req): + self.c.p.set_result(data['args']) self.c.addReq('GET', '/get', callback, params=args) res = self.c.p.get(3) @@ -54,13 +58,27 @@ class RestfulClientTest(unittest.TestCase): body = {'user': 'username', 'pw': 'password'} - def callback(code, data, req): - if code == 200: - self.c.p.set_result(data['json']) - return - self.c.p.set_result(False) + def callback(data, req): + self.c.p.set_result(data['json']) self.c.addReq('POST', '/post', callback, data=body) res = self.c.p.get(3) 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) + diff --git a/tests/restful/WebSocketClientTest.py b/tests/network/WebSocketClientTest.py similarity index 100% rename from tests/restful/WebSocketClientTest.py rename to tests/network/WebSocketClientTest.py diff --git a/tests/restful/__init__.py b/tests/network/__init__.py similarity index 100% rename from tests/restful/__init__.py rename to tests/network/__init__.py