extern crate serde_json; extern crate susydev_jsonrpc_core; extern crate susydev_jsonrpc_pubsub; #[macro_use] extern crate susydev_jsonrpc_macros; use std::sync::Arc; use susydev_jsonrpc_core::futures::sync::mpsc; use susydev_jsonrpc_pubsub::{PubSubHandler, SubscriptionId, Session, PubSubMetadata}; use susydev_jsonrpc_macros::{pubsub, Trailing}; 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 { type Metadata; #[pubsub(name = "hello")] { /// Hello subscription #[rpc(name = "hello_subscribe")] fn subscribe(&self, Self::Metadata, pubsub::Subscriber, u32, Trailing); /// Unsubscribe from hello subscription. #[rpc(name = "hello_unsubscribe")] fn unsubscribe(&self, Option, SubscriptionId) -> Result; } } } #[derive(Default)] struct RpcImpl; impl Rpc for RpcImpl { type Metadata = Metadata; fn subscribe(&self, _meta: Self::Metadata, subscriber: pubsub::Subscriber, _pre: u32, _trailing: Trailing) { let _sink = subscriber.assign_id(SubscriptionId::Number(5)); } fn unsubscribe(&self, _meta: Option, _id: SubscriptionId) -> Result { Ok(true) } } #[derive(Clone, Default)] struct Metadata; impl susydev_jsonrpc_core::Metadata for Metadata {} impl PubSubMetadata for Metadata { fn session(&self) -> Option> { let (tx, _rx) = mpsc::channel(1); Some(Arc::new(Session::new(tx))) } } #[test] fn test_invalid_trailing_pubsub_params() { let mut io = PubSubHandler::default(); let rpc = RpcImpl::default(); io.extend_with(rpc.to_delegate()); // when let meta = Metadata; let req = r#"{"jsonrpc":"2.0","id":1,"method":"hello_subscribe","params":[]}"#; let res = io.handle_request_sync(req, meta); let expected = r#"{ "jsonrpc": "2.0", "error": { "code": -32602, "message": "Couldn't parse parameters: `params` should have at least 1 argument(s)", "data": "\"\"" }, "id": 1 }"#; let expected: susydev_jsonrpc_core::Response = serde_json::from_str(expected).unwrap(); let result: susydev_jsonrpc_core::Response = serde_json::from_str(&res.unwrap()).unwrap(); assert_eq!(expected, result); }