Skip to content

Commit

Permalink
Add tests for redirect example.
Browse files Browse the repository at this point in the history
  • Loading branch information
chazelton331 authored and SergioBenitez committed Jan 10, 2017
1 parent a6084ab commit 83e33cf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/redirect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ workspace = "../../"
[dependencies]
rocket = { path = "../../lib" }
rocket_codegen = { path = "../../codegen" }

[dev-dependencies]
rocket = { path = "../../lib", features = ["testing"] }
3 changes: 3 additions & 0 deletions examples/redirect/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#![feature(plugin)]
#![plugin(rocket_codegen)]

extern crate rocket;

#[cfg(test)] mod tests;

use rocket::response::Redirect;

#[get("/")]
Expand Down
38 changes: 38 additions & 0 deletions examples/redirect/src/tests.rs
Original file line number Diff line number Diff line change
@@ -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::<i32>().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);
});
}

0 comments on commit 83e33cf

Please sign in to comment.