From 368e5105a93341c84e3b1620d21f9c0e06e97a2e Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Thu, 15 Dec 2016 20:53:54 -0800 Subject: [PATCH] Return a Response from testing's dispatch_with. --- README.md | 2 +- examples/forms/src/tests.rs | 43 ++++++++++++++++++------------ examples/from_request/src/main.rs | 8 +++--- examples/hello_person/src/tests.rs | 14 +++++----- examples/hello_ranks/src/tests.rs | 7 +++-- examples/hello_world/src/tests.rs | 7 +++-- examples/testing/src/main.rs | 7 +++-- lib/src/testing.rs | 33 ++++++++++++----------- 8 files changed, 72 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index f2843825..650b4e7a 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,7 @@ world!" benchmark: * Rocket throughput higher by 3.7% (higher is better) * Rocket latency lower by 4.0% (lower is better) -## Future Improvements +### Future Improvements Rocket is currently built on a synchronous HTTP backend. Once the Rust asynchronous I/O libraries have stabilized, a migration to a new, more diff --git a/examples/forms/src/tests.rs b/examples/forms/src/tests.rs index 2bca331a..fdce6b74 100644 --- a/examples/forms/src/tests.rs +++ b/examples/forms/src/tests.rs @@ -1,36 +1,45 @@ use super::rocket; use rocket::testing::MockRequest; use rocket::http::Method::*; -use rocket::http::ContentType; +use rocket::http::{ContentType, Status}; -fn test_login bool>(username: &str, password: &str, age: isize, test: F) { +fn test_login(username: &str, password: &str, age: isize, status: Status, + body: Option<&'static str>) { let rocket = rocket::ignite().mount("/", routes![super::user_page, super::login]); - let result = MockRequest::new(Post, "/login") + let mut req = MockRequest::new(Post, "/login") .header(ContentType::Form) - .body(&format!("username={}&password={}&age={}", username, password, age)) - .dispatch_with(&rocket) - .unwrap_or("".to_string()); - assert!(test(result)); + .body(&format!("username={}&password={}&age={}", username, password, age)); + + let mut response = req.dispatch_with(&rocket); + let body_str = response.body().and_then(|body| body.to_string()); + + println!("Checking: {:?}/{:?}", username, password); + assert_eq!(response.status(), status); + + if let Some(string) = body { + assert!(body_str.map_or(true, |s| s.contains(string))); + } } #[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_login("Sergio", "password", 30, Status::SeeOther, None); } +const OK: Status = self::Status::Ok; + #[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_login("Sergio", "password", 20, OK, Some("Sorry, 20 is too young!")); + test_login("Sergio", "password", 200, OK, Some("Are you sure you're 200?")); + test_login("Sergio", "jk", -100, OK, Some("'-100' is not a valid integer.")); + test_login("Sergio", "ok", 30, OK, Some("Wrong password!")); + test_login("Mike", "password", 30, OK, Some("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()); + // Mess with the form formatting. + test_login("Sergio&other=blah&", "password", 0, Status::BadRequest, None); + test_login("&&&===&", "password", 0, Status::BadRequest, None); } diff --git a/examples/from_request/src/main.rs b/examples/from_request/src/main.rs index 75c142c2..7a0b26ef 100644 --- a/examples/from_request/src/main.rs +++ b/examples/from_request/src/main.rs @@ -46,11 +46,11 @@ mod test { req = req.header(header); } - // FIXME: Should be able to count headers directly! let rocket = rocket::ignite().mount("/", routes![super::header_count]); - let result = req.dispatch_with(&rocket); - assert_eq!(result.unwrap(), - format!("Your request contained {} headers!", num_headers)); + let mut response = req.dispatch_with(&rocket); + + let expect = format!("Your request contained {} headers!", num_headers); + assert_eq!(response.body().and_then(|b| b.to_string()), Some(expect)); } #[test] diff --git a/examples/hello_person/src/tests.rs b/examples/hello_person/src/tests.rs index 5809b6f5..6457443c 100644 --- a/examples/hello_person/src/tests.rs +++ b/examples/hello_person/src/tests.rs @@ -1,19 +1,21 @@ use super::rocket; use rocket::testing::MockRequest; use rocket::http::Method::*; + use rocket::http::Status; fn test(uri: &str, expected: String) { let rocket = rocket::ignite().mount("/", routes![super::hello, super::hi]); - let result = MockRequest::new(Get, uri).dispatch_with(&rocket); - assert_eq!(result.unwrap(), expected); + let mut req = MockRequest::new(Get, uri); + let mut response = req.dispatch_with(&rocket); + + assert_eq!(response.body().and_then(|b| b.to_string()), Some(expected)); } fn test_404(uri: &str) { let rocket = rocket::ignite().mount("/", routes![super::hello, super::hi]); - let result = MockRequest::new(Get, uri).dispatch_with(&rocket); - // FIXME: Be able to check that actual HTTP response status code. - // assert!(result.unwrap().contains("404")); - assert!(result.is_none()); + let mut req = MockRequest::new(Get, uri); + let response = req.dispatch_with(&rocket); + assert_eq!(response.status(), Status::NotFound); } #[test] diff --git a/examples/hello_ranks/src/tests.rs b/examples/hello_ranks/src/tests.rs index 3ad27fc2..b4ab2d6c 100644 --- a/examples/hello_ranks/src/tests.rs +++ b/examples/hello_ranks/src/tests.rs @@ -4,8 +4,11 @@ use rocket::http::Method::*; fn test(uri: &str, expected: String) { let rocket = rocket::ignite().mount("/", routes![super::hello, super::hi]); - let result = MockRequest::new(Get, uri).dispatch_with(&rocket); - assert_eq!(result.unwrap(), expected); + let mut req = MockRequest::new(Get, uri); + + let mut response = req.dispatch_with(&rocket); + let body_str = response.body().and_then(|body| body.to_string()); + assert_eq!(body_str, Some(expected)); } #[test] diff --git a/examples/hello_world/src/tests.rs b/examples/hello_world/src/tests.rs index 36ce61fa..25067c69 100644 --- a/examples/hello_world/src/tests.rs +++ b/examples/hello_world/src/tests.rs @@ -5,6 +5,9 @@ use rocket::http::Method::*; #[test] fn hello_world() { let rocket = rocket::ignite().mount("/", routes![super::hello]); - let result = MockRequest::new(Get, "/").dispatch_with(&rocket); - assert_eq!(result.unwrap().as_str(), "Hello, world!"); + let mut req = MockRequest::new(Get, "/"); + let mut response = req.dispatch_with(&rocket); + + let body_str = response.body().and_then(|body| body.to_string()); + assert_eq!(body_str, Some("Hello, world!".to_string())); } diff --git a/examples/testing/src/main.rs b/examples/testing/src/main.rs index 375f7dec..32ba073f 100644 --- a/examples/testing/src/main.rs +++ b/examples/testing/src/main.rs @@ -21,7 +21,10 @@ mod test { #[test] fn test_hello() { let rocket = rocket::ignite().mount("/", routes![super::hello]); - let result = MockRequest::new(Get, "/").dispatch_with(&rocket); - assert_eq!(result.unwrap().as_str(), "Hello, world!"); + let mut req = MockRequest::new(Get, "/"); + let mut response = req.dispatch_with(&rocket); + + let body_string = response.body().and_then(|b| b.to_string()); + assert_eq!(body_string, Some("Hello, world!".to_string())); } } diff --git a/lib/src/testing.rs b/lib/src/testing.rs index bbb24f7d..25fe335d 100644 --- a/lib/src/testing.rs +++ b/lib/src/testing.rs @@ -93,15 +93,20 @@ //! #[test] //! fn test_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!"); +//! let mut req = MockRequest::new(Get, "/"); +//! let mut response = req.dispatch_with(&rocket); +//! +//! // Write the body out as a string. +//! let body_str = response.body().unwrap().to_string().unwrap(); +//! +//! // Check that the body contains what we expect. +//! assert_eq!(body_str, "Hello, world!".to_string()); //! } //! } //! ``` use http::{Method, Header, Cookie}; -use ::{Rocket, Request, Data}; +use ::{Rocket, Request, Response, Data}; /// A type for mocking requests for testing Rocket applications. pub struct MockRequest { @@ -207,20 +212,18 @@ impl MockRequest { /// /// # fn main() { /// let rocket = rocket::ignite().mount("/", routes![hello]); - /// let result = MockRequest::new(Get, "/").dispatch_with(&rocket); - /// assert_eq!(&result.unwrap(), "Hello, world!"); + /// let mut req = MockRequest::new(Get, "/"); + /// let mut response = req.dispatch_with(&rocket); + /// + /// let body_str = response.body().unwrap().to_string().unwrap(); + /// assert_eq!(body_str, "Hello, world!".to_string()); /// # } /// ``` - /// FIXME: Can now return Response to get all info! - pub fn dispatch_with(&mut self, rocket: &Rocket) -> Option { + pub fn dispatch_with<'r>(&'r mut self, rocket: &Rocket) -> Response<'r> { let data = ::std::mem::replace(&mut self.data, Data::new(vec![])); - - let mut response = match rocket.dispatch(&self.request, data) { + match rocket.dispatch(&self.request, data) { Ok(response) => response, - // FIXME: Send to catcher? Not sure what user would want. - Err(_status) => return None - }; - - response.body().and_then(|body| body.to_string()) + Err(status) => rocket.handle_error(status, &self.request) + } } }