Add tests to the hello_world, helllo_ranks, and hello_person examples.

This commit is contained in:
Sergio Benitez 2016-10-17 15:14:57 -07:00
parent 33f0274f62
commit a145a11848
13 changed files with 137 additions and 3 deletions

View File

@ -7,3 +7,6 @@ workspace = "../../"
[dependencies] [dependencies]
rocket = { path = "../../lib" } rocket = { path = "../../lib" }
rocket_codegen = { path = "../../codegen" } rocket_codegen = { path = "../../codegen" }
[dev-dependencies]
rocket = { path = "../../lib", features = ["testing"] }

View File

@ -4,6 +4,7 @@
extern crate rocket; extern crate rocket;
mod files; mod files;
#[cfg(test)] mod tests;
use rocket::request::Form; use rocket::request::Form;
use rocket::response::Redirect; use rocket::response::Redirect;

View File

@ -0,0 +1,33 @@
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 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"));
}

View File

@ -7,3 +7,6 @@ workspace = "../../"
[dependencies] [dependencies]
rocket = { path = "../../lib" } rocket = { path = "../../lib" }
rocket_codegen = { path = "../../codegen" } rocket_codegen = { path = "../../codegen" }
[dev-dependencies]
rocket = { path = "../../lib", features = ["testing"] }

View File

@ -3,6 +3,8 @@
extern crate rocket; extern crate rocket;
#[cfg(test)] mod tests;
#[get("/hello/<name>/<age>")] #[get("/hello/<name>/<age>")]
fn hello(name: &str, age: i8) -> String { fn hello(name: &str, age: i8) -> String {
format!("Hello, {} year old named {}!", age, name) format!("Hello, {} year old named {}!", age, name)

View File

@ -0,0 +1,40 @@
use super::rocket;
use rocket::testing::MockRequest;
use rocket::http::Method::*;
fn test(uri: &str, expected: String) {
let rocket = rocket::ignite().mount("/", routes![super::hello, super::hi]);
let req = MockRequest::new(Get, uri);
let result = req.dispatch_with(&rocket);
assert_eq!(result.unwrap(), expected);
}
fn test_404(uri: &str) {
let rocket = rocket::ignite().mount("/", routes![super::hello, super::hi]);
let req = MockRequest::new(Get, uri);
let result = req.dispatch_with(&rocket);
// TODO: Be able to check that actual HTTP response status code.
assert!(result.unwrap().contains("404 Not Found"));
}
#[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/128");
test_404("/hello/Mike/-129");
}
#[test]
fn test_hi() {
for name in &["Mike", "A", "123", "hi", "c"] {
test(&format!("/hello/{}", name), name.to_string());
}
}

View File

@ -7,3 +7,6 @@ workspace = "../../"
[dependencies] [dependencies]
rocket = { path = "../../lib" } rocket = { path = "../../lib" }
rocket_codegen = { path = "../../codegen" } rocket_codegen = { path = "../../codegen" }
[dev-dependencies]
rocket = { path = "../../lib", features = ["testing"] }

View File

@ -3,6 +3,8 @@
extern crate rocket; extern crate rocket;
#[cfg(test)] mod tests;
#[get("/hello/<name>/<age>")] #[get("/hello/<name>/<age>")]
fn hello(name: &str, age: i8) -> String { fn hello(name: &str, age: i8) -> String {
format!("Hello, {} year old named {}!", age, name) format!("Hello, {} year old named {}!", age, name)

View File

@ -0,0 +1,33 @@
use super::rocket;
use rocket::testing::MockRequest;
use rocket::http::Method::*;
fn test(uri: &str, expected: String) {
let rocket = rocket::ignite().mount("/", routes![super::hello, super::hi]);
let req = MockRequest::new(Get, uri);
let result = req.dispatch_with(&rocket);
assert_eq!(result.unwrap(), expected);
}
#[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_hi() {
// Invalid integers.
for &(name, age) in &[("Mike", 1000), ("Michael", 128), ("A", -800), ("a", -200)] {
test(&format!("/hello/{}/{}", name, age),
format!("Hi {}! Your age ({}) is kind of funky.", name, age));
}
// Non-integers.
for &(name, age) in &[("Mike", "!"), ("Michael", "hi"), ("A", "blah"), ("a", "0-1")] {
test(&format!("/hello/{}/{}", name, age),
format!("Hi {}! Your age ({}) is kind of funky.", name, age));
}
}

View File

@ -7,3 +7,6 @@ workspace = "../../"
[dependencies] [dependencies]
rocket = { path = "../../lib" } rocket = { path = "../../lib" }
rocket_codegen = { path = "../../codegen" } rocket_codegen = { path = "../../codegen" }
[dev-dependencies]
rocket = { path = "../../lib", features = ["testing"] }

View File

@ -3,6 +3,8 @@
extern crate rocket; extern crate rocket;
#[cfg(test)] mod tests;
#[get("/")] #[get("/")]
fn hello() -> &'static str { fn hello() -> &'static str {
"Hello, world!" "Hello, world!"

View File

@ -0,0 +1,11 @@
use super::rocket;
use rocket::testing::MockRequest;
use rocket::http::Method::*;
#[test]
fn hello_world() {
let rocket = rocket::ignite().mount("/", routes![super::hello]);
let req = MockRequest::new(Get, "/");
let result = req.dispatch_with(&rocket);
assert_eq!(result.unwrap().as_str(), "Hello, world!");
}

View File

@ -67,9 +67,7 @@ impl MockRequest {
match String::from_utf8(response.into_inner()) { match String::from_utf8(response.into_inner()) {
Ok(string) => { Ok(string) => {
// TODO: Expose the full response (with headers) somewhow. // TODO: Expose the full response (with headers) somewhow.
string.find("\r\n\r\n").map(|i| { string.find("\r\n\r\n").map(|i| string[(i + 4)..].to_string())
string[(i + 4)..].to_string()
})
} }
Err(e) => { Err(e) => {
error_!("Could not create string from response: {:?}", e); error_!("Could not create string from response: {:?}", e);