Add tests for the errors example

This commit is contained in:
Reilly Tucker Siemens 2017-01-01 22:55:08 -08:00
parent 2852c526c3
commit 5c12bc988c
No known key found for this signature in database
GPG Key ID: 1B458E7C7CB154A9
3 changed files with 38 additions and 0 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

@ -3,6 +3,9 @@
extern crate rocket;
#[cfg(test)]
mod tests;
#[get("/hello/<name>/<age>")]
fn hello(name: &str, age: i8) -> String {
format!("Hello, {} year old named {}!", age, name)

View File

@ -0,0 +1,32 @@
use super::rocket;
use rocket::testing::MockRequest;
use rocket::http::{Method, Status};
fn test(uri: &str, status: Status, body: String) {
let rocket = rocket::ignite()
.mount("/", routes![super::hello])
.catch(errors![super::not_found]);
let mut req = MockRequest::new(Method::Get, uri);
let mut response = req.dispatch_with(&rocket);
assert_eq!(response.status(), status);
assert_eq!(response.body().and_then(|b| b.into_string()), Some(body));
}
#[test]
fn test_hello() {
let (name, age) = ("Arthur", 42);
let uri = format!("/hello/{}/{}", name, age);
test(&uri, Status::Ok, format!("Hello, {} year old named {}!", age, name));
}
#[test]
fn test_hello_invalid_age() {
for &(name, age) in &[("Ford", -129), ("Trillian", 128)] {
let uri = format!("/hello/{}/{}", name, age);
let body = format!("<p>Sorry, but '{}' is not a valid path!</p>
<p>Try visiting /hello/&lt;name&gt;/&lt;age&gt; instead.</p>",
uri);
test(&uri, Status::NotFound, body);
}
}