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
use hyper::Method;
use hyper::header::Header;
use tokio_core::reactor::Core;
use context::{self, Context};
use endpoint::{Endpoint, EndpointResult};
use request::{Body, Request};
#[derive(Debug)]
pub struct TestCase {
request: Request,
body: Option<Body>,
}
impl TestCase {
pub fn new(method: Method, uri: &str) -> Self {
let request = Request::new(method, uri).expect("invalid URI");
Self {
request,
body: None,
}
}
pub fn get(uri: &str) -> Self {
Self::new(Method::Get, uri)
}
pub fn post(uri: &str) -> Self {
Self::new(Method::Post, uri)
}
pub fn put(uri: &str) -> Self {
Self::new(Method::Put, uri)
}
pub fn delete(uri: &str) -> Self {
Self::new(Method::Delete, uri)
}
pub fn patch(uri: &str) -> Self {
Self::new(Method::Patch, uri)
}
pub fn with_header<H: Header>(mut self, header: H) -> Self {
self.request.headers.set(header);
self
}
pub fn with_body<B: Into<Body>>(mut self, body: B) -> Self {
self.body = Some(body.into());
self
}
}
pub fn run_test<E: Endpoint>(endpoint: E, input: TestCase) -> EndpointResult<Result<E::Item, E::Error>> {
let req = input.request;
let body = input.body.unwrap_or_default();
let base = context::RequestInfo::new(&req, body);
let mut ctx = Context::from(&base);
let f = endpoint.apply(&mut ctx)?;
let mut core = Core::new().unwrap();
Ok(core.run(f))
}