|
| 1 | +//! An utility package to test jsonrpc-core based projects. |
| 2 | +//! |
| 3 | +//! ``` |
| 4 | +//! #[macro_use] |
| 5 | +//! extern crate jsonrpc_macros; |
| 6 | +//! |
| 7 | +//! extern crate jsonrpc_core as core; |
| 8 | +//! extern crate jsonrpc_test as test; |
| 9 | +//! |
| 10 | +//! use core::Result; |
| 11 | +//! |
| 12 | +//! build_rpc_trait! { |
| 13 | +//! pub trait Test { |
| 14 | +//! #[rpc(name = "rpc_some_method")] |
| 15 | +//! fn some_method(&self, u64) -> Result<u64>; |
| 16 | +//! } |
| 17 | +//! } |
| 18 | +//! |
| 19 | +//! struct Dummy; |
| 20 | +//! impl Test for Dummy { |
| 21 | +//! fn some_method(&self, x: u64) -> Result<u64> { |
| 22 | +//! Ok(x * 2) |
| 23 | +//! } |
| 24 | +//! } |
| 25 | +//! |
| 26 | +//! fn main() { |
| 27 | +//! // Initialize new instance of test environment |
| 28 | +//! let rpc = test::Rpc::new(Dummy.to_delegate()); |
| 29 | +//! |
| 30 | +//! // make a request and verify the response as a pretty-printed string |
| 31 | +//! assert_eq!(rpc.request("rpc_some_method", &[5]), r#"10"#); |
| 32 | +//! |
| 33 | +//! // You can also test RPC created without macros: |
| 34 | +//! let rpc = { |
| 35 | +//! let mut io = core::IoHandler::new(); |
| 36 | +//! io.add_method("rpc_test_method", |_| { |
| 37 | +//! Err(core::Error::internal_error()) |
| 38 | +//! }); |
| 39 | +//! test::Rpc::from(io) |
| 40 | +//! }; |
| 41 | +//! |
| 42 | +//! assert_eq!(rpc.request("rpc_test_method", &()), r#"{ |
| 43 | +//! "code": -32603, |
| 44 | +//! "message": "Internal error" |
| 45 | +//! }"#); |
| 46 | +//! } |
| 47 | +//! ``` |
| 48 | +
|
| 49 | +#[warn(missing_docs)] |
| 50 | + |
| 51 | +extern crate jsonrpc_core as rpc; |
| 52 | +extern crate serde; |
| 53 | +extern crate serde_json; |
| 54 | + |
| 55 | +use std::collections::HashMap; |
| 56 | + |
| 57 | +/// Test RPC options. |
| 58 | +#[derive(Default, Debug)] |
| 59 | +pub struct Options { |
| 60 | + /// Disable printing requests and responses. |
| 61 | + pub no_print: bool, |
| 62 | +} |
| 63 | + |
| 64 | +#[derive(Default, Debug)] |
| 65 | +/// RPC instance. |
| 66 | +pub struct Rpc { |
| 67 | + /// Underlying `IoHandler`. |
| 68 | + pub io: rpc::IoHandler, |
| 69 | + /// Options |
| 70 | + pub options: Options, |
| 71 | +} |
| 72 | + |
| 73 | +impl From<rpc::IoHandler> for Rpc { |
| 74 | + fn from(io: rpc::IoHandler) -> Self { |
| 75 | + Rpc { io, ..Default::default() } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl Rpc { |
| 80 | + /// Create a new RPC instance from a single delegate. |
| 81 | + pub fn new<D>(delegate: D) -> Self where |
| 82 | + D: Into<HashMap<String, rpc::RemoteProcedure<()>>>, |
| 83 | + { |
| 84 | + let mut io = rpc::IoHandler::new(); |
| 85 | + io.extend_with(delegate); |
| 86 | + io.into() |
| 87 | + } |
| 88 | + |
| 89 | + /// Perform a single, synchronous method call. |
| 90 | + pub fn request<T>(&self, method: &str, params: &T) -> String where |
| 91 | + T: serde::Serialize, |
| 92 | + { |
| 93 | + use self::rpc::types::response; |
| 94 | + |
| 95 | + let request = format!( |
| 96 | + "{{ \"jsonrpc\":\"2.0\", \"id\": 1, \"method\": \"{}\", \"params\": {} }}", |
| 97 | + method, |
| 98 | + serde_json::to_string_pretty(params).expect("Serialization should be infallible."), |
| 99 | + ); |
| 100 | + |
| 101 | + let response = self.io |
| 102 | + .handle_request_sync(&request) |
| 103 | + .expect("We are sending a method call not notification."); |
| 104 | + |
| 105 | + // extract interesting part from the response |
| 106 | + let extracted = match serde_json::from_str(&response).expect("We will always get a single output.") { |
| 107 | + response::Output::Success(response::Success { result, .. }) => serde_json::to_string_pretty(&result), |
| 108 | + response::Output::Failure(response::Failure { error, .. }) => serde_json::to_string_pretty(&error), |
| 109 | + }.expect("Serialization is infallible; qed"); |
| 110 | + |
| 111 | + |
| 112 | + println!("\n{}\n --> {}\n", request, extracted); |
| 113 | + |
| 114 | + extracted |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +#[cfg(test)] |
| 119 | +mod tests { |
| 120 | + use super::*; |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn should_test_simple_method() { |
| 124 | + // given |
| 125 | + let rpc = { |
| 126 | + let mut io = rpc::IoHandler::new(); |
| 127 | + io.add_method("test_method", |_| { |
| 128 | + Ok(rpc::Value::Number(5.into())) |
| 129 | + }); |
| 130 | + Rpc::from(io) |
| 131 | + }; |
| 132 | + |
| 133 | + // when |
| 134 | + assert_eq!( |
| 135 | + rpc.request("test_method", &[5u64]), |
| 136 | + r#"5"# |
| 137 | + ); |
| 138 | + } |
| 139 | +} |
0 commit comments