Add tests for optional_result example.

This commit is contained in:
Seth Lopez 2016-12-27 19:37:54 -05:00 committed by Sergio Benitez
parent 55a2535896
commit ab94e344b4
3 changed files with 30 additions and 0 deletions

View File

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

View File

@ -3,6 +3,9 @@
extern crate rocket; extern crate rocket;
#[cfg(test)]
mod tests;
#[get("/users/<name>")] #[get("/users/<name>")]
fn user(name: &str) -> Option<&'static str> { fn user(name: &str) -> Option<&'static str> {
if name == "Sergio" { if name == "Sergio" {

View File

@ -0,0 +1,24 @@
use super::rocket;
use rocket::testing::MockRequest;
use rocket::http::{Method, Status};
#[test]
fn test_200() {
let rocket = rocket::ignite().mount("/", routes![super::user]);
let mut request = MockRequest::new(Method::Get, "/users/Sergio");
let mut response = request.dispatch_with(&rocket);
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.body().and_then(|b| b.into_string()),
Some("Hello, Sergio!".to_string()));
}
#[test]
fn test_404() {
let rocket = rocket::ignite().mount("/", routes![super::user]);
let mut request = MockRequest::new(Method::Get, "/users/unknown");
let response = request.dispatch_with(&rocket);
// Only test the status because the body is the default 404.
assert_eq!(response.status(), Status::NotFound);
}