2017-06-06 20:41:04 +00:00
|
|
|
use rocket::local::Client;
|
2016-12-22 06:56:58 +00:00
|
|
|
use rocket::http::Status;
|
2016-10-17 22:14:57 +00:00
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
async fn test(uri: String, expected: String) {
|
2020-06-16 12:01:26 +00:00
|
|
|
let client = Client::new(super::rocket()).await.unwrap();
|
2019-08-24 17:27:10 +00:00
|
|
|
assert_eq!(client.get(&uri).dispatch().await.body_string().await, Some(expected));
|
2016-10-17 22:14:57 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
async fn test_404(uri: &'static str) {
|
2020-06-16 12:01:26 +00:00
|
|
|
let client = Client::new(super::rocket()).await.unwrap();
|
2019-08-24 17:27:10 +00:00
|
|
|
assert_eq!(client.get(uri).dispatch().await.status(), Status::NotFound);
|
2016-10-17 22:14:57 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn test_hello() {
|
2016-10-17 22:14:57 +00:00
|
|
|
for &(name, age) in &[("Mike", 22), ("Michael", 80), ("A", 0), ("a", 127)] {
|
2019-11-26 16:25:56 +00:00
|
|
|
test(format!("/hello/{}/{}", name, age),
|
|
|
|
format!("Hello, {} year old named {}!", age, name)).await;
|
2016-10-17 22:14:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn test_failing_hello() {
|
|
|
|
test_404("/hello/Mike/1000").await;
|
|
|
|
test_404("/hello/Mike/-129").await;
|
|
|
|
test_404("/hello/Mike/-1").await;
|
2016-10-17 22:14:57 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn test_hi() {
|
2016-10-17 22:14:57 +00:00
|
|
|
for name in &["Mike", "A", "123", "hi", "c"] {
|
2019-11-26 16:25:56 +00:00
|
|
|
test(format!("/hello/{}", name), name.to_string()).await;
|
2016-10-17 22:14:57 +00:00
|
|
|
}
|
|
|
|
}
|