]> piware.de Git - learn-rust.git/commitdiff
warp-server: Add route for serving static directory
authorMartin Pitt <martin@piware.de>
Fri, 9 Dec 2022 09:25:18 +0000 (10:25 +0100)
committerMartin Pitt <martin@piware.de>
Fri, 9 Dec 2022 09:25:18 +0000 (10:25 +0100)
Add a static/ file tree for testing.

static/README.md [new file with mode: 0644]
static/dir1/optzip.txt [new file with mode: 0644]
static/dir1/optzip.txt.gz [new file with mode: 0644]
static/onlycompressed.txt.gz [new file with mode: 0644]
static/plain.txt [new file with mode: 0644]
warp-server/src/main.rs

diff --git a/static/README.md b/static/README.md
new file mode 100644 (file)
index 0000000..41f2f35
--- /dev/null
@@ -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 (file)
index 0000000..cc31dba
--- /dev/null
@@ -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 (file)
index 0000000..5742cd6
Binary files /dev/null and b/static/dir1/optzip.txt.gz differ
diff --git a/static/onlycompressed.txt.gz b/static/onlycompressed.txt.gz
new file mode 100644 (file)
index 0000000..8542d28
Binary files /dev/null and b/static/onlycompressed.txt.gz differ
diff --git a/static/plain.txt b/static/plain.txt
new file mode 100644 (file)
index 0000000..9300012
--- /dev/null
@@ -0,0 +1 @@
+Hello world! This is uncompressed text.
index fe36ca7faf989572f7eb765e64a4e8497fde8f97..06b289af1405e0ed5be440a54a126134aea6bd0d 100644 (file)
@@ -71,9 +71,13 @@ pub fn api() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection>
         .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);
+    }
 }