def test_jsonrpc_spec_v2_example11(prot): requests = prot.parse_request("""[ {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"}, {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}, {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"}, {"foo": "boo"}, {"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"}, {"jsonrpc": "2.0", "method": "get_data", "id": "9"} ]""") assert isinstance(requests[3], JSONRPCInvalidRequestError) responses = requests.create_batch_response() responses.append(requests[0].respond(7)) responses.append(requests[2].respond(19)) responses.append(requests[3].error_respond()) responses.append(requests[4].error_respond(MethodNotFoundError('foo.get'))) responses.append(requests[5].respond(['hello', 5])) assert _json_equal( """[ {"jsonrpc": "2.0", "result": 7, "id": "1"}, {"jsonrpc": "2.0", "result": 19, "id": "2"}, {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}, {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "5"}, {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"} ]""", responses.serialize())
def test_jsonrpc_spec_v2_example4(prot): request = prot.create_request("foobar") request.unique_id = 1 assert request.serialize() == b"\x94\x00\x01\xa6foobar\x90" response = request.error_respond(MethodNotFoundError("foobar")) assert _msgpack_equal( b"\x94\x01\x01\x92\xd1\x80\xa7\xb0Method not found\xc0", response.serialize() )
def test_jsonrpc_spec_v2_example4(prot): request = prot.create_request('foobar') request.unique_id = str(1) assert _json_equal("""{"jsonrpc": "2.0", "method": "foobar", "id": "1"}""", request.serialize()) response = request.error_respond(MethodNotFoundError('foobar')) assert _json_equal( """{"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "1"}""", response.serialize())
def test_jsonrpc_spec_v2_example11(prot): # Since MSGPACK does not support batched request, we test the requests # one by one requests = [] for data in [ b"\x94\x00\x01\xa3sum\x93\x01\x02\x04", # [0, 1, "sum", [1,2,4]] b"\x93\x02\xacnotify_hello\x91\x07", # [2, "notify_hello", [7] b"\x94\x00\x02\xa8subtract\x92*\x17", # [0, 2, "subtract", [42,23]] b"\x92\xa3foo\xa3boo", # ["foo", "boo"] b"\x94\x00\x05\xa7foo.get\x92\xa4name\xa6myself", # [0, 5, "foo.get", ["name", "myself"]] b"\x94\x00\t\xa8get_data\x90", # [0, 9, "get_data", []] ]: try: requests.append(prot.parse_request(data)) except Exception as ex: requests.append(ex) assert isinstance(requests[3], MSGPACKRPCInvalidRequestError) responses = [] responses.append(requests[0].respond(7)) responses.append(requests[1].error_respond(MethodNotFoundError("notify_hello"))) responses.append(requests[2].respond(19)) responses.append(requests[3].error_respond()) responses.append(requests[4].error_respond(MethodNotFoundError("foo.get"))) responses.append(requests[5].respond(["hello", 5])) responses = [ response.serialize() if response else response for response in responses ] assert responses[0] == b"\x94\x01\x01\xc0\x07" assert responses[1] is None assert responses[2] == b"\x94\x01\x02\xc0\x13" assert responses[3] == b"\x94\x01\xc0\x92\xd1\x80\xa8\xafInvalid request\xc0" assert responses[4] == b"\x94\x01\x05\x92\xd1\x80\xa7\xb0Method not found\xc0" assert responses[5] == b"\x94\x01\t\xc0\x92\xa5hello\x05"
def _dispatch(self, request): try: try: method = self.get_method(request.method) except KeyError as e: logger.error("RPC method not found: {}".format(request.method)) return request.error_respond(MethodNotFoundError(e)) # we found the method try: result = method(*request.args, **request.kwargs) except Exception as e: # an error occured within the method, return it logger.error("RPC method {} failed:\n{}".format( request.method, utils.indent(traceback.format_exc(), 2))) return request.error_respond(e) # respond with result return request.respond(result) except Exception as e: logger.error("RPC method {} failed unexpectedly:\n{}".format( request.method, utils.indent(traceback.format_exc(), 2))) # unexpected error, do not let client know what happened return request.error_respond(ServerError())