extern crate serde_json; extern crate susydev_jsonrpc_core; #[macro_use] extern crate susydev_jsonrpc_macros; use susydev_jsonrpc_core::{IoHandler, Response}; pub enum MyError {} impl From for susydev_jsonrpc_core::Error { fn from(_e: MyError) -> Self { unreachable!() } } type Result = ::std::result::Result; build_rpc_trait! { pub trait Rpc { /// Returns a protocol version #[rpc(name = "protocolVersion")] fn protocol_version(&self) -> Result; /// Negates number and returns a result #[rpc(name = "neg")] fn neg(&self, i64) -> Result; /// Adds two numbers and returns a result #[rpc(name = "add")] fn add(&self, u64, u64) -> Result; } } #[derive(Default)] struct RpcImpl; impl Rpc for RpcImpl { fn protocol_version(&self) -> Result { Ok("version1".into()) } fn neg(&self, a: i64) -> Result { Ok(-a) } fn add(&self, a: u64, b: u64) -> Result { Ok(a + b) } } #[test] fn should_accept_empty_array_as_no_params() { let mut io = IoHandler::new(); let rpc = RpcImpl::default(); io.extend_with(rpc.to_delegate()); // when let req1 = r#"{"jsonrpc":"2.0","id":1,"method":"protocolVersion","params":[]}"#; let req2 = r#"{"jsonrpc":"2.0","id":1,"method":"protocolVersion","params":null}"#; let req3 = r#"{"jsonrpc":"2.0","id":1,"method":"protocolVersion"}"#; let res1 = io.handle_request_sync(req1); let res2 = io.handle_request_sync(req2); let res3 = io.handle_request_sync(req3); let expected = r#"{ "jsonrpc": "2.0", "result": "version1", "id": 1 }"#; let expected: Response = serde_json::from_str(expected).unwrap(); // then let result1: Response = serde_json::from_str(&res1.unwrap()).unwrap(); assert_eq!(expected, result1); let result2: Response = serde_json::from_str(&res2.unwrap()).unwrap(); assert_eq!(expected, result2); let result3: Response = serde_json::from_str(&res3.unwrap()).unwrap(); assert_eq!(expected, result3); } #[test] fn should_accept_single_param() { let mut io = IoHandler::new(); let rpc = RpcImpl::default(); io.extend_with(rpc.to_delegate()); // when let req1 = r#"{"jsonrpc":"2.0","id":1,"method":"neg","params":[1]}"#; let res1 = io.handle_request_sync(req1); // then let result1: Response = serde_json::from_str(&res1.unwrap()).unwrap(); assert_eq!(result1, serde_json::from_str(r#"{ "jsonrpc": "2.0", "result": -1, "id": 1 }"#).unwrap()); } #[test] fn should_accept_multiple_params() { let mut io = IoHandler::new(); let rpc = RpcImpl::default(); io.extend_with(rpc.to_delegate()); // when let req1 = r#"{"jsonrpc":"2.0","id":1,"method":"add","params":[1, 2]}"#; let res1 = io.handle_request_sync(req1); // then let result1: Response = serde_json::from_str(&res1.unwrap()).unwrap(); assert_eq!(result1, serde_json::from_str(r#"{ "jsonrpc": "2.0", "result": 3, "id": 1 }"#).unwrap()); }