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