Split out the API into a separate function, to make it accessible for
unit tests.
-#[tokio::main]
-async fn main() {
- env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
-
+pub fn api() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
let hello = warp::path!("hello" / String)
.and(warp::header::<String>("user-agent"))
.and_then(hello);
let hello = warp::path!("hello" / String)
.and(warp::header::<String>("user-agent"))
.and_then(hello);
.and(warp::ws())
.map(|ws: warp::ws::Ws| { ws.on_upgrade(ws_rev_connected) });
.and(warp::ws())
.map(|ws: warp::ws::Ws| { ws.on_upgrade(ws_rev_connected) });
- .with(warp::log("warp-server"));
+ .with(warp::log("warp-server"))
+}
+#[tokio::main]
+async fn main() {
+ env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
+
+ warp::serve(api())
.run(([127, 0, 0, 1], 3030))
.await;
}
.run(([127, 0, 0, 1], 3030))
.await;
}
+
+#[cfg(test)]
+mod tests {
+ #[tokio::test]
+ async fn test_hello() {
+ let res = warp::test::request()
+ .path("/hello/rust")
+ .header("user-agent", "TestBrowser 0.1")
+ .reply(&super::api())
+ .await;
+ assert_eq!(res.status(), 200);
+ assert_eq!(res.body(), "Hello, rust from TestBrowser 0.1!");
+ }
+}