From f0836e22fa87610eeafa934311cd879ccda2fd6d Mon Sep 17 00:00:00 2001 From: mikejiang Date: Sat, 18 Feb 2017 15:01:12 +0800 Subject: [PATCH] Add tests for static_files example. --- examples/static_files/Cargo.toml | 3 ++ examples/static_files/src/main.rs | 11 +++++-- examples/static_files/src/tests.rs | 53 ++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 examples/static_files/src/tests.rs diff --git a/examples/static_files/Cargo.toml b/examples/static_files/Cargo.toml index 84d5c276..e90b7c34 100644 --- a/examples/static_files/Cargo.toml +++ b/examples/static_files/Cargo.toml @@ -6,3 +6,6 @@ workspace = "../../" [dependencies] rocket = { path = "../../lib" } rocket_codegen = { path = "../../codegen" } + +[dev-dependencies] +rocket = { path = "../../lib", features = ["testing"] } diff --git a/examples/static_files/src/main.rs b/examples/static_files/src/main.rs index 0655bc61..a47f80ff 100644 --- a/examples/static_files/src/main.rs +++ b/examples/static_files/src/main.rs @@ -3,6 +3,9 @@ extern crate rocket; +#[cfg(test)] +mod tests; + use std::io; use std::path::{Path, PathBuf}; @@ -18,6 +21,10 @@ fn files(file: PathBuf) -> Option { NamedFile::open(Path::new("static/").join(file)).ok() } -fn main() { - rocket::ignite().mount("/", routes![index, files]).launch(); +fn rocket() -> rocket::Rocket { + rocket::ignite().mount("/", routes![index, files]) +} + +fn main() { + rocket().launch(); } diff --git a/examples/static_files/src/tests.rs b/examples/static_files/src/tests.rs new file mode 100644 index 00000000..85626ee4 --- /dev/null +++ b/examples/static_files/src/tests.rs @@ -0,0 +1,53 @@ +use std::fs::File; +use std::io::Read; + +use rocket::testing::MockRequest; +use rocket::http::Method::*; +use rocket::http::Status; + +use super::rocket; + +fn test_query_file (path: &str, file: T, status: Status) + where T: Into> +{ + let rocket = rocket(); + let mut req = MockRequest::new(Get, &path); + + let mut response = req.dispatch_with(&rocket); + assert_eq!(response.status(), status); + + let body_data = response.body().and_then(|body| body.into_bytes()); + if let Some(filename) = file.into() { + let expected_data = read_file_content(filename); + assert!(body_data.map_or(false, |s| s == expected_data)); + } +} + +fn read_file_content(path: &str) -> Vec { + let mut fp = File::open(&path).expect(&format!("Can not open {}", path)); + let mut file_content = vec![]; + + fp.read_to_end(&mut file_content).expect(&format!("Reading {} failed.", path)); + file_content +} + +#[test] +fn test_index_html() { + test_query_file("/", "static/index.html", Status::Ok); +} + +#[test] +fn test_hidden_file() { + test_query_file("/hidden/hi.txt", "static/hidden/hi.txt", Status::Ok); +} + +#[test] +fn test_icon_file() { + test_query_file("/rocket-icon.jpg", "static/rocket-icon.jpg", Status::Ok); +} + +#[test] +fn test_invalid_path() { + test_query_file("/thou_shalt_not_exist", None, Status::NotFound); + test_query_file("/thou/shalt/not/exist", None, Status::NotFound); +}