]> piware.de Git - learn-rust.git/commitdiff
axum-server: Add unit tests for static directory route
authorMartin Pitt <martin@piware.de>
Fri, 9 Dec 2022 13:12:38 +0000 (14:12 +0100)
committerMartin Pitt <martin@piware.de>
Fri, 9 Dec 2022 13:12:53 +0000 (14:12 +0100)
axum-server/src/main.rs

index 7bb1e94fc0cc41f990ec4646eb22d6fd4c26c366..fa44ac1695c12888da8f52dfa8f3ee38d7f16f88 100644 (file)
@@ -106,4 +106,40 @@ mod tests {
             .unwrap();
         assert_res_ok_body(res, b"Hello rust from TestBrowser 0.1").await;
     }
+
+    #[tokio::test]
+    async fn test_static_dir() {
+        let res = super::app()
+            .oneshot(Request::builder().uri("/dir/plain.txt").body(Body::empty()).unwrap())
+            .await
+            .unwrap();
+        assert_res_ok_body(res, b"Hello world! This is uncompressed text.\n").await;
+
+        // transparent .gz lookup, without gzip transfer encoding
+        let res = super::app()
+            .oneshot(Request::builder()
+                        .uri("/dir/dir1/optzip.txt")
+                        .header("accept-encoding", "deflate")
+                        .body(Body::empty()).unwrap())
+            .await
+            .unwrap();
+        assert_eq!(res.status(), StatusCode::OK);
+        // that returns the uncompressed file
+        assert_res_ok_body(res, b"This file is available uncompressed or compressed\n\
+                                  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n").await;
+
+        // transparent .gz lookup, with gzip transfer encoding
+        let res = super::app()
+            .oneshot(Request::builder()
+                        .uri("/dir/dir1/optzip.txt")
+                        .header("accept-encoding", "deflate, gzip")
+                        .body(Body::empty()).unwrap())
+            .await
+            .unwrap();
+        assert_eq!(res.status(), StatusCode::OK);
+        let res_bytes: &[u8] = &hyper::body::to_bytes(res.into_body()).await.unwrap();
+        // that returns the compressed file
+        assert_eq!(res_bytes.len(), 63); // file size of ../static/dir1/optzip.txt.gz
+        assert_eq!(res_bytes[0], 31);
+    }
 }