Skip to content

Commit

Permalink
Fix rpc alias(es) key constant (#375)
Browse files Browse the repository at this point in the history
* Fix rpc alias(es) key constant

* Bump derive patch
  • Loading branch information
ascjones authored and tomusdrw committed Feb 5, 2019
1 parent 5e82ae0 commit 5234288
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
2 changes: 1 addition & 1 deletion derive/Cargo.toml
Expand Up @@ -7,7 +7,7 @@ homepage = "https://github.com/paritytech/jsonrpc"
license = "MIT"
name = "jsonrpc-derive"
repository = "https://github.com/paritytech/jsonrpc"
version = "10.0.1"
version = "10.0.2"

[lib]
proc-macro = true
Expand Down
2 changes: 1 addition & 1 deletion derive/src/rpc_attr.rs
Expand Up @@ -21,7 +21,7 @@ pub enum PubSubMethodKind {
const RPC_ATTR_NAME: &'static str = "rpc";
const RPC_NAME_KEY: &'static str = "name";
const SUBSCRIPTION_NAME_KEY: &'static str = "subscription";
const ALIASES_KEY: &'static str = "aliases";
const ALIASES_KEY: &'static str = "alias";
const PUB_SUB_ATTR_NAME: &'static str = "pubsub";
const METADATA_META_WORD: &'static str = "meta";
const SUBSCRIBE_META_WORD: &'static str = "subscribe";
Expand Down
32 changes: 31 additions & 1 deletion derive/tests/macros.rs
Expand Up @@ -22,7 +22,7 @@ pub trait Rpc {
fn neg(&self, _: i64) -> Result<i64>;

/// Adds two numbers and returns a result
#[rpc(name = "add")]
#[rpc(name = "add", alias("add_alias1", "add_alias2"))]
fn add(&self, _: u64, _: u64) -> Result<u64>;
}

Expand Down Expand Up @@ -114,3 +114,33 @@ fn should_accept_multiple_params() {
"id": 1
}"#).unwrap());
}

#[test]
fn should_use_method_name_aliases() {
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_alias1","params":[1, 2]}"#;
let req2 = r#"{"jsonrpc":"2.0","id":1,"method":"add_alias2","params":[1, 2]}"#;

let res1 = io.handle_request_sync(req1);
let res2 = io.handle_request_sync(req2);

// 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());

let result2: Response = serde_json::from_str(&res2.unwrap()).unwrap();
assert_eq!(result2, serde_json::from_str(r#"{
"jsonrpc": "2.0",
"result": 3,
"id": 1
}"#).unwrap());
}

24 changes: 23 additions & 1 deletion derive/tests/pubsub-macros.rs
Expand Up @@ -23,7 +23,7 @@ pub trait Rpc {
type Metadata;

/// Hello subscription
#[pubsub(subscription = "hello", subscribe, name = "hello_subscribe", alias("hello_sub"))]
#[pubsub(subscription = "hello", subscribe, name = "hello_subscribe", alias("hello_alias"))]
fn subscribe(&self, _: Self::Metadata, _: Subscriber<String>, _: u32, _: Option<u64>);

/// Unsubscribe from hello subscription.
Expand Down Expand Up @@ -87,3 +87,25 @@ fn test_invalid_trailing_pubsub_params() {
let result: jsonrpc_core::Response = serde_json::from_str(&res.unwrap()).unwrap();
assert_eq!(expected, result);
}

#[test]
fn test_subscribe_with_alias() {
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_alias","params":[1]}"#;
let res = io.handle_request_sync(req, meta);
let expected = r#"{
"jsonrpc": "2.0",
"result": 5,
"id": 1
}"#;

let expected: jsonrpc_core::Response = serde_json::from_str(expected).unwrap();
let result: jsonrpc_core::Response = serde_json::from_str(&res.unwrap()).unwrap();
assert_eq!(expected, result);
}

0 comments on commit 5234288

Please sign in to comment.