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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
pub mod request;
use std::convert::TryFrom;
use std::fmt;
use std::ops::{Deref, DerefMut};
use anyhow::anyhow;
use futures::prelude::*;
use hyper::client::connect::Connect;
use hyper::client::Client;
use hyper::header::CONTENT_TYPE;
use hyper::{body, Body, Method, Response, Uri};
use log::warn;
use mime;
use tokio::time::Sleep;
pub use crate::plain::test::TestServer;
use futures::TryFutureExt;
pub use request::TestRequest;
pub(crate) trait BodyReader {
fn read_body(&mut self, response: Response<Body>) -> Result<Vec<u8>, hyper::Error>;
}
pub trait Server: Clone {
fn run_future<F, O>(&self, future: F) -> O
where
F: Future<Output = O>;
fn request_expiry(&self) -> Sleep;
fn run_request<F>(&self, f: F) -> anyhow::Result<F::Ok>
where
F: TryFuture + Unpin + Send + 'static,
F::Ok: Send,
F::Error: Into<anyhow::Error> + Send,
{
let expiry_fut = self
.request_expiry()
.then(future::ok::<(), F::Error>)
.boxed();
self.run_future(
future::try_select(f, expiry_fut)
.map_err(|either| either.factor_first().0.into())
.and_then(|might_expire| {
future::ready(match might_expire {
future::Either::Left((item, _)) => Ok(item),
future::Either::Right(_) => Err(anyhow!("timed out")),
})
})
.into_future()
.map_err(|error| error.into()),
)
}
}
impl<T: Server> BodyReader for T {
fn read_body(&mut self, response: Response<Body>) -> Result<Vec<u8>, hyper::Error> {
let f = body::to_bytes(response.into_body()).and_then(|b| future::ok(b.to_vec()));
self.run_future(f).map_err(|error| error.into())
}
}
pub struct TestClient<TS: Server, C: Connect> {
pub(crate) client: Client<C, Body>,
pub(crate) test_server: TS,
}
impl<TS: Server + 'static, C: Connect + Clone + Send + Sync + 'static> TestClient<TS, C> {
pub fn head<U>(&self, uri: U) -> TestRequest<TS, C>
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<http::Error>,
{
self.build_request(Method::HEAD, uri)
}
pub fn get<U>(&self, uri: U) -> TestRequest<TS, C>
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<http::Error>,
{
self.build_request(Method::GET, uri)
}
pub fn options<U>(&self, uri: U) -> TestRequest<TS, C>
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<http::Error>,
{
self.build_request(Method::OPTIONS, uri)
}
pub fn post<B, U>(&self, uri: U, body: B, mime: mime::Mime) -> TestRequest<TS, C>
where
B: Into<Body>,
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<http::Error>,
{
self.build_request_with_body(Method::POST, uri, body, mime)
}
pub fn put<B, U>(&self, uri: U, body: B, mime: mime::Mime) -> TestRequest<TS, C>
where
B: Into<Body>,
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<http::Error>,
{
self.build_request_with_body(Method::PUT, uri, body, mime)
}
pub fn patch<B, U>(&self, uri: U, body: B, mime: mime::Mime) -> TestRequest<TS, C>
where
B: Into<Body>,
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<http::Error>,
{
self.build_request_with_body(Method::PATCH, uri, body, mime)
}
pub fn delete<U>(&self, uri: U) -> TestRequest<TS, C>
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<http::Error>,
{
self.build_request(Method::DELETE, uri)
}
pub fn build_request<U>(&self, method: Method, uri: U) -> TestRequest<TS, C>
where
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<http::Error>,
{
TestRequest::new(self, method, uri)
}
pub fn build_request_with_body<B, U>(
&self,
method: Method,
uri: U,
body: B,
mime: mime::Mime,
) -> TestRequest<TS, C>
where
B: Into<Body>,
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<http::Error>,
{
let mut request = self.build_request(method, uri);
{
let headers = request.headers_mut();
headers.insert(CONTENT_TYPE, mime.to_string().parse().unwrap());
}
*request.body_mut() = body.into();
request
}
pub fn perform(&self, req: TestRequest<TS, C>) -> anyhow::Result<TestResponse> {
let req_future = self.client.request(req.request()).map_err(|e| {
warn!("Error from test client request {:?}", e);
e
});
self.test_server
.run_request(req_future)
.map(|response| TestResponse {
response,
reader: Box::new(self.test_server.clone()),
})
}
}
pub struct TestResponse {
response: Response<Body>,
reader: Box<dyn BodyReader>,
}
impl Deref for TestResponse {
type Target = Response<Body>;
fn deref(&self) -> &Response<Body> {
&self.response
}
}
impl DerefMut for TestResponse {
fn deref_mut(&mut self) -> &mut Response<Body> {
&mut self.response
}
}
impl fmt::Debug for TestResponse {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TestResponse")
}
}
impl Into<Response<Body>> for TestResponse {
fn into(self) -> Response<Body> {
self.response
}
}
impl TestResponse {
pub fn read_body(mut self) -> Result<Vec<u8>, hyper::Error> {
self.reader.read_body(self.response)
}
pub fn read_utf8_body(self) -> anyhow::Result<String> {
let buf = self.read_body()?;
let s = String::from_utf8(buf)?;
Ok(s)
}
}