index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2021-07-21 22:04:48.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2021-07-21 22:04:48.0 +00:00:00
commit
dd7f995039cf06c1dc08f99f74c3a75a9b24af2a [patch]
tree
8169395ed570ed212c91ba408a8a83481c4e12b5
parent
4dc8a25876b8188b75a911264b7787a24f86bd97
download
dd7f995039cf06c1dc08f99f74c3a75a9b24af2a.tar.gz

test: handle file for static_file module



Diff

 Cargo.toml            |  3 +++-
 tests/static_files.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 55 insertions(+)

diff --git a/Cargo.toml b/Cargo.toml
index e4deda0..e5286a0 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -50,6 +50,9 @@ ctrlc = { version = "3.1", features = ["termination"] }
[target.'cfg(all(target_env = "musl", target_pointer_width = "64"))'.dependencies.jemallocator]
version = "0.3"

[dev-dependencies]
bytes = "1.0"

[profile.release]
opt-level = 3
lto = "fat"
diff --git a/tests/static_files.rs b/tests/static_files.rs
new file mode 100644
index 0000000..e4e2dd9
--- /dev/null
+++ b/tests/static_files.rs
@@ -0,0 +1,52 @@
#![deny(warnings)]

#[cfg(test)]
mod tests {
    use bytes::Bytes;
    use headers::HeaderMap;
    use http::Method;
    use std::fs;
    use std::path::PathBuf;

    extern crate static_web_server;
    use static_web_server::static_files;

    fn root_dir() -> PathBuf {
        PathBuf::from("docker/public/")
    }

    #[tokio::test]
    async fn handle_file() {
        let mut res = static_files::handle(
            &Method::GET,
            &HeaderMap::new(),
            root_dir(),
            "index.html",
            false,
        )
        .await
        .expect("unexpected response error on `handle` function");

        let buf = fs::read(root_dir().join("index.html"))
            .expect("unexpected error during index.html reading");
        let buf = Bytes::from(buf);

        assert_eq!(res.status(), 200);
        assert_eq!(res.headers()["content-length"], buf.len().to_string());
        assert_eq!(res.headers()["accept-ranges"], "bytes");

        let ctype = &res.headers()["content-type"];

        assert!(
            ctype == "text/html",
            "content-type is not html: {:?}",
            ctype,
        );

        let body = hyper::body::to_bytes(res.body_mut())
            .await
            .expect("unexpected bytes error during `body` convertion");

        assert_eq!(body, buf);
    }
}