From a145a11848d8a3f33e429df3b9658ba0626c9ee9 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Mon, 17 Oct 2016 15:14:57 -0700 Subject: [PATCH] Add tests to the hello_world, helllo_ranks, and hello_person examples. --- examples/forms/Cargo.toml | 3 +++ examples/forms/src/main.rs | 1 + examples/forms/src/tests.rs | 33 ++++++++++++++++++++++++ examples/hello_person/Cargo.toml | 3 +++ examples/hello_person/src/main.rs | 2 ++ examples/hello_person/src/tests.rs | 40 ++++++++++++++++++++++++++++++ examples/hello_ranks/Cargo.toml | 3 +++ examples/hello_ranks/src/main.rs | 2 ++ examples/hello_ranks/src/tests.rs | 33 ++++++++++++++++++++++++ examples/hello_world/Cargo.toml | 3 +++ examples/hello_world/src/main.rs | 2 ++ examples/hello_world/src/tests.rs | 11 ++++++++ lib/src/testing.rs | 4 +-- 13 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 examples/forms/src/tests.rs create mode 100644 examples/hello_person/src/tests.rs create mode 100644 examples/hello_ranks/src/tests.rs create mode 100644 examples/hello_world/src/tests.rs diff --git a/examples/forms/Cargo.toml b/examples/forms/Cargo.toml index 81e47d5a..df7f1d99 100644 --- a/examples/forms/Cargo.toml +++ b/examples/forms/Cargo.toml @@ -7,3 +7,6 @@ workspace = "../../" [dependencies] rocket = { path = "../../lib" } rocket_codegen = { path = "../../codegen" } + +[dev-dependencies] +rocket = { path = "../../lib", features = ["testing"] } diff --git a/examples/forms/src/main.rs b/examples/forms/src/main.rs index fbe2519d..cdb8b989 100644 --- a/examples/forms/src/main.rs +++ b/examples/forms/src/main.rs @@ -4,6 +4,7 @@ extern crate rocket; mod files; +#[cfg(test)] mod tests; use rocket::request::Form; use rocket::response::Redirect; diff --git a/examples/forms/src/tests.rs b/examples/forms/src/tests.rs new file mode 100644 index 00000000..1db94183 --- /dev/null +++ b/examples/forms/src/tests.rs @@ -0,0 +1,33 @@ +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")); +} diff --git a/examples/hello_person/Cargo.toml b/examples/hello_person/Cargo.toml index 0340590a..3bd31abb 100644 --- a/examples/hello_person/Cargo.toml +++ b/examples/hello_person/Cargo.toml @@ -7,3 +7,6 @@ workspace = "../../" [dependencies] rocket = { path = "../../lib" } rocket_codegen = { path = "../../codegen" } + +[dev-dependencies] +rocket = { path = "../../lib", features = ["testing"] } diff --git a/examples/hello_person/src/main.rs b/examples/hello_person/src/main.rs index 0756b107..43f08fc7 100644 --- a/examples/hello_person/src/main.rs +++ b/examples/hello_person/src/main.rs @@ -3,6 +3,8 @@ extern crate rocket; +#[cfg(test)] mod tests; + #[get("/hello//")] fn hello(name: &str, age: i8) -> String { format!("Hello, {} year old named {}!", age, name) diff --git a/examples/hello_person/src/tests.rs b/examples/hello_person/src/tests.rs new file mode 100644 index 00000000..a4ab3033 --- /dev/null +++ b/examples/hello_person/src/tests.rs @@ -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()); + } +} diff --git a/examples/hello_ranks/Cargo.toml b/examples/hello_ranks/Cargo.toml index d8031af9..2408b888 100644 --- a/examples/hello_ranks/Cargo.toml +++ b/examples/hello_ranks/Cargo.toml @@ -7,3 +7,6 @@ workspace = "../../" [dependencies] rocket = { path = "../../lib" } rocket_codegen = { path = "../../codegen" } + +[dev-dependencies] +rocket = { path = "../../lib", features = ["testing"] } diff --git a/examples/hello_ranks/src/main.rs b/examples/hello_ranks/src/main.rs index d542e576..f8d12abd 100644 --- a/examples/hello_ranks/src/main.rs +++ b/examples/hello_ranks/src/main.rs @@ -3,6 +3,8 @@ extern crate rocket; +#[cfg(test)] mod tests; + #[get("/hello//")] fn hello(name: &str, age: i8) -> String { format!("Hello, {} year old named {}!", age, name) diff --git a/examples/hello_ranks/src/tests.rs b/examples/hello_ranks/src/tests.rs new file mode 100644 index 00000000..b419dcdb --- /dev/null +++ b/examples/hello_ranks/src/tests.rs @@ -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)); + } +} diff --git a/examples/hello_world/Cargo.toml b/examples/hello_world/Cargo.toml index 3b0f1e0a..4e366e92 100644 --- a/examples/hello_world/Cargo.toml +++ b/examples/hello_world/Cargo.toml @@ -7,3 +7,6 @@ workspace = "../../" [dependencies] rocket = { path = "../../lib" } rocket_codegen = { path = "../../codegen" } + +[dev-dependencies] +rocket = { path = "../../lib", features = ["testing"] } diff --git a/examples/hello_world/src/main.rs b/examples/hello_world/src/main.rs index 3f4d2061..524574ad 100644 --- a/examples/hello_world/src/main.rs +++ b/examples/hello_world/src/main.rs @@ -3,6 +3,8 @@ extern crate rocket; +#[cfg(test)] mod tests; + #[get("/")] fn hello() -> &'static str { "Hello, world!" diff --git a/examples/hello_world/src/tests.rs b/examples/hello_world/src/tests.rs new file mode 100644 index 00000000..b142f800 --- /dev/null +++ b/examples/hello_world/src/tests.rs @@ -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!"); +} diff --git a/lib/src/testing.rs b/lib/src/testing.rs index 10093850..2b1abfbf 100644 --- a/lib/src/testing.rs +++ b/lib/src/testing.rs @@ -67,9 +67,7 @@ impl MockRequest { match String::from_utf8(response.into_inner()) { Ok(string) => { // TODO: Expose the full response (with headers) somewhow. - string.find("\r\n\r\n").map(|i| { - string[(i + 4)..].to_string() - }) + string.find("\r\n\r\n").map(|i| string[(i + 4)..].to_string()) } Err(e) => { error_!("Could not create string from response: {:?}", e);