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
use error::Error;
use futures::Future;
use model::XkcdResponse;
use super::{XkcdRequestSender, parse_xkcd_response};
pub fn get<'a, R>(client: &'a R, id: u32) -> Box<'a + Future<Item = XkcdResponse, Error = Error>>
where R: XkcdRequestSender,
{
let method = format!("{}/info.0.json", id);
let res = client.send(&method)
.map_err(From::from)
.and_then(|res| {
parse_xkcd_response(&res)
})
.map_err(From::from);
Box::new(res)
}
pub fn latest<'a, R>(client: &'a R) -> Box<'a + Future<Item = XkcdResponse, Error = Error>>
where R: XkcdRequestSender,
{
let res = client.send("info.0.json")
.map_err(From::from)
.and_then(|res| {
parse_xkcd_response(&res)
})
.map_err(From::from);
Box::new(res)
}
#[cfg(test)]
mod tests {
use super::{get, latest};
use test_helpers::MockXkcdRequestSender;
use tokio_core::reactor::Core;
use util::read_sample_data_from_path;
#[test]
fn test_get_comic_ok_response() {
let mut core = Core::new().unwrap();
let response = read_sample_data_from_path("tests/sample-data/example.json");
let client = MockXkcdRequestSender::respond_with(response);
let result = core.run(get(&client, 1572)).unwrap();
assert_eq!(result.num, 1572);
}
#[test]
fn test_latest_comic_ok_response() {
let mut core = Core::new().unwrap();
let response = read_sample_data_from_path("tests/sample-data/example.json");
let client = MockXkcdRequestSender::respond_with(response);
let result = core.run(latest(&client)).unwrap();
assert_eq!(result.num, 1572);
}
}