Make 'cargo test' work without '--all-features'.

This commit is contained in:
Sergio Benitez 2017-01-05 14:51:00 -06:00
parent 2da08a975c
commit 069f09cb7e
4 changed files with 125 additions and 110 deletions

View File

@ -4,7 +4,6 @@
extern crate rocket; extern crate rocket;
use rocket::request::Form; use rocket::request::Form;
use rocket::http::Status;
#[derive(FromForm)] #[derive(FromForm)]
struct FormData { struct FormData {
@ -17,31 +16,35 @@ fn bug(form_data: Form<FormData>) -> &'static str {
"OK" "OK"
} }
use rocket::testing::MockRequest; #[cfg(feature = "testing")]
use rocket::http::Method::*; mod tests {
use rocket::http::ContentType; use super::*;
use rocket::testing::MockRequest;
use rocket::http::Method::*;
use rocket::http::{Status, ContentType};
#[test] #[test]
fn method_eval() { fn method_eval() {
let rocket = rocket::ignite().mount("/", routes![bug]); let rocket = rocket::ignite().mount("/", routes![bug]);
let mut req = MockRequest::new(Post, "/") let mut req = MockRequest::new(Post, "/")
.header(ContentType::Form) .header(ContentType::Form)
.body("_method=patch&form_data=Form+data"); .body("_method=patch&form_data=Form+data");
let mut response = req.dispatch_with(&rocket); let mut response = req.dispatch_with(&rocket);
let body_str = response.body().and_then(|b| b.into_string()); let body_str = response.body().and_then(|b| b.into_string());
assert_eq!(body_str, Some("OK".to_string())); assert_eq!(body_str, Some("OK".to_string()));
} }
#[test] #[test]
fn get_passes_through() { fn get_passes_through() {
let rocket = rocket::ignite().mount("/", routes![bug]); let rocket = rocket::ignite().mount("/", routes![bug]);
let mut req = MockRequest::new(Get, "/") let mut req = MockRequest::new(Get, "/")
.header(ContentType::Form) .header(ContentType::Form)
.body("_method=patch&form_data=Form+data"); .body("_method=patch&form_data=Form+data");
let response = req.dispatch_with(&rocket); let response = req.dispatch_with(&rocket);
assert_eq!(response.status(), Status::NotFound); assert_eq!(response.status(), Status::NotFound);
}
} }

View File

@ -15,31 +15,35 @@ fn bug(form_data: Form<FormData>) -> String {
form_data.into_inner().form_data form_data.into_inner().form_data
} }
use rocket::testing::MockRequest; #[cfg(feature = "testing")]
use rocket::http::Method::*; mod tests {
use rocket::http::ContentType; use super::*;
use rocket::http::Status; use rocket::testing::MockRequest;
use rocket::http::Method::*;
use rocket::http::ContentType;
use rocket::http::Status;
fn check_decoding(raw: &str, decoded: &str) { fn check_decoding(raw: &str, decoded: &str) {
let rocket = rocket::ignite().mount("/", routes![bug]); let rocket = rocket::ignite().mount("/", routes![bug]);
let mut req = MockRequest::new(Post, "/") let mut req = MockRequest::new(Post, "/")
.header(ContentType::Form) .header(ContentType::Form)
.body(format!("form_data={}", raw)); .body(format!("form_data={}", raw));
let mut response = req.dispatch_with(&rocket); let mut response = req.dispatch_with(&rocket);
let body_string = response.body().and_then(|b| b.into_string()); let body_string = response.body().and_then(|b| b.into_string());
assert_eq!(response.status(), Status::Ok); assert_eq!(response.status(), Status::Ok);
assert_eq!(Some(decoded.to_string()), body_string); assert_eq!(Some(decoded.to_string()), body_string);
} }
#[test] #[test]
fn test_proper_decoding() { fn test_proper_decoding() {
check_decoding("password", "password"); check_decoding("password", "password");
check_decoding("", ""); check_decoding("", "");
check_decoding("+", " "); check_decoding("+", " ");
check_decoding("%2B", "+"); check_decoding("%2B", "+");
check_decoding("1+1", "1 1"); check_decoding("1+1", "1 1");
check_decoding("1%2B1", "1+1"); check_decoding("1%2B1", "1+1");
check_decoding("%3Fa%3D1%26b%3D2", "?a=1&b=2"); check_decoding("%3Fa%3D1%26b%3D2", "?a=1&b=2");
}
} }

View File

