]> piware.de Git - learn-rust.git/blobdiff - warp-server/src/main.rs
concepts: rustfmt
[learn-rust.git] / warp-server / src / main.rs
index 7aa6f977d831c977800cb248818231a3b85d21fe..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"))
 }
 
@@ -98,4 +102,78 @@ mod tests {
         assert_eq!(res.status(), 200);
         assert_eq!(res.body(), "Hello, rust from TestBrowser 0.1!");
     }
+
+    #[tokio::test]
+    async fn test_ws_echo() {
+        let mut client = warp::test::ws()
+            .path("/ws-echo")
+            .handshake(super::api())
+            .await
+            .expect("handshake failed");
+
+        // text
+        client.send_text("Hello").await;
+        let reply = client.recv().await.unwrap();
+        assert_eq!(reply.to_str().unwrap(), "Hello");
+
+        // binary
+        let msg: Vec<u8> = vec![42, 99];
+        client.send(warp::ws::Message::binary(msg.clone())).await;
+        let reply = client.recv().await.unwrap();
+        assert_eq!(reply.as_bytes(), &msg);
+
+        //close
+        client.send(warp::ws::Message::close()).await;
+        client.recv_closed().await.unwrap();
+    }
+
+    #[tokio::test]
+    async fn test_ws_rev() {
+        let mut client = warp::test::ws()
+            .path("/ws-rev")
+            .handshake(super::api())
+            .await
+            .expect("handshake failed");
+
+        // text
+        client.send_text("Hello\n").await;
+        let reply = client.recv().await.unwrap();
+        assert_eq!(reply.to_str().unwrap(), "\nolleH");
+
+        // binary
+        let msg: Vec<u8> = vec![42, 99];
+        client.send(warp::ws::Message::binary(msg)).await;
+        let reply = client.recv().await.unwrap();
+        assert_eq!(reply.as_bytes(), vec![99, 42]);
+
+        //close
+        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);
+    }
 }