X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=blobdiff_plain;f=warp-server%2Fsrc%2Fmain.rs;fp=warp-server%2Fsrc%2Fmain.rs;h=06b289af1405e0ed5be440a54a126134aea6bd0d;hp=fe36ca7faf989572f7eb765e64a4e8497fde8f97;hb=25dad5d64048c91e315daf4199753c9cab3a1496;hpb=f89086d8e40c377921576e04038d7a08680f7907 diff --git a/warp-server/src/main.rs b/warp-server/src/main.rs index fe36ca7..06b289a 100644 --- a/warp-server/src/main.rs +++ b/warp-server/src/main.rs @@ -71,9 +71,13 @@ pub fn api() -> impl Filter .and(warp::ws()) .map(|ws: warp::ws::Ws| { ws.on_upgrade(ws_rev_connected) }); + let static_dir = warp::path("dir") + .and(warp::fs::dir("../static")); + hello .or(ws_echo) .or(ws_rev) + .or(static_dir) .with(warp::log("warp-server")) } @@ -146,4 +150,30 @@ mod tests { client.send(warp::ws::Message::close()).await; client.recv_closed().await.unwrap(); } + + #[tokio::test] + async fn test_static_dir() { + let res = warp::test::request() + .path("/dir/plain.txt") + .reply(&super::api()) + .await; + assert_eq!(res.status(), 200); + assert_eq!(res.body(), "Hello world! This is uncompressed text.\n"); + + // subdir + let res = warp::test::request() + .path("/dir/dir1/optzip.txt") + .reply(&super::api()) + .await; + assert_eq!(res.status(), 200); + assert_eq!(res.body(), "This file is available uncompressed or compressed\n\ + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"); + + // fs::dir does not support transparent decompression + let res = warp::test::request() + .path("/dir/onlycompressed.txt") + .reply(&super::api()) + .await; + assert_eq!(res.status(), 404); + } }