24 stable releases (10 major)

18.0.0 Jul 20, 2021
17.1.0 Jun 7, 2021
17.0.0 Jan 20, 2021
16.0.0 Dec 14, 2020
8.0.0 Nov 21, 2017

#227 in Testing


Used in snarkos-rpc

MIT license

120KB
3K SLoC

An utility package to test jsonrpc-core based projects.

use jsonrpc_derive::rpc;
use jsonrpc_test as test;

use jsonrpc_core::{Result, Error, IoHandler};

#[rpc]
pub trait Test {
   #[rpc(name = "rpc_some_method")]
   fn some_method(&self, a: u64) -> Result<u64>;
}


struct Dummy;
impl Test for Dummy {
   fn some_method(&self, x: u64) -> Result<u64> {
       Ok(x * 2)
   }
}

fn main() {
  // Initialize new instance of test environment
  let rpc = test::Rpc::new(Dummy.to_delegate());

  // make a request and verify the response as a pretty-printed string
  assert_eq!(rpc.request("rpc_some_method", &[5]), r#"10"#);

  // You can also test RPC created without macros:
  let rpc = {
    let mut io = IoHandler::new();
    io.add_sync_method("rpc_test_method", |_| {
       Err(Error::internal_error())
    });
    test::Rpc::from(io)
  };

  assert_eq!(rpc.request("rpc_test_method", &()), r#"{
  "code": -32603,
  "message": "Internal error"
}"#);
}

Dependencies

~4.5–6MB
~120K SLoC