use rocket::local::asynchronous::Client; use rocket::http::Status; async fn test(uri: &str, status: Status, body: String) { let rocket = rocket::ignite() .mount("/", routes![super::hello]) .register(catchers![super::not_found]); let client = Client::new(rocket).await.unwrap(); let response = client.get(uri).dispatch().await; assert_eq!(response.status(), status); assert_eq!(response.into_string().await, Some(body)); } #[rocket::async_test] async 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)).await; } #[rocket::async_test] async fn test_hello_invalid_age() { for &(name, age) in &[("Ford", -129), ("Trillian", 128)] { let uri = format!("/hello/{}/{}", name, age); let body = format!("

Sorry, but '{}' is not a valid path!

Try visiting /hello/<name>/<age> instead.

", uri); test(&uri, Status::NotFound, body).await; } }