| @@ 243-312 (lines=70) @@ | ||
| 240 | handle_ws_message.assert_called_once() |
|
| 241 | ||
| 242 | ||
| 243 | async def test_rpc_call_with_invalid_batch(aiohttp_client): |
|
| 244 | """ |
|
| 245 | --> [ |
|
| 246 | {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"}, |
|
| 247 | {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}, |
|
| 248 | {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"}, |
|
| 249 | {"foo": "boo"}, |
|
| 250 | {"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"}, |
|
| 251 | {"jsonrpc": "2.0", "method": "get_data", "id": "9"} |
|
| 252 | ] |
|
| 253 | <-- [ |
|
| 254 | {"jsonrpc": "2.0", "result": 7, "id": "1"}, |
|
| 255 | {"jsonrpc": "2.0", "result": 19, "id": "2"}, |
|
| 256 | {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}, |
|
| 257 | {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "5"}, |
|
| 258 | {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"} |
|
| 259 | ] |
|
| 260 | """ |
|
| 261 | ||
| 262 | def subtract(a, b): |
|
| 263 | return a - b |
|
| 264 | ||
| 265 | def notify_hello(a): |
|
| 266 | return a |
|
| 267 | ||
| 268 | def get_data(): |
|
| 269 | return ['hello', 5] |
|
| 270 | ||
| 271 | def my_sum(*args): |
|
| 272 | return sum(args) |
|
| 273 | ||
| 274 | rpc_server = aiohttp_rpc.WsJsonRpcServer() |
|
| 275 | rpc_server.add_method(subtract) |
|
| 276 | rpc_server.add_method((aiohttp_rpc.JsonRpcMethod(prefix='', func=my_sum, custom_name='sum'))) |
|
| 277 | rpc_server.add_method(notify_hello) |
|
| 278 | rpc_server.add_method(get_data) |
|
| 279 | ||
| 280 | client = await utils.make_ws_client(aiohttp_client, rpc_server) |
|
| 281 | ||
| 282 | called_methods = [ |
|
| 283 | aiohttp_rpc.CalledJsonRpcMethod(msg_id=1, name='sum', params=[1, 2, 4]), |
|
| 284 | aiohttp_rpc.CalledJsonRpcMethod(name='notify_hello', params=[1, 2, 4], is_notification=True), |
|
| 285 | aiohttp_rpc.CalledJsonRpcMethod(msg_id=2, name='subtract', params=[42, 23]), |
|
| 286 | aiohttp_rpc.CalledJsonRpcMethod(msg_id=5, name='foo.get', params={'name': 'myself'}), |
|
| 287 | aiohttp_rpc.CalledJsonRpcMethod(msg_id=9, name='get_data'), |
|
| 288 | ] |
|
| 289 | ||
| 290 | async with aiohttp_rpc.WsJsonRpcClient('/rpc', session=client) as rpc: |
|
| 291 | assert await rpc.batch(called_methods) == [7, None, 19, errors.MethodNotFound(), ['hello', 5]] |
|
| 292 | assert await rpc.batch(called_methods, save_order=False) == [7, 19, errors.MethodNotFound(), ['hello', 5]] |
|
| 293 | ||
| 294 | result = await rpc.send_json([ |
|
| 295 | {'jsonrpc': '2.0', 'method': 'sum', 'params': [1, 2, 4], 'id': '1'}, |
|
| 296 | {'jsonrpc': '2.0', 'method': 'notify_hello', 'params': [7]}, |
|
| 297 | {'jsonrpc': '2.0', 'method': 'subtract', 'params': [42, 23], 'id': '2'}, |
|
| 298 | {'foo': 'boo'}, |
|
| 299 | {'jsonrpc': '2.0', 'method': 'foo.get', 'params': {'name': 'myself'}, 'id': '5'}, |
|
| 300 | {'jsonrpc': '2.0', 'method': 'get_data', 'id': '9'} |
|
| 301 | ]) |
|
| 302 | ||
| 303 | assert result[0] == [ |
|
| 304 | {'jsonrpc': '2.0', 'result': 7, 'id': '1'}, |
|
| 305 | {'jsonrpc': '2.0', 'result': 19, 'id': '2'}, |
|
| 306 | {'jsonrpc': '2.0', 'error': { |
|
| 307 | 'code': -32600, 'message': 'A request must contain "method" and "jsonrpc".' |
|
| 308 | }, 'id': None}, |
|
| 309 | {'jsonrpc': '2.0', 'error': { |
|
| 310 | 'code': -32601, 'message': 'The method does not exist / is not available.' |
|
| 311 | }, 'id': '5'}, |
|
| 312 | {'jsonrpc': '2.0', 'result': ['hello', 5], 'id': '9'}, |
|
| 313 | ] |
|
| 314 | ||
| @@ 200-269 (lines=70) @@ | ||
| 197 | assert result[0] == [json_with_error, json_with_error, json_with_error] |
|
| 198 | ||
| 199 | ||
| 200 | async def test_rpc_call_with_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(prefix='', func=my_sum, custom_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.CalledJsonRpcMethod(msg_id=1, name='sum', params=[1, 2, 4]), |
|
| 241 | aiohttp_rpc.CalledJsonRpcMethod(name='notify_hello', params=[1, 2, 4], is_notification=True), |
|
| 242 | aiohttp_rpc.CalledJsonRpcMethod(msg_id=2, name='subtract', params=[42, 23]), |
|
| 243 | aiohttp_rpc.CalledJsonRpcMethod(msg_id=5, name='foo.get', params={'name': 'myself'}), |
|
| 244 | aiohttp_rpc.CalledJsonRpcMethod(msg_id=9, 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': 'A 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 | ||