@ -3,9 +3,7 @@
extern crate rocket; extern crate rocket;
use rocket::Route;
use rocket::response::{status, content}; use rocket::response::{status, content};
use rocket::http::ContentType;
#[get("/empty")] #[get("/empty")]
fn empty() -> status::NoContent { fn empty() -> status::NoContent {
@ -22,51 +20,57 @@ fn other() -> content::JSON<()> {
content::JSON(()) content::JSON(())
} }
fn routes() -> Vec<Route> { #[cfg(feature = "testing")]
routes![index, empty, other] mod tests {
} use super::*;
use rocket::testing::MockRequest; use rocket::Route;
use rocket::http::Method::*; use rocket::testing::MockRequest;
use rocket::http::Status; use rocket::http::Method::*;
use rocket::response::Body; use rocket::http::{Status, ContentType};
use rocket::response::Body;
#[test] fn routes() -> Vec<Route> {
fn auto_head() { routes![index, empty, other]
let rocket = rocket::ignite().mount("/", routes());
let mut req = MockRequest::new(Head, "/");
let mut response = req.dispatch_with(&rocket);
assert_eq!(response.status(), Status::Ok);
if let Some(body) = response.body() {
match body {
Body::Sized(_, n) => assert_eq!(n, "Hello, world!".len() as u64),
_ => panic!("Expected a sized body!")
}
assert_eq!(body.into_string(), Some("".to_string()));
} else {
panic!("Expected an empty body!")
} }
#[test]
fn auto_head() {
let rocket = rocket::ignite().mount("/", routes());
let content_type: Vec<_> = response.header_values("Content-Type").collect(); let mut req = MockRequest::new(Head, "/");
assert_eq!(content_type, vec![ContentType::Plain.to_string()]); let mut response = req.dispatch_with(&rocket);
let mut req = MockRequest::new(Head, "/empty"); assert_eq!(response.status(), Status::Ok);
let response = req.dispatch_with(&rocket); if let Some(body) = response.body() {
assert_eq!(response.status(), Status::NoContent); match body {
} Body::Sized(_, n) => assert_eq!(n, "Hello, world!".len() as u64),
_ => panic!("Expected a sized body!")
#[test] }
fn user_head() {
let rocket = rocket::ignite().mount("/", routes()); assert_eq!(body.into_string(), Some("".to_string()));
let mut req = MockRequest::new(Head, "/other"); } else {
let response = req.dispatch_with(&rocket); panic!("Expected an empty body!")
}
assert_eq!(response.status(), Status::Ok);
let content_type: Vec<_> = response.header_values("Content-Type").collect(); let content_type: Vec<_> = response.header_values("Content-Type").collect();
assert_eq!(content_type, vec![ContentType::JSON.to_string()]); assert_eq!(content_type, vec![ContentType::Plain.to_string()]);
let mut req = MockRequest::new(Head, "/empty");
let response = req.dispatch_with(&rocket);
assert_eq!(response.status(), Status::NoContent);
}
#[test]
fn user_head() {
let rocket = rocket::ignite().mount("/", routes());
let mut req = MockRequest::new(Head, "/other");
let response = req.dispatch_with(&rocket);
assert_eq!(response.status(), Status::Ok);
let content_type: Vec<_> = response.header_values("Content-Type").collect();
assert_eq!(content_type, vec![ContentType::JSON.to_string()]);
}
} }

View File

@ -30,26 +30,30 @@ fn dual(user: String, path: Segments) -> String {
user + "/is/" + &path.collect::<Vec<_>>().join("/") user + "/is/" + &path.collect::<Vec<_>>().join("/")
} }
use rocket::testing::MockRequest; #[cfg(feature = "testing")]
use rocket::http::Method::*; mod tests {
use super::*;
use rocket::testing::MockRequest;
use rocket::http::Method::*;
#[test] #[test]
fn segments_works() { fn segments_works() {
let rocket = rocket::ignite() let rocket = rocket::ignite()
.mount("/", routes![test, two, one_two, none, dual]) .mount("/", routes![test, two, one_two, none, dual])
.mount("/point", routes![test, two, one_two, dual]); .mount("/point", routes![test, two, one_two, dual]);
// We construct a path that matches each of the routes above. We ensure the // We construct a path that matches each of the routes above. We ensure the
// prefix is stripped, confirming that dynamic segments are working. // prefix is stripped, confirming that dynamic segments are working.
for prefix in &["", "/test", "/two", "/one/two", for prefix in &["", "/test", "/two", "/one/two",
"/point/test", "/point/two", "/point/one/two", "/point/test", "/point/two", "/point/one/two",
"/static", "/point/static"] "/static", "/point/static"]
{ {
let path = "this/is/the/path/we/want"; let path = "this/is/the/path/we/want";
let mut req = MockRequest::new(Get, format!("{}/{}", prefix, path)); let mut req = MockRequest::new(Get, format!("{}/{}", prefix, path));
let mut response = req.dispatch_with(&rocket); let mut response = req.dispatch_with(&rocket);
let body_str = response.body().and_then(|b| b.into_string()); let body_str = response.body().and_then(|b| b.into_string());
assert_eq!(body_str, Some(path.into())); assert_eq!(body_str, Some(path.into()));
}
} }
} }