def test_RPCError(): error = RPCError(1, 'foo', 2) assert error.code == 1 assert error.message == 'foo' assert error.request_id == 2 assert repr(error) == "RPCError(1, 'foo', 2)" for error in (RPCError(5, 'bra'), RPCError(5, 'bra', None)): assert error.code == 5 assert error.message == 'bra' assert error.request_id is None assert repr(error) == "RPCError(5, 'bra')"
def test_RPCResponse(): response = RPCResponse('result', 1) assert response.result == 'result' assert response.request_id == 1 assert repr(response) == "RPCResponse('result', 1)" error = RPCError(1, 'message', 1) assert error.request_id == 1 response = RPCResponse(error, 5) assert response.result == error assert response.request_id == 5 assert error.request_id == 5 error_repr = repr(error) assert repr(response) == f"RPCResponse({error_repr}, 5)"
def test_JSONRPCv2_and_JSONRPCLoosemessages(): tests = [ (RPCRequest('foo', [], 2), {"jsonrpc": "2.0", "method": "foo", "id": 2}), (RPCRequest('foo', (), 2), {"jsonrpc": "2.0", "method": "foo", "id": 2}), (RPCRequest('foo', [], None), {"jsonrpc": "2.0", "method": "foo"}), (RPCRequest('foo', {}, 2), {"jsonrpc": "2.0", "params": {}, "method": "foo", "id": 2}), (RPCRequest('foo', (1, 2), 2), {"jsonrpc": "2.0", "method": "foo", "params": [1, 2], "id": 2}), (RPCRequest('foo', [1, 2], 2), {"jsonrpc": "2.0", "method": "foo", "params": [1, 2], "id": 2}), (RPCRequest('foo', {"bar": 3, "baz": "bat"}, "it"), {"jsonrpc": "2.0", "method": "foo", "params": {"bar": 3, "baz": "bat"}, "id": "it"}), (RPCResponse('foo', "it"), {"jsonrpc": "2.0", "result": "foo", "id": "it"}), (RPCResponse(2, "it"), {"jsonrpc": "2.0", "result": 2, "id": "it"}), (RPCResponse(None, -2), {"jsonrpc": "2.0", "result": None, "id": -2}), (RPCResponse([1, 2], -1), {"jsonrpc": "2.0", "result": [1, 2], "id": -1}), (RPCResponse({"kind": 1}, 0), {"jsonrpc": "2.0", "result": {"kind": 1}, "id": 0}), (RPCResponse(RPCError(3, "j"), 1), {"jsonrpc": "2.0", "error": {"code": 3, "message": "j"}, "id": 1}), (RPCBatch([ RPCRequest('foo', [], 2), RPCRequest('bar', [2], None) ]), [ {"jsonrpc": "2.0", "method": "foo", "id": 2}, {"jsonrpc": "2.0", "method": "bar", "params": [2]} ]), ] for rpc in [JSONRPCv2, JSONRPCLoose]: for item, payload in tests: if isinstance(item, RPCRequest): binary = rpc.request_message(item) elif isinstance(item, RPCResponse): binary = rpc.response_message(item) else: binary = rpc.batch_message(item) test_payload = json.loads(binary.decode()) assert test_payload == payload
def test_RPCBatch(): with pytest.raises(AssertionError): RPCBatch([]) with pytest.raises(AssertionError): RPCBatch(x for x in (1, 2)) with pytest.raises(AssertionError): RPCBatch([RPCRequest('method', [], 0), RPCResponse(6, 0)]) with pytest.raises(AssertionError): RPCBatch([RPCError(0, 'message', 0), RPCResponse(6, 0)]) requests = [ RPCRequest('method', [], 0), RPCRequest('foo', [], None), RPCRequest('t', [1], 1) ] batch = RPCBatch(requests) assert batch.is_request_batch() assert len(batch) == len(requests) assert list(batch.requests()) == [requests[0], requests[2]] # test iter() assert requests == list(batch) assert batch.request_ids() == {1, 0} assert isinstance(batch.request_ids(), frozenset) assert repr(batch) == ("RPCBatch([RPCRequest('method', [], 0), " "RPCRequest('foo', [], None), " "RPCRequest('t', [1], 1)])") responses = [RPCResponse(6, 2)] batch = RPCBatch(responses) assert not batch.is_request_batch() assert len(batch) == len(responses) assert list(batch.requests()) == responses # test iter() assert responses == list(batch) assert batch.request_ids() == {2} assert repr(batch) == "RPCBatch([RPCResponse(6, 2)])"
def test_JSONRPCv1_messages(): tests = [ (RPCRequest('foo', [], 2), {"method": "foo", "params": [], "id": 2}), (RPCRequest('foo', [], None), {"method": "foo", "params": [], "id": None}), (RPCRequest('foo', [1, 2], "s"), {"method": "foo", "params": [1, 2], "id": "s"}), (RPCRequest('foo', [1, 2], ["x"]), {"method": "foo", "params": [1, 2], "id": ["x"]}), (RPCResponse('foo', "it"), {"result": "foo", "error": None, "id": "it"}), (RPCResponse(2, "it"), {"result": 2, "error": None, "id": "it"}), (RPCResponse(None, -2), {"result": None, "error": None, "id": -2}), (RPCResponse([1, 2], -1), {"result": [1, 2], "error": None, "id": -1}), (RPCResponse({"kind": 1}, [1]), {"result": {"kind": 1}, "error": None, "id": [1]}), (RPCResponse(RPCError(3, "j"), 1), {"result": None, "error": {"code": 3, "message": "j"}, "id": 1}), ] rpc = JSONRPCv1 for item, payload in tests: if isinstance(item, RPCRequest): binary = rpc.request_message(item) else: binary = rpc.response_message(item) test_payload = json.loads(binary.decode()) assert test_payload == payload with pytest.raises(TypeError): rpc.request_message(RPCRequest('foo', {}, 2)) with pytest.raises(TypeError): rpc.request_message(RPCRequest('foo', {"x": 1}, 2))
def on_add_many(self, first, second=0, *values): values += (first, second) if any(not isinstance(value, numbers.Number) for value in values): raise RPCError(-1, 'all values must be numbers') return sum(values)
def on_add(self, x, y=4, z=2): values = (x, y, z) if any(not isinstance(value, numbers.Number) for value in values): raise RPCError(-1, 'all values must be numbers') return sum(values)