Skip to content

Commit a79ff2b

Browse files
committed
Merge branch 'add-integration-test'
2 parents 38ec5f0 + 93dca45 commit a79ff2b

File tree

4 files changed

+109
-5
lines changed

4 files changed

+109
-5
lines changed

http/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ tls = ["hyper-tls", "native-tls"]
2626

2727
[dev-dependencies]
2828
env_logger = "*"
29-
jsonrpc-core = "7.1.0"
30-
jsonrpc-macros = "7.1.0"
31-
jsonrpc-http-server = "7.1.0"
29+
jsonrpc-core = "7.1.1"
30+
jsonrpc-macros = "7.1.1"
31+
jsonrpc-http-server = "7.1.1"
3232

3333

3434
[badges]

http/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ When TLS support is compiled in the instances returned by
2525
[`HttpTransport::shared`](struct.HttpTransport.html#method.shared) support both plaintext http
2626
and https over TLS, backed by the `hyper_tls::HttpsConnector` connector.
2727

28-
## Usage
28+
## Examples
29+
30+
See the integration test in `tests/localhost.rs` for code that creates an actual HTTP server
31+
with `jsonrpc_http_server`, and sends requests to it with this crate.
32+
33+
Here is a small example of how to use this crate together with `jsonrpc_core`:
2934

3035
```rust
3136
#[macro_use] extern crate jsonrpc_client_core;

http/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@
3131
//! [`HttpTransport::shared`](struct.HttpTransport.html#method.shared) support both plaintext http
3232
//! and https over TLS, backed by the `hyper_tls::HttpsConnector` connector.
3333
//!
34-
//! # Usage
34+
//! # Examples
35+
//!
36+
//! See the integration test in `tests/localhost.rs` for code that creates an actual HTTP server
37+
//! with `jsonrpc_http_server`, and sends requests to it with this crate.
38+
//!
39+
//! Here is a small example of how to use this crate together with `jsonrpc_core`:
3540
//!
3641
//! ```rust,no_run
3742
//! #[macro_use] extern crate jsonrpc_client_core;

http/tests/localhost.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)