Rocket/examples/forms/src/tests.rs
Sergio Benitez 44f5f1998d New HTTP types: ContentType, Status. Responder/Handler/ErrorHandler changed.
This is a complete rework of `Responder`s and of the http backend in
general. This gets Rocket one step closer to HTTP library independence,
enabling many future features such as transparent async I/O, automatic
HEAD request parsing, pre/post hooks, and more.

Summary of changes:

  * `Responder::response` no longer takes in `FreshHyperResponse`.
    Instead, it returns a new `Response` type.
  * The new `Response` type now encapsulates a full HTTP response. As a
    result, `Responder`s now return it.
  * The `Handler` type now returns an `Outcome` directly.
  * The `ErrorHandler` returns a `Result`. It can no longer forward,
    which made no sense previously.
  * `Stream` accepts a chunked size parameter.
  * `StatusCode` removed in favor of new `Status` type.
  * `ContentType` significantly modified.
  * New, lightweight `Header` type that plays nicely with `Response`.
2016-12-15 00:47:31 -08:00

36 lines
1.4 KiB
Rust

use super::rocket;
use rocket::testing::MockRequest;
use rocket::http::Method::*;
fn test_login<F: Fn(String) -> bool>(username: &str, password: &str, age: isize, test: F) {
let rocket = rocket::ignite().mount("/", routes![super::user_page, super::login]);
let result = MockRequest::new(Post, "/login")
.headers(&[("Content-Type", "application/x-www-form-urlencoded")])
.body(&format!("username={}&password={}&age={}", username, password, age))
.dispatch_with(&rocket)
.unwrap_or("".to_string());
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() {
// FIXME: Need to be able to examine the status.
// test_login("Sergio&other=blah&", "password", 30, |s| s.contains("400 Bad Request"));
test_login("Sergio&other=blah&", "password", 30, |s| s.is_empty());
}