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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
use crate::server::data::ApplicationState;

use regex::Regex;

use futures::{future, Future, Stream};

use hyper::service::service_fn;
use hyper::{
    http::response::Builder as HyperResponseBuilder, Body, Chunk, HeaderMap,
    Request as HyperRequest, Response as HyperResponse, Server, StatusCode,
};

use hyper::header::HeaderValue;
use hyper::http::header::HeaderName;
use std::collections::BTreeMap;
use std::str::FromStr;

pub(crate) mod data;
mod handlers;
mod routes;
mod util;

type GenericError = Box<dyn std::error::Error + Send + Sync>;
type ResponseFuture = Box<dyn Future<Item = HyperResponse<Body>, Error = GenericError> + Send>;

/// Holds server configuration properties.
#[derive(Debug)]
pub struct HttpMockConfig {
    pub port: u16,
    pub workers: usize,
    pub expose: bool,
}

impl HttpMockConfig {
    pub fn new(port: u16, workers: usize, expose: bool) -> Self {
        Self {
            port,
            workers,
            expose,
        }
    }
}

#[derive(Default, Debug)]
pub(crate) struct ServerRequestHeader {
    pub method: String,
    pub path: String,
    pub query: String,
    pub headers: BTreeMap<String, String>,
}

impl ServerRequestHeader {
    pub fn from(req: &HyperRequest<Body>) -> Result<ServerRequestHeader, String> {
        let headers = extract_headers(req.headers());
        if let Err(e) = headers {
            return Err(format!("error parsing headers: {}", e));
        }

        let method = req.method().as_str().to_string();
        let path = req.uri().path().to_string();
        let query = req.uri().query().unwrap_or("").to_string();
        let headers = headers.unwrap();

        let server_request = ServerRequestHeader::new(method, path, query, headers);

        Ok(server_request)
    }

    pub fn new(
        method: String,
        path: String,
        query: String,
        headers: BTreeMap<String, String>,
    ) -> Self {
        Self {
            method,
            path,
            query,
            headers,
        }
    }
}

#[derive(Default, Debug)]
pub(crate) struct ServerResponse {
    pub status: u16,
    pub headers: BTreeMap<String, String>,
    pub body: String,
}

impl ServerResponse {
    pub fn new(status: u16, headers: BTreeMap<String, String>, body: String) -> Self {
        Self {
            status,
            headers,
            body,
        }
    }
}

/// Extracts all headers from the URI of the given request.
fn extract_headers(header_map: &HeaderMap) -> Result<BTreeMap<String, String>, String> {
    let mut headers = BTreeMap::new();
    for (hn, hv) in header_map {
        let hn = hn.as_str().to_string();
        let hv = hv.to_str();
        if let Err(e) = hv {
            return Err(format!("error parsing headers: {}", e));
        }
        headers.insert(hn, hv.unwrap().to_string());
    }
    Ok(headers)
}

/// Starts a new instance of an HTTP mock server. You should never need to use this function
/// directly. Use it if you absolutely need to manage the low-level details of how the mock
/// server operates.
pub fn start_server(http_mock_config: HttpMockConfig) {
    let port = http_mock_config.port;
    let host = match http_mock_config.expose {
        true => "0.0.0.0",    // allow traffic from all sources
        false => "127.0.0.1", // allow traffic from localhost only
    };

    hyper::rt::run(future::lazy(move || {
        let new_service = move || {
            service_fn(|req: HyperRequest<Body>| {
                let request_header = ServerRequestHeader::from(&req);
                Box::new(
                    req.into_body()
                        .concat2()
                        .from_err()
                        .and_then(|entire_body: Chunk| {
                            if let Err(e) = request_header {
                                return Ok(error_response(format!("Cannot parse request: {}", e)));
                            }

                            let body = String::from_utf8(entire_body.to_vec());
                            if let Err(e) = body {
                                return Ok(error_response(format!("Cannot read body: {}", e)));
                            }

                            let routing_result =
                                route_request(&STATE, &request_header.unwrap(), body.unwrap());
                            if let Err(e) = routing_result {
                                return Ok(error_response(format!("Request handler error: {}", e)));
                            }

                            let response = map_response(routing_result.unwrap());
                            if let Err(e) = response {
                                return Ok(error_response(format!("Cannot build response: {}", e)));
                            }

                            Ok(response.unwrap())
                        }),
                ) as ResponseFuture
            })
        };

        let addr = &format!("{}:{}", host, port).parse().unwrap();
        let server = Server::bind(&addr)
            .serve(new_service)
            .map_err(|e| log::error!("server error: {}", e));

        log::info!("Listening on {}", addr);

        server
    }));
}

/// Maps a server response to a hyper response.
fn map_response(route_response: ServerResponse) -> Result<HyperResponse<Body>, String> {
    let mut builder = HyperResponse::builder();
    builder.status(route_response.status);

    for (k, v) in route_response.headers {
        let value = add_header(&mut builder, &k, &v);
        if let Err(e) = value {
            return Err(format!("Cannot create header from value string: {}", e));
        }
    }

    let result = builder.body(Body::from(route_response.body));
    if let Err(e) = result {
        return Err(format!("Cannot create HTTP response: {}", e));
    }

    Ok(result.unwrap())
}

/// Adds a header to a hyper response.
fn add_header(builder: &mut HyperResponseBuilder, key: &str, value: &str) -> Result<(), String> {
    let name = HeaderName::from_str(key);
    if let Err(e) = name {
        return Err(format!("Cannot create header from name: {}", e));
    }

    let value = HeaderValue::from_str(value);
    if let Err(e) = value {
        return Err(format!("Cannot create header from value: {}", e));
    }

    let value = value.unwrap();
    let value = value.to_str();
    if let Err(e) = value {
        return Err(format!("Cannot create header from value string: {}", e));
    }

    builder.header(name.unwrap(), value.unwrap());

    Ok(())
}

/// Routes a request to the appropriate route handler.
fn route_request(
    state: &ApplicationState,
    request_header: &ServerRequestHeader,
    body: String,
) -> Result<ServerResponse, String> {
    log::trace!("Routing incoming request: {:?}", request_header);

    if MOCKS_PATH.is_match(&request_header.path) {
        match request_header.method.as_str() {
            "POST" => return routes::add(state, body),
            "DELETE" => return routes::delete_all(state),
            _ => {}
        }
    }

    if MOCK_PATH.is_match(&request_header.path) {
        let id = get_path_param(&MOCK_PATH, 1, &request_header.path);
        if let Err(e) = id {
            return Err(format!("Cannot parse id from path: {}", e));
        }
        let id = id.unwrap();

        match request_header.method.as_str() {
            "GET" => return routes::read_one(state, id),
            "DELETE" => return routes::delete_one(state, id),
            _ => {}
        }
    }

    return routes::serve(state, request_header, body);
}

/// Get request path parameters.
fn get_path_param(regex: &Regex, idx: usize, path: &str) -> Result<usize, String> {
    let cap = regex.captures(path);
    if cap.is_none() {
        return Err(format!(
            "Error capturing parameter from request path: {}",
            path
        ));
    }
    let cap = cap.unwrap();

    let id = cap.get(idx);
    if id.is_none() {
        return Err(format!(
            "Error capturing resource id in request path: {}",
            path
        ));
    }
    let id = id.unwrap().as_str();

    let id = id.parse::<usize>();
    if let Err(e) = id {
        return Err(format!("Error parsing id as a number: {}", e));
    }
    let id = id.unwrap();

    Ok(id)
}

/// Creates a default error response.
fn error_response(body: String) -> HyperResponse<Body> {
    HyperResponse::builder()
        .status(StatusCode::INTERNAL_SERVER_ERROR)
        .body(Body::from(body))
        .expect("Cannot build route error response")
}

lazy_static! {
    static ref MOCK_PATH: Regex = Regex::new(r"/__mocks/([0-9]+)$").unwrap();
    static ref MOCKS_PATH: Regex = Regex::new(r"/__mocks$").unwrap();
    static ref STATE: ApplicationState = ApplicationState::new();
}

#[cfg(test)]
mod test {
    use crate::server::{MOCKS_PATH, MOCK_PATH};

    #[test]
    fn route_regex_test() {
        assert_eq!(MOCK_PATH.is_match("/__mocks/1"), true);
        assert_eq!(MOCK_PATH.is_match("/__mocks/1295473892374"), true);
        assert_eq!(MOCK_PATH.is_match("/__mocks/abc"), false);
        assert_eq!(MOCK_PATH.is_match("/__mocks"), false);
        assert_eq!(MOCK_PATH.is_match("/__mocks/345345/test"), false);
        assert_eq!(MOCK_PATH.is_match("test/__mocks/345345/test"), false);

        assert_eq!(MOCKS_PATH.is_match("/__mocks"), true);
        assert_eq!(MOCKS_PATH.is_match("/__mocks/5"), false);
        assert_eq!(MOCKS_PATH.is_match("test/__mocks/5"), false);
        assert_eq!(MOCKS_PATH.is_match("test/__mocks/567"), false);
    }
}