|
| 1 | +// Copyright 2017 Amagicom AB. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | +// option. This file may not be copied, modified, or distributed |
| 7 | +// except according to those terms. |
| 8 | + |
| 9 | +extern crate env_logger; |
| 10 | +extern crate futures; |
| 11 | +#[macro_use] |
| 12 | +extern crate jsonrpc_client_core; |
| 13 | +extern crate jsonrpc_client_http; |
| 14 | + |
| 15 | +extern crate tokio_core; |
| 16 | + |
| 17 | +extern crate jsonrpc_core; |
| 18 | +extern crate jsonrpc_http_server; |
| 19 | +#[macro_use] |
| 20 | +extern crate jsonrpc_macros; |
| 21 | + |
| 22 | +use futures::Future; |
| 23 | + |
| 24 | +use jsonrpc_client_http::HttpTransport; |
| 25 | +use jsonrpc_core::{Error, IoHandler}; |
| 26 | +use jsonrpc_http_server::ServerBuilder; |
| 27 | + |
| 28 | + |
| 29 | +// Generate server API trait. Actual implementation at bottom of file. |
| 30 | +build_rpc_trait! { |
| 31 | + pub trait ServerApi { |
| 32 | + #[rpc(name = "to_upper")] |
| 33 | + fn to_upper(&self, String) -> Result<String, Error>; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// Generate client struct with same API as server. |
| 38 | +jsonrpc_client!(pub struct ToUpperClient { |
| 39 | + pub fn to_upper(&mut self, string: &str) -> RpcRequest<String>; |
| 40 | +}); |
| 41 | + |
| 42 | + |
| 43 | +#[test] |
| 44 | +fn localhost_ping_pong() { |
| 45 | + env_logger::init().unwrap(); |
| 46 | + |
| 47 | + // Spawn a server hosting the `ServerApi` API. |
| 48 | + let server = spawn_server(); |
| 49 | + |
| 50 | + let uri = format!("http://{}", server.address()); |
| 51 | + println!("Testing towards server at {}", uri); |
| 52 | + |
| 53 | + // Create the Tokio Core event loop that will drive the RPC client and the async requests. |
| 54 | + let mut core = tokio_core::reactor::Core::new().unwrap(); |
| 55 | + |
| 56 | + // Create the HTTP transport handle and create a RPC client with that handle. |
| 57 | + let transport = HttpTransport::shared(&core.handle()) |
| 58 | + .unwrap() |
| 59 | + .handle(&uri) |
| 60 | + .unwrap(); |
| 61 | + let mut client = ToUpperClient::new(transport); |
| 62 | + |
| 63 | + // Just calling the method gives back a `RpcRequest`, which is a future |
| 64 | + // that can be used to execute the actual RPC call. |
| 65 | + let rpc_future1 = client.to_upper("MaKe me UppeRcase!!1"); |
| 66 | + let rpc_future2 = client.to_upper("foobar"); |
| 67 | + |
| 68 | + let joined_future = rpc_future1.join(rpc_future2); |
| 69 | + // Run the event loop with the two calls to make them actually run. |
| 70 | + let (result1, result2) = core.run(joined_future).unwrap(); |
| 71 | + |
| 72 | + assert_eq!("MAKE ME UPPERCASE!!1", result1); |
| 73 | + assert_eq!("FOOBAR", result2); |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +/// Simple struct that will implement the RPC API defined at the top of this file. |
| 78 | +struct Server; |
| 79 | + |
| 80 | +impl ServerApi for Server { |
| 81 | + fn to_upper(&self, s: String) -> Result<String, Error> { |
| 82 | + Ok(s.to_uppercase()) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +fn spawn_server() -> jsonrpc_http_server::Server { |
| 87 | + let server = Server; |
| 88 | + let mut io = IoHandler::new(); |
| 89 | + io.extend_with(server.to_delegate()); |
| 90 | + |
| 91 | + ServerBuilder::new(io) |
| 92 | + .start_http(&"127.0.0.1:0".parse().unwrap()) |
| 93 | + .unwrap() |
| 94 | +} |
0 commit comments