| @@ 259-328 (lines=70) @@ | ||
| 256 | handle_ws_message.assert_called_once() |
|
| 257 | ||
| 258 | ||
| 259 | async def test_rpc_call_with_invalid_batch(aiohttp_client): |
|
| 260 | """ |
|
| 261 | --> [ |
|
| 262 | {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"}, |
|
| 263 | {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}, |
|
| 264 | {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"}, |
|
| 265 | {"foo": "boo"}, |
|
| 266 | {"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"}, |
|
| 267 | {"jsonrpc": "2.0", "method": "get_data", "id": "9"} |
|
| 268 | ] |
|
| 269 | <-- [ |
|
| 270 | {"jsonrpc": "2.0", "result": 7, "id": "1"}, |
|
| 271 | {"jsonrpc": "2.0", "result": 19, "id": "2"}, |
|
| 272 | {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}, |
|
| 273 | {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "5"}, |
|
| 274 | {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"} |
|
| 275 | ] |
|
| 276 | """ |
|
| 277 | ||
| 278 | def subtract(a, b): |
|
| 279 | return a - b |
|
| 280 | ||
| 281 | def notify_hello(a): |
|
| 282 | return a |
|
| 283 | ||
| 284 | def get_data(): |
|
| 285 | return ['hello', 5] |
|
| 286 | ||
| 287 | def my_sum(*args): |
|
| 288 | return sum(args) |
|
| 289 | ||
| 290 | rpc_server = aiohttp_rpc.WsJsonRpcServer() |
|
| 291 | rpc_server.add_method(subtract) |
|
| 292 | rpc_server.add_method((aiohttp_rpc.JsonRpcMethod(prefix='', func=my_sum, custom_name='sum'))) |
|
| 293 | rpc_server.add_method(notify_hello) |
|
| 294 | rpc_server.add_method(get_data) |
|
| 295 | ||
| 296 | client = await utils.make_ws_client(aiohttp_client, rpc_server) |
|
| 297 | ||
| 298 | called_methods = [ |
|
| 299 | aiohttp_rpc.JsonRpcRequest(id=1, method_name='sum', params=[1, 2, 4]), |
|
| 300 | aiohttp_rpc.JsonRpcRequest(method_name='notify_hello', params=[1, 2, 4]), |
|
| 301 | aiohttp_rpc.JsonRpcRequest(id=2, method_name='subtract', params=[42, 23]), |
|
| 302 | aiohttp_rpc.JsonRpcRequest(id=5, method_name='foo.get', params={'name': 'myself'}), |
|
| 303 | aiohttp_rpc.JsonRpcRequest(id=9, method_name='get_data'), |
|
| 304 | ] |
|
| 305 | ||
| 306 | async with aiohttp_rpc.WsJsonRpcClient('/rpc', session=client) as rpc: |
|
| 307 | assert await rpc.batch(called_methods) == [7, None, 19, errors.MethodNotFound(), ['hello', 5]] |
|
| 308 | assert await rpc.batch(called_methods, save_order=False) == [7, 19, errors.MethodNotFound(), ['hello', 5]] |
|
| 309 | ||
| 310 | result = await rpc.send_json([ |
|
| 311 | {'jsonrpc': '2.0', 'method': 'sum', 'params': [1, 2, 4], 'id': '1'}, |
|
| 312 | {'jsonrpc': '2.0', 'method': 'notify_hello', 'params': [7]}, |
|
| 313 | {'jsonrpc': '2.0', 'method': 'subtract', 'params': [42, 23], 'id': '2'}, |
|
| 314 | {'foo': 'boo'}, |
|
| 315 | {'jsonrpc': '2.0', 'method': 'foo.get', 'params': {'name': 'myself'}, 'id': '5'}, |
|
| 316 | {'jsonrpc': '2.0', 'method': 'get_data', 'id': '9'} |
|
| 317 | ]) |
|
| 318 | ||
| 319 | assert result[0] == [ |
|
| 320 | {'jsonrpc': '2.0', 'result': 7, 'id': '1'}, |
|
| 321 | {'jsonrpc': '2.0', 'result': 19, 'id': '2'}, |
|
| 322 | {'jsonrpc': '2.0', 'error': { |
|
| 323 | 'code': -32600, 'message': 'A request must contain "method" and "jsonrpc".' |
|
| 324 | }, 'id': None}, |
|
| 325 | {'jsonrpc': '2.0', 'error': { |
|
| 326 | 'code': -32601, 'message': 'The method does not exist / is not available.' |
|
| 327 | }, 'id': '5'}, |
|
| 328 | {'jsonrpc': '2.0', 'result': ['hello', 5], 'id': '9'}, |
|
| 329 | ] |
|
| 330 | ||
| @@ 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.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': '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 | ||