2016-10-17 22:14:57 +00:00
|
|
|
use super::rocket;
|
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
|
|
|
|
2017-06-06 20:41:04 +00:00
|
|
|
fn client() -> Client {
|
|
|
|
Client::new(rocket::ignite().mount("/", routes![super::hello, super::hi])).unwrap()
|
|
|
|
}
|
|
|
|
|
2016-10-17 22:14:57 +00:00
|
|
|
fn test(uri: &str, expected: String) {
|
2017-06-06 20:41:04 +00:00
|
|
|
let client = client();
|
|
|
|
assert_eq!(client.get(uri).dispatch().body_string(), Some(expected));
|
2016-10-17 22:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_404(uri: &str) {
|
2017-06-06 20:41:04 +00:00
|
|
|
let client = client();
|
|
|
|
assert_eq!(client.get(uri).dispatch().status(), Status::NotFound);
|
2016-10-17 22:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hello() {
|
|
|
|
for &(name, age) in &[("Mike", 22), ("Michael", 80), ("A", 0), ("a", 127)] {
|
|
|
|
test(&format!("/hello/{}/{}", name, age),
|
|
|
|
format!("Hello, {} year old named {}!", age, name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_failing_hello() {
|
|
|
|
test_404("/hello/Mike/1000");
|
|
|
|
test_404("/hello/Mike/-129");
|
2016-12-22 06:56:58 +00:00
|
|
|
test_404("/hello/Mike/-1");
|
2016-10-17 22:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hi() {
|
|
|
|
for name in &["Mike", "A", "123", "hi", "c"] {
|
|
|
|
test(&format!("/hello/{}", name), name.to_string());
|
|
|
|
}
|
|
|
|
}
|