From 25dad5d64048c91e315daf4199753c9cab3a1496 Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Fri, 9 Dec 2022 10:25:18 +0100 Subject: [PATCH] warp-server: Add route for serving static directory Add a static/ file tree for testing. --- static/README.md | 1 + static/dir1/optzip.txt | 2 ++ static/dir1/optzip.txt.gz | Bin 0 -> 63 bytes static/onlycompressed.txt.gz | Bin 0 -> 66 bytes static/plain.txt | 1 + warp-server/src/main.rs | 30 ++++++++++++++++++++++++++++++ 6 files changed, 34 insertions(+) create mode 100644 static/README.md create mode 100644 static/dir1/optzip.txt create mode 100644 static/dir1/optzip.txt.gz create mode 100644 static/onlycompressed.txt.gz create mode 100644 static/plain.txt diff --git a/static/README.md b/static/README.md new file mode 100644 index 0000000..41f2f35 --- /dev/null +++ b/static/README.md @@ -0,0 +1 @@ +This directory contains static files for serving through HTTP frameworks. diff --git a/static/dir1/optzip.txt b/static/dir1/optzip.txt new file mode 100644 index 0000000..cc31dba --- /dev/null +++ b/static/dir1/optzip.txt @@ -0,0 +1,2 @@ +This file is available uncompressed or compressed +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA diff --git a/static/dir1/optzip.txt.gz b/static/dir1/optzip.txt.gz new file mode 100644 index 0000000000000000000000000000000000000000..5742cd618a6813ecb5f72453f1dbf9b2c4e6eee8 GIT binary patch literal 63 zcmb2|=3oE;CT8xFCv?I*PM`4%Ww_Lt+Bws*)6>B(Gp?vE&oMBourRC3scs@u)A6Ss PN)MPA;#qVwlY!a*h@lj% literal 0 HcmV?d00001 diff --git a/static/onlycompressed.txt.gz b/static/onlycompressed.txt.gz new file mode 100644 index 0000000000000000000000000000000000000000..8542d289a839292e2d4e2dac2ab643b48814f38b GIT binary patch literal 66 zcmb2|=HTe~Gbx#YIX^F_GC4oDpeVJtI5kDDq@sl3^ZC;&Lp*f6&z$k}IprDZp%dn@ W{*=yxq9p 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); + } } -- 2.39.2