2016-12-29 03:29:12 +00:00
|
|
|
use super::rocket;
|
2017-06-06 20:41:04 +00:00
|
|
|
use rocket::local::{Client, LocalResponse as Response};
|
2016-12-29 03:29:12 +00:00
|
|
|
use rocket::http::Status;
|
|
|
|
|
|
|
|
macro_rules! run_test {
|
|
|
|
($query:expr, $test_fn:expr) => ({
|
2018-09-20 04:14:30 +00:00
|
|
|
let client = Client::new(rocket()).unwrap();
|
2017-06-06 20:41:04 +00:00
|
|
|
$test_fn(client.get(format!("/hello{}", $query)).dispatch());
|
2016-12-29 03:29:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn age_and_name_params() {
|
2017-04-14 08:59:28 +00:00
|
|
|
run_test!("?age=10&name=john", |mut response: Response| {
|
|
|
|
assert_eq!(response.body_string(),
|
2018-09-20 04:14:30 +00:00
|
|
|
Some("Hello, 10 year old named john!".into()));
|
|
|
|
});
|
|
|
|
|
|
|
|
run_test!("?age=20&name=john", |mut response: Response| {
|
|
|
|
assert_eq!(response.body_string(),
|
|
|
|
Some("20 years old? Hi, john!".into()));
|
2017-04-14 08:59:28 +00:00
|
|
|
});
|
2016-12-29 03:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn age_param_only() {
|
2018-09-20 04:14:30 +00:00
|
|
|
run_test!("?age=10", |mut response: Response| {
|
|
|
|
assert_eq!(response.body_string(),
|
|
|
|
Some("We're gonna need a name, and only a name.".into()));
|
|
|
|
});
|
|
|
|
|
|
|
|
run_test!("?age=20", |mut response: Response| {
|
|
|
|
assert_eq!(response.body_string(),
|
|
|
|
Some("We're gonna need a name, and only a name.".into()));
|
2017-04-14 08:59:28 +00:00
|
|
|
});
|
2016-12-29 03:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn name_param_only() {
|
2017-04-14 08:59:28 +00:00
|
|
|
run_test!("?name=John", |mut response: Response| {
|
|
|
|
assert_eq!(response.body_string(), Some("Hello John!".into()));
|
|
|
|
});
|
2016-12-29 03:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_params() {
|
2018-09-20 04:14:30 +00:00
|
|
|
run_test!("", |mut response: Response| {
|
|
|
|
assert_eq!(response.body_string(),
|
|
|
|
Some("We're gonna need a name, and only a name.".into()));
|
2017-04-14 08:59:28 +00:00
|
|
|
});
|
2016-12-29 03:29:12 +00:00
|
|
|
|
2018-09-20 04:14:30 +00:00
|
|
|
run_test!("?", |mut response: Response| {
|
|
|
|
assert_eq!(response.body_string(),
|
|
|
|
Some("We're gonna need a name, and only a name.".into()));
|
2017-04-14 08:59:28 +00:00
|
|
|
});
|
2016-12-29 03:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-09-20 04:14:30 +00:00
|
|
|
fn extra_params() {
|
|
|
|
run_test!("?age=20&name=Bob&extra", |mut response: Response| {
|
|
|
|
assert_eq!(response.body_string(),
|
|
|
|
Some("20 years old? Hi, Bob!".into()));
|
2017-04-14 08:59:28 +00:00
|
|
|
});
|
2016-12-29 03:29:12 +00:00
|
|
|
|
2018-09-20 04:14:30 +00:00
|
|
|
run_test!("?age=30&name=Bob&extra", |mut response: Response| {
|
|
|
|
assert_eq!(response.body_string(),
|
|
|
|
Some("We're gonna need a name, and only a name.".into()));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn wrong_path() {
|
|
|
|
run_test!("/other?age=20&name=Bob", |response: Response| {
|
2017-04-14 08:59:28 +00:00
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
});
|
2016-12-29 03:29:12 +00:00
|
|
|
}
|