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
#![forbid(future_incompatible, rust_2018_idioms)]
#![deny(missing_debug_implementations, nonstandard_style)]
#![warn(missing_docs, missing_doc_code_examples)]
#![cfg_attr(test, deny(warnings))]
#![feature(futures_api, async_await)]
use futures::{executor::block_on, prelude::*};
use http_service::{HttpService, Request, Response};
#[derive(Debug)]
pub struct TestBackend<T: HttpService> {
service: T,
connection: T::Connection,
}
impl<T: HttpService> TestBackend<T> {
fn wrap(service: T) -> Result<Self, <T::ConnectionFuture as TryFuture>::Error> {
let connection = block_on(service.connect().into_future())?;
Ok(Self {
service,
connection,
})
}
pub fn simulate(&mut self, req: Request) -> Result<Response, <T::Fut as TryFuture>::Error> {
block_on(
self.service
.respond(&mut self.connection, req)
.into_future(),
)
}
}
pub fn make_server<T: HttpService>(
service: T,
) -> Result<TestBackend<T>, <T::ConnectionFuture as TryFuture>::Error> {
TestBackend::wrap(service)
}