forked from blinktrade/bitex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_serviceHandler.py
153 lines (116 loc) · 5.94 KB
/
test_serviceHandler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""
Copyright (c) 2007 Jan-Klaas Kollhof
This file is part of jsonrpc.
jsonrpc is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this software; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
import unittest
import jsonrpc
from types import *
class Service(object):
@jsonrpc.ServiceMethod
def echo(self, arg):
return arg
def not_a_serviceMethod(self):
pass
@jsonrpc.ServiceMethod
def raiseError(self):
raise Exception("foobar")
class Handler(jsonrpc.ServiceHandler):
def __init__(self, service):
self.service=service
def translateRequest(self, data):
self._requestTranslated=True
return jsonrpc.ServiceHandler.translateRequest(self, data)
def findServiceEndpoint(self, name):
self._foundServiceEndpoint=True
return jsonrpc.ServiceHandler.findServiceEndpoint(self, name)
def invokeServiceEndpoint(self, meth, params):
self._invokedEndpoint=True
return jsonrpc.ServiceHandler.invokeServiceEndpoint(self, meth, params)
def translateResult(self, result, error, id_):
self._resultTranslated=True
return jsonrpc.ServiceHandler.translateResult(self, result, error, id_)
class TestServiceHandler(unittest.TestCase):
def setUp(self):
self.service = Service()
def tearDown(self):
pass
def test_RequestProcessing(self):
handler = Handler(self.service)
json=jsonrpc.dumps({"method":"echo", 'params':['foobar'], 'id':''})
result = handler.handleRequest(json)
self.assert_(handler._requestTranslated)
self.assert_(handler._foundServiceEndpoint)
self.assert_(handler._invokedEndpoint)
self.assert_(handler._resultTranslated)
def test_translateRequest(self):
handler = Handler(self.service)
json=jsonrpc.dumps({"method":"echo", 'params':['foobar'], 'id':''})
req = handler.translateRequest(json)
self.assertEquals(req['method'], "echo")
self.assertEquals(req['params'],['foobar'])
self.assertEquals(req['id'],'')
def test_findServiceEndpoint(self):
handler = Handler(self.service)
self.assertRaises(jsonrpc.ServiceMethodNotFound, handler.findServiceEndpoint, "notfound")
self.assertRaises(jsonrpc.ServiceMethodNotFound, handler.findServiceEndpoint, "not_a_serviceMethod")
meth = handler.findServiceEndpoint("echo")
self.assertEquals(self.service.echo, meth)
def test_invokeEndpoint(self):
handler = Handler(self.service)
meth = handler.findServiceEndpoint("echo")
rslt = handler.invokeServiceEndpoint(meth, ['spam'])
self.assertEquals(rslt, 'spam')
def test_translateResults(self):
handler=Handler(self.service)
data=handler.translateResult("foobar", None, "spam")
self.assertEquals(jsonrpc.loads(data), {"result":"foobar","id":"spam","error":None})
def test_translateError(self):
handler=Handler(self.service)
exc = Exception()
data=handler.translateResult(None, exc, "id")
self.assertEquals(jsonrpc.loads(data), {"result":None,"id":"id","error":{"name":"Exception", "message":""}})
def test_translateUnencodableResults(self):
handler=Handler(self.service)
data=handler.translateResult(self, None, "spam")
self.assertEquals(jsonrpc.loads(data), {"result":None,"id":"spam","error":{"name":"JSONEncodeException", "message":"Result Object Not Serializable"}})
def test_handleRequestEcho(self):
handler=Handler(self.service)
json=jsonrpc.dumps({"method":"echo", 'params':['foobar'], 'id':''})
result = handler.handleRequest(json)
self.assertEquals(jsonrpc.loads(result), jsonrpc.loads('{"result":"foobar", "error":null, "id":""}'))
def test_handleRequestMethodNotFound(self):
handler=Handler(self.service)
json=jsonrpc.dumps({"method":"not_found", 'params':['foobar'], 'id':''})
result = handler.handleRequest(json)
self.assertEquals(jsonrpc.loads(result), {"result":None, "error":{"name":"ServiceMethodNotFound", "message":""}, "id":""})
def test_handleRequestMethodNotAllowed(self):
handler=Handler(self.service)
json=jsonrpc.dumps({"method":"not_a_ServiceMethod", 'params':['foobar'], 'id':''})
result = handler.handleRequest(json)
self.assertEquals(jsonrpc.loads(result), {"result":None, "error":{"name":"ServiceMethodNotFound", "message":""}, "id":""})
def test_handleRequestMethodRaiseError(self):
handler=Handler(self.service)
json=jsonrpc.dumps({"method":"raiseError", 'params':[], 'id':''})
result = handler.handleRequest(json)
self.assertEquals(jsonrpc.loads(result), {"result":None, "error":{"name":"Exception", "message":"foobar"}, "id":""})
def test_handleBadRequestData(self):
handler=Handler(self.service)
json = "This is not a JSON-RPC request"
result = handler.handleRequest(json)
self.assertEquals(jsonrpc.loads(result), {"result":None, "error":{"name":"ServiceRequestNotTranslatable", "message":json}, "id":""})
def test_handleBadRequestObject(self):
handler=Handler(self.service)
json = "{}"
result = handler.handleRequest(json)
self.assertEquals(jsonrpc.loads(result), {"result":None, "error":{"name":"BadServiceRequest", "message":json}, "id":""})