use super::rocket; use rocket::testing::MockRequest; use rocket::http::Method::*; fn test_login bool>(username: &str, password: &str, age: isize, test: F) { let rocket = rocket::ignite().mount("/", routes![super::user_page, super::login]); let req = MockRequest::new(Post, "/login") .headers(&[("Content-Type", "application/x-www-form-urlencoded")]) .body(&format!("username={}&password={}&age={}", username, password, age)); let result = req.dispatch_with(&rocket); let result = result.unwrap(); assert!(test(result)); } #[test] fn test_good_login() { // TODO: Be able to check if it's a redirect, and process the redirect. test_login("Sergio", "password", 30, |s| s.is_empty()); } #[test] fn test_bad_login() { test_login("Sergio", "password", 20, |s| s == "Sorry, 20 is too young!"); test_login("Sergio", "password", 200, |s| s == "Are you sure you're 200?"); test_login("Sergio", "password", -100, |s| s == "'-100' is not a valid integer."); test_login("Sergio", "ok", 30, |s| s == "Wrong password!"); test_login("Mike", "password", 30, |s| s == "Unrecognized user, 'Mike'."); } #[test] fn test_bad_form() { test_login("Sergio&other=blah&", "password", 30, |s| s.contains("400 Bad Request")); }