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 throughput higher by 3.7% (higher is better)
|
||||||
* Rocket latency lower by 4.0% (lower 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
|
Rocket is currently built on a synchronous HTTP backend. Once the Rust
|
||||||
asynchronous I/O libraries have stabilized, a migration to a new, more
|
asynchronous I/O libraries have stabilized, a migration to a new, more
|
||||||
|
|
|
@ -1,36 +1,45 @@
|
||||||
use super::rocket;
|
use super::rocket;
|
||||||
use rocket::testing::MockRequest;
|
use rocket::testing::MockRequest;
|
||||||
use rocket::http::Method::*;
|
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 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)
|
.header(ContentType::Form)
|
||||||
.body(&format!("username={}&password={}&age={}", username, password, age))
|
.body(&format!("username={}&password={}&age={}", username, password, age));
|
||||||
.dispatch_with(&rocket)
|
|
||||||
.unwrap_or("".to_string());
|
let mut response = req.dispatch_with(&rocket);
|
||||||
assert!(test(result));
|
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]
|
#[test]
|
||||||
fn test_good_login() {
|
fn test_good_login() {
|
||||||
// TODO: Be able to check if it's a redirect, and process the redirect.
|
test_login("Sergio", "password", 30, Status::SeeOther, None);
|
||||||
test_login("Sergio", "password", 30, |s| s.is_empty());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const OK: Status = self::Status::Ok;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bad_login() {
|
fn test_bad_login() {
|
||||||
test_login("Sergio", "password", 20, |s| s == "Sorry, 20 is too young!");
|
test_login("Sergio", "password", 20, OK, Some("Sorry, 20 is too young!"));
|
||||||
test_login("Sergio", "password", 200, |s| s == "Are you sure you're 200?");
|
test_login("Sergio", "password", 200, OK, Some("Are you sure you're 200?"));
|
||||||
test_login("Sergio", "password", -100, |s| s == "'-100' is not a valid integer.");
|
test_login("Sergio", "jk", -100, OK, Some("'-100' is not a valid integer."));
|
||||||
test_login("Sergio", "ok", 30, |s| s == "Wrong password!");
|
test_login("Sergio", "ok", 30, OK, Some("Wrong password!"));
|
||||||
test_login("Mike", "password", 30, |s| s == "Unrecognized user, 'Mike'.");
|
test_login("Mike", "password", 30, OK, Some("Unrecognized user, 'Mike'."));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bad_form() {
|
fn test_bad_form() {
|
||||||
// FIXME: Need to be able to examine the status.
|
// Mess with the form formatting.
|
||||||
// test_login("Sergio&other=blah&", "password", 30, |s| s.contains("400 Bad Request"));
|
test_login("Sergio&other=blah&", "password", 0, Status::BadRequest, None);
|
||||||
test_login("Sergio&other=blah&", "password", 30, |s| s.is_empty());
|
test_login("&&&===&", "password", 0, Status::BadRequest, None);
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,11 +46,11 @@ mod test {
|
||||||
req = req.header(header);
|
req = req.header(header);
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Should be able to count headers directly!
|
|
||||||
let rocket = rocket::ignite().mount("/", routes![super::header_count]);
|
let rocket = rocket::ignite().mount("/", routes![super::header_count]);
|
||||||
let result = req.dispatch_with(&rocket);
|
let mut response = req.dispatch_with(&rocket);
|
||||||
assert_eq!(result.unwrap(),
|
|
||||||
format!("Your request contained {} headers!", num_headers));
|
let expect = format!("Your request contained {} headers!", num_headers);
|
||||||
|
assert_eq!(response.body().and_then(|b| b.to_string()), Some(expect));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -1,19 +1,21 @@
|
||||||
use super::rocket;
|
use super::rocket;
|
||||||
use rocket::testing::MockRequest;
|
use rocket::testing::MockRequest;
|
||||||
use rocket::http::Method::*;
|
use rocket::http::Method::*;
|
||||||
|
use rocket::http::Status;
|
||||||
|
|
||||||
fn test(uri: &str, expected: String) {
|
fn test(uri: &str, expected: String) {
|
||||||
let rocket = rocket::ignite().mount("/", routes![super::hello, super::hi]);
|
let rocket = rocket::ignite().mount("/", routes![super::hello, super::hi]);
|
||||||
let result = MockRequest::new(Get, uri).dispatch_with(&rocket);
|
let mut req = MockRequest::new(Get, uri);
|
||||||
assert_eq!(result.unwrap(), expected);
|
let mut response = req.dispatch_with(&rocket);
|
||||||
|
|
||||||
|
assert_eq!(response.body().and_then(|b| b.to_string()), Some(expected));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_404(uri: &str) {
|
fn test_404(uri: &str) {
|
||||||
let rocket = rocket::ignite().mount("/", routes![super::hello, super::hi]);
|
let rocket = rocket::ignite().mount("/", routes![super::hello, super::hi]);
|
||||||
let result = MockRequest::new(Get, uri).dispatch_with(&rocket);
|
let mut req = MockRequest::new(Get, uri);
|
||||||
// FIXME: Be able to check that actual HTTP response status code.
|
let response = req.dispatch_with(&rocket);
|
||||||
// assert!(result.unwrap().contains("404"));
|
assert_eq!(response.status(), Status::NotFound);
|
||||||
assert!(result.is_none());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -4,8 +4,11 @@ use rocket::http::Method::*;
|
||||||
|
|
||||||
fn test(uri: &str, expected: String) {
|
fn test(uri: &str, expected: String) {
|
||||||
let rocket = rocket::ignite().mount("/", routes![super::hello, super::hi]);
|
let rocket = rocket::ignite().mount("/", routes![super::hello, super::hi]);
|
||||||
let result = MockRequest::new(Get, uri).dispatch_with(&rocket);
|
let mut req = MockRequest::new(Get, uri);
|
||||||
assert_eq!(result.unwrap(), expected);
|
|
||||||
|
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]
|
#[test]
|
||||||
|
|
|
@ -5,6 +5,9 @@ use rocket::http::Method::*;
|
||||||
#[test]
|
#[test]
|
||||||
fn hello_world() {
|
fn hello_world() {
|
||||||
let rocket = rocket::ignite().mount("/", routes![super::hello]);
|
let rocket = rocket::ignite().mount("/", routes![super::hello]);
|
||||||
let result = MockRequest::new(Get, "/").dispatch_with(&rocket);
|
let mut req = MockRequest::new(Get, "/");
|
||||||
assert_eq!(result.unwrap().as_str(), "Hello, world!");
|
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]
|
#[test]
|
||||||
fn test_hello() {
|
fn test_hello() {
|
||||||
let rocket = rocket::ignite().mount("/", routes![super::hello]);
|
let rocket = rocket::ignite().mount("/", routes![super::hello]);
|
||||||
let result = MockRequest::new(Get, "/").dispatch_with(&rocket);
|
let mut req = MockRequest::new(Get, "/");
|
||||||
assert_eq!(result.unwrap().as_str(), "Hello, world!");
|
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]
|
//! #[test]
|
||||||
//! fn test_hello_world() {
|
//! fn test_hello_world() {
|
||||||
//! let rocket = rocket::ignite().mount("/", routes![super::hello]);
|
//! let rocket = rocket::ignite().mount("/", routes![super::hello]);
|
||||||
//! let req = MockRequest::new(Get, "/");
|
//! let mut req = MockRequest::new(Get, "/");
|
||||||
//! let result = req.dispatch_with(&rocket);
|
//! let mut response = req.dispatch_with(&rocket);
|
||||||
//! assert_eq!(result.unwrap().as_str(), "Hello, world!");
|
//!
|
||||||
|
//! // 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 http::{Method, Header, Cookie};
|
||||||
use ::{Rocket, Request, Data};
|
use ::{Rocket, Request, Response, Data};
|
||||||
|
|
||||||
/// A type for mocking requests for testing Rocket applications.
|
/// A type for mocking requests for testing Rocket applications.
|
||||||
pub struct MockRequest {
|
pub struct MockRequest {
|
||||||
|
@ -207,20 +212,18 @@ impl MockRequest {
|
||||||
///
|
///
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// let rocket = rocket::ignite().mount("/", routes![hello]);
|
/// let rocket = rocket::ignite().mount("/", routes![hello]);
|
||||||
/// let result = MockRequest::new(Get, "/").dispatch_with(&rocket);
|
/// let mut req = MockRequest::new(Get, "/");
|
||||||
/// assert_eq!(&result.unwrap(), "Hello, world!");
|
/// 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<'r>(&'r mut self, rocket: &Rocket) -> Response<'r> {
|
||||||
pub fn dispatch_with(&mut self, rocket: &Rocket) -> Option<String> {
|
|
||||||
let data = ::std::mem::replace(&mut self.data, Data::new(vec![]));
|
let data = ::std::mem::replace(&mut self.data, Data::new(vec![]));
|
||||||
|
match rocket.dispatch(&self.request, data) {
|
||||||
let mut response = match rocket.dispatch(&self.request, data) {
|
|
||||||
Ok(response) => response,
|
Ok(response) => response,
|
||||||
// FIXME: Send to catcher? Not sure what user would want.
|
Err(status) => rocket.handle_error(status, &self.request)
|
||||||
Err(_status) => return None
|
}
|
||||||
};
|
|
||||||
|
|
||||||
response.body().and_then(|body| body.to_string())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue