2017-06-06 20:41:04 +00:00
|
|
|
use rocket::local::Client;
|
|
|
|
use rocket::http::Status;
|
2017-01-02 06:55:08 +00:00
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
async fn test(uri: &str, status: Status, body: String) {
|
2017-01-02 06:55:08 +00:00
|
|
|
let rocket = rocket::ignite()
|
|
|
|
.mount("/", routes![super::hello])
|
2018-09-16 08:47:51 +00:00
|
|
|
.register(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();
|
2019-08-24 17:27:10 +00:00
|
|
|
let mut response = client.get(uri).dispatch().await;
|
2017-01-02 06:55:08 +00:00
|
|
|
assert_eq!(response.status(), status);
|
2019-08-24 17:27:10 +00:00
|
|
|
assert_eq!(response.body_string().await, Some(body));
|
2017-01-02 06:55:08 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn test_hello() {
|
2017-01-02 06:55:08 +00:00
|
|
|
let (name, age) = ("Arthur", 42);
|
|
|
|
let uri = format!("/hello/{}/{}", name, age);
|
2019-08-24 17:27:10 +00:00
|
|
|
test(&uri, Status::Ok, format!("Hello, {} year old named {}!", age, name)).await;
|
2017-01-02 06:55:08 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn test_hello_invalid_age() {
|
2017-01-02 06:55:08 +00:00
|
|
|
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);
|
2019-08-24 17:27:10 +00:00
|
|
|
test(&uri, Status::NotFound, body).await;
|
2017-01-02 06:55:08 +00:00
|
|
|
}
|
|
|
|
}
|