From 83e33cf0bef56d46b97815760bf0498c0db24ecf Mon Sep 17 00:00:00 2001 From: Cliff H Date: Thu, 29 Dec 2016 11:41:23 -0500 Subject: [PATCH] Add tests for redirect example. --- examples/redirect/Cargo.toml | 3 +++ examples/redirect/src/main.rs | 3 +++ examples/redirect/src/tests.rs | 38 ++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 examples/redirect/src/tests.rs diff --git a/examples/redirect/Cargo.toml b/examples/redirect/Cargo.toml index 5a59bcfb..e71d8bcc 100644 --- a/examples/redirect/Cargo.toml +++ b/examples/redirect/Cargo.toml @@ -7,3 +7,6 @@ workspace = "../../" [dependencies] rocket = { path = "../../lib" } rocket_codegen = { path = "../../codegen" } + +[dev-dependencies] +rocket = { path = "../../lib", features = ["testing"] } diff --git a/examples/redirect/src/main.rs b/examples/redirect/src/main.rs index 0fad379c..bfbf4104 100644 --- a/examples/redirect/src/main.rs +++ b/examples/redirect/src/main.rs @@ -1,7 +1,10 @@ #![feature(plugin)] #![plugin(rocket_codegen)] + extern crate rocket; +#[cfg(test)] mod tests; + use rocket::response::Redirect; #[get("/")] diff --git a/examples/redirect/src/tests.rs b/examples/redirect/src/tests.rs new file mode 100644 index 00000000..69848b0d --- /dev/null +++ b/examples/redirect/src/tests.rs @@ -0,0 +1,38 @@ +use super::rocket; +use rocket::testing::MockRequest; +use rocket::Response; +use rocket::http::Method::*; +use rocket::http::Status; + +macro_rules! run_test { + ($path:expr, $test_fn:expr) => ({ + let rocket = rocket::ignite().mount("/", routes![super::root, super::login]); + let mut request = MockRequest::new(Get, format!($path)); + + $test_fn(request.dispatch_with(&rocket)); + }) +} + +#[test] +fn test_root() { + run_test!("/", |mut response: Response| { + assert!(response.body().is_none()); + assert_eq!(response.status(), Status::SeeOther); + for h in response.headers() { + match h.name.as_str() { + "Location" => assert_eq!(h.value, "/login"), + "Content-Length" => assert_eq!(h.value.parse::().unwrap(), 0), + _ => { /* let these through */ } + } + } + }); +} + +#[test] +fn test_login() { + run_test!("/login", |mut response: Response| { + let body_string = response.body().and_then(|body| body.into_string()); + assert_eq!(body_string, Some("Hi! Please log in before continuing.".to_string())); + assert_eq!(response.status(), Status::Ok); + }); +}