Add tests for optional_redirect example.

This commit is contained in:
Seth Lopez 2016-12-27 18:46:50 -05:00 committed by Sergio Benitez
parent 21ddb7390f
commit 9580d6cdfd
3 changed files with 45 additions and 1 deletions

View File

@ -7,3 +7,6 @@ workspace = "../../"
[dependencies]
rocket = { path = "../../lib" }
rocket_codegen = { path = "../../codegen" }
[dev-dependencies]
rocket = { path = "../../lib", features = ["testing"] }

View File

@ -2,6 +2,9 @@
#![plugin(rocket_codegen)]
extern crate rocket;
#[cfg(test)]
mod tests;
use rocket::response::Redirect;
#[get("/")]
@ -13,7 +16,7 @@ fn root() -> Redirect {
fn user(name: &str) -> Result<&'static str, Redirect> {
match name {
"Sergio" => Ok("Hello, Sergio!"),
_ => Err(Redirect::to("/users/login"))
_ => Err(Redirect::to("/users/login")),
}
}

View File

@ -0,0 +1,38 @@
use super::rocket;
use rocket::testing::MockRequest;
use rocket::http::{Method, Status};
fn test_200(uri: &str, expected_body: &str) {
let rocket = rocket::ignite()
.mount("/", routes![super::root, super::user, super::login]);
let mut request = MockRequest::new(Method::Get, uri);
let mut response = request.dispatch_with(&rocket);
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.body().and_then(|b| b.into_string()),
Some(expected_body.to_string()));
}
fn test_303(uri: &str, expected_location: &str) {
let rocket = rocket::ignite()
.mount("/", routes![super::root, super::user, super::login]);
let mut request = MockRequest::new(Method::Get, uri);
let response = request.dispatch_with(&rocket);
let location_headers: Vec<_> = response.header_values("Location").collect();
assert_eq!(response.status(), Status::SeeOther);
assert_eq!(location_headers, vec![expected_location]);
}
#[test]
fn test() {
test_200("/users/Sergio", "Hello, Sergio!");
test_200("/users/login",
"Hi! That user doesn't exist. Maybe you need to log in?");
}
#[test]
fn test_redirects() {
test_303("/", "/users/login");
test_303("/users/unknown", "/users/login");
}