-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
86 lines (68 loc) · 1.86 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
extern crate json_request;
extern crate iron;
#[macro_use]
extern crate router;
extern crate bodyparser;
extern crate hyper;
extern crate rustc_serialize;
use iron::prelude::*;
use iron::status;
use iron::Protocol;
use json_request::{request, Method};
struct PingServer;
impl PingServer {
pub fn build() -> Iron<Chain> {
Iron::new(Chain::new(router!(
post "/ping" => PingServer::post
)))
}
fn post(_: &mut Request) -> IronResult<Response> {
Ok(Response::with((status::Ok, "{\"pong\": true}")))
}
}
#[derive(Debug, RustcEncodable)]
struct RequestData {
ping: bool
}
#[derive(Debug, RustcDecodable)]
struct ResponseData {
pong: bool
}
struct StackListener {
server: ::hyper::server::Listening,
host: String,
}
impl StackListener {
pub fn new(port: u16) -> StackListener {
let host = format!("0.0.0.0:{}", port);
StackListener {
server: PingServer::build().listen_with(&host[..], 1, Protocol::Http).unwrap(),
host: host
}
}
pub fn url(&self, path: &str) -> String {
format!("http://{}{}", self.host, path)
}
}
impl Drop for StackListener {
fn drop(&mut self) {
self.server.close().unwrap();
}
}
#[test]
#[allow(unused_variables)]
fn with_data() {
let server = StackListener::new(40918);
let req = RequestData { ping: true };
// When this fails, the error I get it "called Option::unwrap() on a None value" which is not
// helpful for resolving what the problem is.
let url = server.url("/ping");
let res: ResponseData = request(Method::Post, &url[..], Some(req)).unwrap().unwrap();
}
#[test]
#[allow(unused_variables)]
fn none_data() {
let server = StackListener::new(40919);
let url = server.url("/ping");
let res: ResponseData = request(Method::Post, &url[..], None::<u8>).unwrap().unwrap();
}