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
#[macro_use]
extern crate log;
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use futures;
use futures::lock::Mutex;
use futures::Future;
use hyper::{service::Service, Uri};
mod streams;
pub use crate::streams::MockPollStream;
#[macro_export]
macro_rules! mock_connector (
($name:ident {
$($url:expr => $res:expr)*
}) => (
#[derive(Clone)]
pub struct $name($crate::HostToReplyConnector);
impl Default for $name {
fn default() -> Self {
let mut c = Self(Default::default());
$(c.0.m.insert($url.to_string(), $res.to_string());)*
c
}
}
impl hyper::service::Service<hyper::Uri> for $name {
type Response = $crate::MockPollStream;
type Error = std::io::Error;
type Future = std::pin::Pin<Box<dyn std::future::Future<Output = Result<Self::Response, Self::Error>> + Send>>;
fn poll_ready(&mut self, cx: &mut std::task::Context<'_>) -> std::task::Poll<Result<(), Self::Error>> {
self.0.poll_ready(cx)
}
fn call(&mut self, req: hyper::Uri) -> Self::Future {
self.0.call(req)
}
}
)
);
#[derive(Default, Clone)]
pub struct HostToReplyConnector {
pub m: HashMap<String, String>,
}
impl Service<Uri> for HostToReplyConnector {
type Response = crate::MockPollStream;
type Error = std::io::Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Uri) -> Self::Future {
debug!("HostToReplyConnector::connect({:?})", req);
match (|| {
self.m.get(&format!("{}://{}", req.scheme()?, req.host()?))
})() {
Some(res) => Box::pin(futures::future::ok(MockPollStream::new(
res.clone().into_bytes(),
))),
None => panic!("HostToReplyConnector doesn't know url {}", req),
}
}
}
#[macro_export]
macro_rules! mock_connector_in_order (
($name:ident {
$( $res:expr )*
}) => (
#[derive(Clone)]
pub struct $name($crate::SequentialConnector);
impl Default for $name {
fn default() -> $name {
Self($crate::SequentialConnector::new(vec![
$($res.to_string(),)*
]))
}
}
impl hyper::service::Service<hyper::Uri> for $name {
type Response = $crate::MockPollStream;
type Error = std::io::Error;
type Future = std::pin::Pin<Box<dyn std::future::Future<Output = Result<Self::Response, Self::Error>> + Send>>;
fn poll_ready(&mut self, cx: &mut std::task::Context<'_>) -> std::task::Poll<Result<(), Self::Error>> {
self.0.poll_ready(cx)
}
fn call(&mut self, req: hyper::Uri) -> Self::Future {
self.0.call(req)
}
}
)
);
#[derive(Clone)]
pub struct SequentialConnector {
pub content: Arc<[String]>,
pub current: Arc<Mutex<usize>>,
}
impl SequentialConnector {
pub fn new(content: impl Into<Box<[String]>>) -> Self {
let content = content.into();
assert!(
content.len() != 0,
"Not a single streamer return value specified"
);
SequentialConnector {
content: content.into(),
current: Arc::new(Mutex::new(0)),
}
}
}
impl Service<Uri> for SequentialConnector {
type Response = crate::MockPollStream;
type Error = std::io::Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Uri) -> Self::Future {
debug!("SequentialConnector::connect({:?})", req);
let content = self.content.clone();
let current = self.current.clone();
Box::pin(async move {
let mut current = current.lock().await;
let data = content[*current].clone().into_bytes();
*current = *current + 1;
Ok(MockPollStream::new(data))
})
}
}