Skip to content

Commit

Permalink
test: handle file for static_file module
Browse files Browse the repository at this point in the history
  • Loading branch information
joseluisq committed Jul 21, 2021
1 parent 4dc8a25 commit dd7f995
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Expand Up @@ -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"
Expand Down
52 changes: 52 additions & 0 deletions 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);
}
}

0 comments on commit dd7f995

Please sign in to comment.