Crate jsonrpc_test

source ·
Expand description

An utility package to test jsonrpc-core based projects.

#[macro_use]
extern crate jsonrpc_macros;

extern crate jsonrpc_core as core;
extern crate jsonrpc_test as test;

use core::Result;

build_rpc_trait! {
  pub trait Test {
    #[rpc(name = "rpc_some_method")]
    fn some_method(&self, 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 = core::IoHandler::new();
    io.add_method("rpc_test_method", |_| {
	  Err(core::Error::internal_error())
	});
    test::Rpc::from(io)
  };

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

Structs

Test RPC options.
RPC instance.