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
extern crate futures;
extern crate hyper;
extern crate memsocket;
extern crate tokio;
mod connector;
mod never;
use connector::Connector;
use futures::prelude::*;
use hyper::body::{Body, Payload};
use hyper::client::connect::Connect;
use hyper::service::{NewService, Service};
use hyper::{Client, Request, Response};
use never::Never;
use std::error::Error;
pub fn proxy_client<ResBody, ResponseError, ServiceError, ResponseFuture, ServiceFuture, S, N>(
new_service: N,
) -> Client<Connector<N>>
where
ResBody: Payload,
ResponseError: Error + Send + Sync + 'static,
ServiceError: Error + Send + Sync + 'static,
ResponseFuture: Future<Item = Response<S::ResBody>, Error = ResponseError> + Send + 'static,
ServiceFuture: Future<Item = S, Error = ServiceError> + Send + 'static,
S: Service<ReqBody = Body, ResBody = ResBody, Error = ResponseError, Future = ResponseFuture>
+ Send
+ 'static,
N: NewService<
ReqBody = S::ReqBody,
ResBody = S::ResBody,
Future = ServiceFuture,
Error = ResponseError,
Service = S,
InitError = ServiceError,
>
+ Sync
+ Send,
{
Client::builder()
.set_host(true)
.build(Connector::new(new_service))
}
pub fn proxy_client_fn<E, Fut, F>(handler: F) -> Client<impl Connect>
where
E: Error + Send + Sync + 'static,
Fut: Future<Item = Response<Body>, Error = E> + Send + 'static,
F: Fn(Request<Body>) -> Fut + Send + Sync + Copy + 'static,
{
use futures::future;
use hyper::service::service_fn;
proxy_client(move || future::ok::<_, Never>(service_fn(handler)))
}
pub fn proxy_client_fn_ok<F>(handler: F) -> Client<impl Connect>
where
F: Fn(Request<Body>) -> Response<Body> + Send + Sync + Copy + 'static,
{
use futures::future;
proxy_client_fn(move |req| future::ok::<_, Never>(handler(req)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ok() {
use futures::prelude::*;
use tokio::runtime::current_thread::Runtime;
let client = proxy_client_fn_ok(|req| {
let query = req.uri().query().unwrap().to_string();
Response::new(query.into())
});
Runtime::new()
.unwrap()
.block_on({
client
.get("https://example.com?foo=bar".parse().unwrap())
.and_then(|res| res.into_body().concat2())
.map(|bytes| {
let body = String::from_utf8(bytes.to_vec()).unwrap();
assert_eq!(body, "foo=bar");
})
.map_err(|err| panic!("{:?}", err))
})
.unwrap();
}
#[test]
fn test_err() {
use futures::future::{self, FutureResult};
use futures::prelude::*;
use std::fmt::{self, Display, Formatter};
use tokio::runtime::current_thread::Runtime;
#[derive(Debug)]
struct NewServiceError;
impl Display for NewServiceError {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
write!(fmt, "correct error for test")
}
}
impl Error for NewServiceError {
fn description(&self) -> &str {
"It broke"
}
}
impl Service for Never {
type ReqBody = Body;
type ResBody = Body;
type Error = Self;
type Future = FutureResult<Response<Self::ResBody>, Self>;
fn call(&mut self, _: Request<Self::ReqBody>) -> Self::Future {
unreachable!()
}
}
let client = proxy_client(|| future::err::<Never, _>(NewServiceError));
let _ = Runtime::new().unwrap().block_on({
client
.get("https://example.com".parse().unwrap())
.map(|res| panic!("didn't error: {:?}", res))
.map_err(|err| assert!(err.to_string().contains("correct error for test")))
});
}
}