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,12 +16,15 @@ 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, "/")
@ -32,10 +34,10 @@ fn method_eval() {
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, "/")
@ -44,4 +46,5 @@ fn get_passes_through() {
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,12 +15,15 @@ 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, "/")
@ -31,10 +34,10 @@ fn check_decoding(raw: &str, decoded: &str) {
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("+", " ");
@ -42,4 +45,5 @@ fn test_proper_decoding() {
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,17 +20,22 @@ fn other() -> content::JSON<()> {
content::JSON(()) content::JSON(())
} }
fn routes() -> Vec<Route> { #[cfg(feature = "testing")]
mod tests {
use super::*;
use rocket::Route;
use rocket::testing::MockRequest;
use rocket::http::Method::*;
use rocket::http::{Status, ContentType};
use rocket::response::Body;
fn routes() -> Vec<Route> {
routes![index, empty, other] routes![index, empty, other]
} }
use rocket::testing::MockRequest; #[test]
use rocket::http::Method::*; fn auto_head() {
use rocket::http::Status;
use rocket::response::Body;
#[test]
fn auto_head() {
let rocket = rocket::ignite().mount("/", routes()); let rocket = rocket::ignite().mount("/", routes());
let mut req = MockRequest::new(Head, "/"); let mut req = MockRequest::new(Head, "/");
@ -57,10 +60,10 @@ fn auto_head() {
let mut req = MockRequest::new(Head, "/empty"); let mut req = MockRequest::new(Head, "/empty");
let response = req.dispatch_with(&rocket); let response = req.dispatch_with(&rocket);
assert_eq!(response.status(), Status::NoContent); assert_eq!(response.status(), Status::NoContent);
} }
#[test] #[test]
fn user_head() { fn user_head() {
let rocket = rocket::ignite().mount("/", routes()); let rocket = rocket::ignite().mount("/", routes());
let mut req = MockRequest::new(Head, "/other"); let mut req = MockRequest::new(Head, "/other");
let response = req.dispatch_with(&rocket); let response = req.dispatch_with(&rocket);
@ -69,4 +72,5 @@ fn user_head() {
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::JSON.to_string()]);
}
} }

View File

@ -30,11 +30,14 @@ 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]);
@ -52,4 +55,5 @@ fn segments_works() {
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()));
} }
}
} }