-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
78 lines (67 loc) · 2.08 KB
/
lib.rs
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
use serde::{Deserialize, Serialize};
use warp::{filters::BoxedFilter, Filter, Reply};
/// # Response Struct
///
/// `Response` implements `warp::Reply` and is serializable into JSON.
/// The purpose of the Struct is to act as a container for the repsonse body.
///
/// It takes a user agent as a parameter and uses it to define a private `message` property.
///
/// `Response`'s implementation of `warp::Reply` allows `Response` to be called in a `warp` filter's `map` method.
///
/// `Response` will be serialized into JSON string and sent to the client.
#[derive(Serialize, Deserialize, Debug)]
pub struct Response {
message: String,
}
impl warp::Reply for Response {
fn into_response(self) -> warp::reply::Response {
warp::reply::json(&self).into_response()
}
}
impl Response {
fn new(user_agent: String) -> Response {
Response {
message: create_message(&user_agent),
}
}
}
fn create_message(user_agent: &str) -> String {
format!("Hello world!!!! Your user agent is {}", user_agent)
}
/// # Usage in function wrapper
///
/// `main` returns a `BoxedFilter` that is consumed by the [rust-warp template](https://github.com/austinrivas/rust-warp-template).
///
/// ```
/// warp::serve(handler::main())
/// .run(([127, 0, 0, 1], 3000));
/// ```
pub fn main() -> BoxedFilter<(impl Reply,)> {
warp::get()
.and(warp::header("user-agent"))
.map(Response::new)
.boxed()
}
#[cfg(test)]
mod tests {
use serde_json::json;
use std::str;
use warp::http::StatusCode;
use warp::test::request;
use super::*;
#[tokio::test]
async fn test_main() {
let fn_handler = main();
let user_agent = "test";
let expected_body = json!({ "message": create_message(user_agent) });
let resp = request()
.method("GET")
.header("user-agent", user_agent)
.reply(&fn_handler)
.await;
let body = str::from_utf8(resp.body()).unwrap();
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(body, expected_body.to_string());
}
}