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
#[macro_use]
extern crate log;
use std::sync::Mutex;
use std::collections::HashMap;
use futures;
use futures::Future;
use hyper::client::connect;
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() -> $name {
let mut c = $name(Default::default());
$(c.0.m.insert($url.to_string(), $res.to_string());)*
c
}
}
impl hyper::client::connect::Connect for $name {
type Transport = $crate::MockPollStream;
type Error = std::io::Error;
type Future = Box<Future<Item=(Self::Transport, hyper::client::connect::Connected), Error=Self::Error> + Send>;
fn connect(&self, dst: hyper::client::connect::Destination) -> Self::Future {
let key = format!("{}://{}", dst.scheme(), dst.host());
match self.0.m.get(&key) {
Some(res) => Box::new(futures::future::ok(($crate::MockPollStream::new(res.clone().into_bytes()), hyper::client::connect::Connected::new()))),
None => panic!("mock_connector doesn't know url {}", key)
}
}
}
)
);
#[derive(Default, Clone)]
pub struct HostToReplyConnector {
pub m: HashMap<String, String>
}
impl connect::Connect for HostToReplyConnector {
type Transport = MockPollStream;
type Error = std::io::Error;
type Future = Box<dyn Future<Item=(Self::Transport, connect::Connected), Error=Self::Error> + Send>;
fn connect(&self, dst: connect::Destination) -> Self::Future {
debug!("HostToReplyConnector::connect({:?})", dst);
let key = format!("{}://{}", dst.scheme(), dst.host());
match self.m.get(&key) {
Some(res) => Box::new(futures::future::ok((MockPollStream::new(res.clone().into_bytes()), connect::Connected::new()))),
None => panic!("HostToReplyConnector doesn't know url {}", key)
}
}
}
#[macro_export]
macro_rules! mock_connector_in_order (
($name:ident {
$( $res:expr )*
}) => (
pub struct $name($crate::SequentialConnector);
impl Default for $name {
fn default() -> $name {
let mut c = $name(Default::default());
$(c.0.content.push($res.to_string());)*
c
}
}
impl hyper::client::connect::Connect for $name {
type Transport = $crate::MockPollStream;
type Error = std::io::Error;
type Future = Box<Future<Item=(Self::Transport, hyper::client::connect::Connected), Error=Self::Error> + Send>;
fn connect(&self, _dst: hyper::client::connect::Destination) -> Self::Future {
assert!(self.0.content.len() != 0, "Not a single streamer return value specified");
let data = self.0.content[*self.0.current.lock().unwrap()].clone().into_bytes();
*self.0.current.lock().unwrap() += 1;
Box::new(futures::future::ok(($crate::MockPollStream::new(data), hyper::client::connect::Connected::new())))
}
}
)
);
pub struct SequentialConnector {
pub content: Vec<String>,
pub current: Mutex<usize>,
}
impl Default for SequentialConnector {
fn default() -> Self {
SequentialConnector {
content: Vec::new(),
current: Mutex::new(0)
}
}
}
impl connect::Connect for SequentialConnector {
type Transport = MockPollStream;
type Error = std::io::Error;
type Future = Box<dyn Future<Item=(Self::Transport, connect::Connected), Error=Self::Error> + Send>;
fn connect(&self, dst: connect::Destination) -> Self::Future {
debug!("SequentialConnector::connect({:?})", dst);
assert!(self.content.len() != 0, "Not a single streamer return value specified");
let data = self.content[*self.current.lock().unwrap()].clone().into_bytes();
*self.current.lock().unwrap() += 1;
Box::new(futures::future::ok((MockPollStream::new(data), connect::Connected::new())))
}
}