2017-01-02 06:55:08 +00:00
|
|
|
use super::rocket;
|
2017-06-06 20:41:04 +00:00
|
|
|
use rocket::local::Client;
|
|
|
|
use rocket::http::Status;
|
2017-01-02 06:55:08 +00:00
|
|
|
|
|
|
|
fn test(uri: &str, status: Status, body: String) {
|
|
|
|
let rocket = rocket::ignite()
|
|
|
|
.mount("/", routes![super::hello])
|
2017-09-23 02:04:14 +00:00
|
|
|
.catch(catchers![super::not_found]);
|
2017-01-02 06:55:08 +00:00
|
|
|
|
2017-06-06 20:41:04 +00:00
|
|
|
let client = Client::new(rocket).unwrap();
|
|
|
|
let mut response = client.get(uri).dispatch();
|
2017-01-02 06:55:08 +00:00
|
|
|
assert_eq!(response.status(), status);
|
2017-04-14 08:59:28 +00:00
|
|
|
assert_eq!(response.body_string(), Some(body));
|
2017-01-02 06:55:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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/<name>/<age> instead.</p>",
|
|
|
|
uri);
|
|
|
|
test(&uri, Status::NotFound, body);
|
|
|
|
}
|
|
|
|
}
|