1
|
|
|
# https://www.jsonrpc.org/specification#examples |
2
|
|
|
|
3
|
|
|
import pytest |
4
|
|
|
|
5
|
|
|
import aiohttp_rpc |
6
|
|
|
from aiohttp_rpc import errors |
7
|
|
|
from tests import utils |
8
|
|
|
|
9
|
|
|
|
10
|
|
View Code Duplication |
async def test_rpc_call_with_positional_parameters(aiohttp_client): |
|
|
|
|
11
|
|
|
""" |
12
|
|
|
--> {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1} |
13
|
|
|
<-- {"jsonrpc": "2.0", "result": 19, "id": 1} |
14
|
|
|
|
15
|
|
|
--> {"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2} |
16
|
|
|
<-- {"jsonrpc": "2.0", "result": -19, "id": 2} |
17
|
|
|
""" |
18
|
|
|
|
19
|
|
|
def subtract(a, b): |
20
|
|
|
return a - b |
21
|
|
|
|
22
|
|
|
rpc_server = aiohttp_rpc.JsonRpcServer() |
23
|
|
|
rpc_server.add_method(subtract) |
24
|
|
|
|
25
|
|
|
client = await utils.make_client(aiohttp_client, rpc_server) |
26
|
|
|
|
27
|
|
|
async with aiohttp_rpc.JsonRpcClient('/rpc', session=client) as rpc: |
28
|
|
|
assert await rpc.subtract(42, 23) == 19 |
29
|
|
|
assert await rpc.subtract(23, 42) == -19 |
30
|
|
|
|
31
|
|
|
result = await rpc.send_json({'jsonrpc': '2.0', 'method': 'subtract', 'params': [42, 23], 'id': 1}) |
32
|
|
|
assert result[0] == {'jsonrpc': '2.0', 'result': 19, 'id': 1} |
33
|
|
|
|
34
|
|
|
result = await rpc.send_json({'jsonrpc': '2.0', 'method': 'subtract', 'params': [23, 42], 'id': 2}) |
35
|
|
|
assert result[0] == {'jsonrpc': '2.0', 'result': -19, 'id': 2} |
36
|
|
|
|
37
|
|
|
|
38
|
|
View Code Duplication |
async def test_rpc_call_with_named_parameters(aiohttp_client): |
|
|
|
|
39
|
|
|
""" |
40
|
|
|
--> {"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3} |
41
|
|
|
<-- {"jsonrpc": "2.0", "result": 19, "id": 3} |
42
|
|
|
|
43
|
|
|
--> {"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4} |
44
|
|
|
<-- {"jsonrpc": "2.0", "result": 19, "id": 4} |
45
|
|
|
""" |
46
|
|
|
|
47
|
|
|
def subtract(*, subtrahend, minuend): |
48
|
|
|
return minuend - subtrahend |
49
|
|
|
|
50
|
|
|
rpc_server = aiohttp_rpc.JsonRpcServer() |
51
|
|
|
rpc_server.add_method(subtract) |
52
|
|
|
|
53
|
|
|
client = await utils.make_client(aiohttp_client, rpc_server) |
54
|
|
|
|
55
|
|
|
async with aiohttp_rpc.JsonRpcClient('/rpc', session=client) as rpc: |
56
|
|
|
assert await rpc.subtract(subtrahend=23, minuend=42) == 19 |
57
|
|
|
assert await rpc.subtract(minuend=42, subtrahend=23) == 19 |
58
|
|
|
|
59
|
|
|
result = await rpc.send_json({ |
60
|
|
|
'jsonrpc': '2.0', 'method': 'subtract', 'params': {"subtrahend": 23, "minuend": 42}, 'id': 3, |
61
|
|
|
}) |
62
|
|
|
assert result[0] == {'jsonrpc': '2.0', 'result': 19, 'id': 3} |
63
|
|
|
|
64
|
|
|
result = await rpc.send_json({ |
65
|
|
|
'jsonrpc': '2.0', 'method': 'subtract', 'params': {"minuend": 42, "subtrahend": 23}, 'id': 4 |
66
|
|
|
}) |
67
|
|
|
assert result[0] == {'jsonrpc': '2.0', 'result': 19, 'id': 4} |
68
|
|
|
|
69
|
|
|
|
70
|
|
View Code Duplication |
async def test_notification(aiohttp_client): |
|
|
|
|
71
|
|
|
""" |
72
|
|
|
--> {"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]} |
73
|
|
|
--> {"jsonrpc": "2.0", "method": "foobar"} |
74
|
|
|
""" |
75
|
|
|
|
76
|
|
|
def update(*args): |
77
|
|
|
return args |
78
|
|
|
|
79
|
|
|
def foobar(*args): |
80
|
|
|
return 'ok' |
81
|
|
|
|
82
|
|
|
rpc_server = aiohttp_rpc.JsonRpcServer() |
83
|
|
|
rpc_server.add_method(update) |
84
|
|
|
rpc_server.add_method(foobar) |
85
|
|
|
|
86
|
|
|
client = await utils.make_client(aiohttp_client, rpc_server) |
87
|
|
|
|
88
|
|
|
async with aiohttp_rpc.JsonRpcClient('/rpc', session=client) as rpc: |
89
|
|
|
assert await rpc.notify('update', subtrahend=23, minuend=42) is None |
90
|
|
|
assert await rpc.notify('foobar', minuend=42, subtrahend=23) is None |
91
|
|
|
|
92
|
|
|
result = await rpc.send_json({'jsonrpc': '2.0', 'method': 'update', 'params': [1, 2, 3, 4, 5]}) |
93
|
|
|
assert result[0] is None |
94
|
|
|
|
95
|
|
|
result = await rpc.send_json({'jsonrpc': '2.0', 'method': 'foobar'}) |
96
|
|
|
assert result[0] is None |
97
|
|
|
|
98
|
|
|
|
99
|
|
View Code Duplication |
async def test_rpc_call_of_non_existent_method(aiohttp_client): |
|
|
|
|
100
|
|
|
""" |
101
|
|
|
--> {"jsonrpc": "2.0", "method": "foobar", "id": "1"} |
102
|
|
|
<-- {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "1"} |
103
|
|
|
""" |
104
|
|
|
|
105
|
|
|
rpc_server = aiohttp_rpc.JsonRpcServer() |
106
|
|
|
|
107
|
|
|
client = await utils.make_client(aiohttp_client, rpc_server) |
108
|
|
|
|
109
|
|
|
async with aiohttp_rpc.JsonRpcClient('/rpc', session=client) as rpc: |
110
|
|
|
with pytest.raises(errors.MethodNotFound): |
111
|
|
|
assert await rpc.call('foobar', subtrahend=23, minuend=42) |
112
|
|
|
|
113
|
|
|
result = await rpc.send_json({'jsonrpc': '2.0', 'method': 'foobar', 'id': '1'}) |
114
|
|
|
assert result[0] == { |
115
|
|
|
'jsonrpc': '2.0', 'error': {'code': -32601, 'message': errors.MethodNotFound.message}, 'id': '1', |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
async def test_rpc_call_with_invalid_json(aiohttp_client): |
120
|
|
|
""" |
121
|
|
|
--> {"jsonrpc": "2.0", "method": "foobar, "params": "bar", "baz] |
122
|
|
|
<-- {"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null} |
123
|
|
|
""" |
124
|
|
|
|
125
|
|
|
rpc_server = aiohttp_rpc.JsonRpcServer() |
126
|
|
|
|
127
|
|
|
client = await utils.make_client(aiohttp_client, rpc_server) |
128
|
|
|
|
129
|
|
|
async with aiohttp_rpc.JsonRpcClient('/rpc', session=client) as rpc: |
130
|
|
|
http_response = await rpc.session.post( |
131
|
|
|
rpc.url, |
132
|
|
|
data='{"jsonrpc": "2.0", "method": "foobar, "params": "bar", "baz]', |
133
|
|
|
) |
134
|
|
|
json_response = await http_response.json() |
135
|
|
|
del json_response['error']['message'] |
136
|
|
|
|
137
|
|
|
assert json_response == {'jsonrpc': '2.0', 'error': {'code': -32700}, 'id': None} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
async def test_rpc_call_with_an_empty_array(aiohttp_client): |
141
|
|
|
""" |
142
|
|
|
--> [] |
143
|
|
|
<-- {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null} |
144
|
|
|
""" |
145
|
|
|
|
146
|
|
|
rpc_server = aiohttp_rpc.JsonRpcServer() |
147
|
|
|
|
148
|
|
|
client = await utils.make_client(aiohttp_client, rpc_server) |
149
|
|
|
|
150
|
|
|
async with aiohttp_rpc.JsonRpcClient('/rpc', session=client) as rpc: |
151
|
|
|
with pytest.raises(errors.InvalidRequest): |
152
|
|
|
await rpc.batch([]) |
153
|
|
|
|
154
|
|
|
result = await rpc.send_json([]) |
155
|
|
|
assert result[0] == { |
156
|
|
|
'jsonrpc': '2.0', 'error': {'code': -32600, 'message': errors.InvalidRequest.message}, 'id': None, |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
async def test_rpc_call_with_an_invalid_batch(aiohttp_client): |
161
|
|
|
""" |
162
|
|
|
--> [1] |
163
|
|
|
<-- {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null} |
164
|
|
|
""" |
165
|
|
|
|
166
|
|
|
rpc_server = aiohttp_rpc.JsonRpcServer() |
167
|
|
|
|
168
|
|
|
client = await utils.make_client(aiohttp_client, rpc_server) |
169
|
|
|
|
170
|
|
|
async with aiohttp_rpc.JsonRpcClient('/rpc', session=client) as rpc: |
171
|
|
|
result = await rpc.send_json([1]) |
172
|
|
|
assert result[0] == [{ |
173
|
|
|
'jsonrpc': '2.0', 'error': {'code': -32600, 'message': 'Data must be a dict.'}, 'id': None, |
174
|
|
|
}] |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
async def test_rpc_call_with_invalid_batch(aiohttp_client): |
178
|
|
|
""" |
179
|
|
|
--> [1,2,3] |
180
|
|
|
<-- [ |
181
|
|
|
{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}, |
182
|
|
|
{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}, |
183
|
|
|
{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null} |
184
|
|
|
] |
185
|
|
|
""" |
186
|
|
|
|
187
|
|
|
rpc_server = aiohttp_rpc.JsonRpcServer() |
188
|
|
|
|
189
|
|
|
client = await utils.make_client(aiohttp_client, rpc_server) |
190
|
|
|
|
191
|
|
|
json_with_error = { |
192
|
|
|
'jsonrpc': '2.0', 'error': {'code': -32600, 'message': 'Data must be a dict.'}, 'id': None, |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
async with aiohttp_rpc.JsonRpcClient('/rpc', session=client) as rpc: |
196
|
|
|
result = await rpc.send_json([1, 2, 3]) |
197
|
|
|
assert result[0] == [json_with_error, json_with_error, json_with_error] |
198
|
|
|
|
199
|
|
|
|
200
|
|
View Code Duplication |
async def test_rpc_call_with_different_invalid_batch(aiohttp_client): |
|
|
|
|
201
|
|
|
""" |
202
|
|
|
--> [ |
203
|
|
|
{"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"}, |
204
|
|
|
{"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}, |
205
|
|
|
{"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"}, |
206
|
|
|
{"foo": "boo"}, |
207
|
|
|
{"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"}, |
208
|
|
|
{"jsonrpc": "2.0", "method": "get_data", "id": "9"} |
209
|
|
|
] |
210
|
|
|
<-- [ |
211
|
|
|
{"jsonrpc": "2.0", "result": 7, "id": "1"}, |
212
|
|
|
{"jsonrpc": "2.0", "result": 19, "id": "2"}, |
213
|
|
|
{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}, |
214
|
|
|
{"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "5"}, |
215
|
|
|
{"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"} |
216
|
|
|
] |
217
|
|
|
""" |
218
|
|
|
|
219
|
|
|
def subtract(a, b): |
220
|
|
|
return a - b |
221
|
|
|
|
222
|
|
|
def notify_hello(a): |
223
|
|
|
return a |
224
|
|
|
|
225
|
|
|
def get_data(): |
226
|
|
|
return ['hello', 5] |
227
|
|
|
|
228
|
|
|
def my_sum(*args): |
229
|
|
|
return sum(args) |
230
|
|
|
|
231
|
|
|
rpc_server = aiohttp_rpc.JsonRpcServer() |
232
|
|
|
rpc_server.add_method(subtract) |
233
|
|
|
rpc_server.add_method((aiohttp_rpc.JsonRpcMethod(func=my_sum, name='sum'))) |
234
|
|
|
rpc_server.add_method(notify_hello) |
235
|
|
|
rpc_server.add_method(get_data) |
236
|
|
|
|
237
|
|
|
client = await utils.make_client(aiohttp_client, rpc_server) |
238
|
|
|
|
239
|
|
|
called_methods = [ |
240
|
|
|
aiohttp_rpc.JsonRpcRequest(id=1, method_name='sum', params=[1, 2, 4]), |
241
|
|
|
aiohttp_rpc.JsonRpcRequest(method_name='notify_hello', params=[1, 2, 4]), |
242
|
|
|
aiohttp_rpc.JsonRpcRequest(id=2, method_name='subtract', params=[42, 23]), |
243
|
|
|
aiohttp_rpc.JsonRpcRequest(id=5, method_name='foo.get', params={'name': 'myself'}), |
244
|
|
|
aiohttp_rpc.JsonRpcRequest(id=9, method_name='get_data'), |
245
|
|
|
] |
246
|
|
|
|
247
|
|
|
async with aiohttp_rpc.JsonRpcClient('/rpc', session=client) as rpc: |
248
|
|
|
assert await rpc.batch(called_methods) == (7, None, 19, errors.MethodNotFound(), ['hello', 5],) |
249
|
|
|
assert await rpc.batch(called_methods, save_order=False) == (7, 19, errors.MethodNotFound(), ['hello', 5],) |
250
|
|
|
|
251
|
|
|
result = await rpc.send_json([ |
252
|
|
|
{'jsonrpc': '2.0', 'method': 'sum', 'params': [1, 2, 4], 'id': '1'}, |
253
|
|
|
{'jsonrpc': '2.0', 'method': 'notify_hello', 'params': [7]}, |
254
|
|
|
{'jsonrpc': '2.0', 'method': 'subtract', 'params': [42, 23], 'id': '2'}, |
255
|
|
|
{'foo': 'boo'}, |
256
|
|
|
{'jsonrpc': '2.0', 'method': 'foo.get', 'params': {'name': 'myself'}, 'id': '5'}, |
257
|
|
|
{'jsonrpc': '2.0', 'method': 'get_data', 'id': '9'} |
258
|
|
|
]) |
259
|
|
|
|
260
|
|
|
assert result[0] == [ |
261
|
|
|
{'jsonrpc': '2.0', 'result': 7, 'id': '1'}, |
262
|
|
|
{'jsonrpc': '2.0', 'result': 19, 'id': '2'}, |
263
|
|
|
{'jsonrpc': '2.0', 'error': { |
264
|
|
|
'code': -32600, 'message': 'The request must contain "method" and "jsonrpc".' |
265
|
|
|
}, 'id': None}, |
266
|
|
|
{'jsonrpc': '2.0', 'error': { |
267
|
|
|
'code': -32601, 'message': 'The method does not exist / is not available.' |
268
|
|
|
}, 'id': '5'}, |
269
|
|
|
{'jsonrpc': '2.0', 'result': ['hello', 5], 'id': '9'}, |
270
|
|
|
] |
271
|
|
|
|