mirror of https://github.com/rwf2/Rocket.git
Return a Response from testing's dispatch_with.
This commit is contained in:
parent
77cfed0d21
commit
368e5105a9
|
@ -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
|
||||
|
|
|
@ -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<F: Fn(String) -> 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);
|
||||
}
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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()));
|
||||
}
|
||||
|
|
|
@ -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()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue