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;
use rocket::request::Form;
use rocket::http::Status;
#[derive(FromForm)]
struct FormData {
@ -17,31 +16,35 @@ fn bug(form_data: Form<FormData>) -> &'static str {
"OK"
}
use rocket::testing::MockRequest;
use rocket::http::Method::*;
use rocket::http::ContentType;
#[cfg(feature = "testing")]
mod tests {
use super::*;
use rocket::testing::MockRequest;
use rocket::http::Method::*;
use rocket::http::{Status, ContentType};
#[test]
fn method_eval() {
let rocket = rocket::ignite().mount("/", routes![bug]);
#[test]
fn method_eval() {
let rocket = rocket::ignite().mount("/", routes![bug]);
let mut req = MockRequest::new(Post, "/")
.header(ContentType::Form)
.body("_method=patch&form_data=Form+data");
let mut req = MockRequest::new(Post, "/")
.header(ContentType::Form)
.body("_method=patch&form_data=Form+data");
let mut response = req.dispatch_with(&rocket);
let body_str = response.body().and_then(|b| b.into_string());
assert_eq!(body_str, Some("OK".to_string()));
}
#[test]
fn get_passes_through() {
let rocket = rocket::ignite().mount("/", routes![bug]);
let mut req = MockRequest::new(Get, "/")
.header(ContentType::Form)
.body("_method=patch&form_data=Form+data");
let response = req.dispatch_with(&rocket);
assert_eq!(response.status(), Status::NotFound);
let mut response = req.dispatch_with(&rocket);
let body_str = response.body().and_then(|b| b.into_string());
assert_eq!(body_str, Some("OK".to_string()));
}
#[test]
fn get_passes_through() {
let rocket = rocket::ignite().mount("/", routes![bug]);
let mut req = MockRequest::new(Get, "/")
.header(ContentType::Form)
.body("_method=patch&form_data=Form+data");
let response = req.dispatch_with(&rocket);
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
}
use rocket::testing::MockRequest;
use rocket::http::Method::*;
use rocket::http::ContentType;
use rocket::http::Status;
#[cfg(feature = "testing")]
mod tests {
use super::*;
use rocket::testing::MockRequest;
use rocket::http::Method::*;
use rocket::http::ContentType;
use rocket::http::Status;
fn check_decoding(raw: &str, decoded: &str) {
let rocket = rocket::ignite().mount("/", routes![bug]);
fn check_decoding(raw: &str, decoded: &str) {
let rocket = rocket::ignite().mount("/", routes![bug]);
let mut req = MockRequest::new(Post, "/")
.header(ContentType::Form)
.body(format!("form_data={}", raw));
let mut req = MockRequest::new(Post, "/")
.header(ContentType::Form)
.body(format!("form_data={}", raw));
let mut response = req.dispatch_with(&rocket);
let body_string = response.body().and_then(|b| b.into_string());
assert_eq!(response.status(), Status::Ok);
assert_eq!(Some(decoded.to_string()), body_string);
}
#[test]
fn test_proper_decoding() {
check_decoding("password", "password");
check_decoding("", "");
check_decoding("+", " ");
check_decoding("%2B", "+");
check_decoding("1+1", "1 1");
check_decoding("1%2B1", "1+1");
check_decoding("%3Fa%3D1%26b%3D2", "?a=1&b=2");
let mut response = req.dispatch_with(&rocket);
let body_string = response.body().and_then(|b| b.into_string());
assert_eq!(response.status(), Status::Ok);
assert_eq!(Some(decoded.to_string()), body_string);
}
#[test]
fn test_proper_decoding() {
check_decoding("password", "password");
check_decoding("", "");
check_decoding("+", " ");
check_decoding("%2B", "+");
check_decoding("1+1", "1 1");
check_decoding("1%2B1", "1+1");
check_decoding("%3Fa%3D1%26b%3D2", "?a=1&b=2");
}
}

View File

@ -3,9 +3,7 @@
extern crate rocket;
use rocket::Route;
use rocket::response::{status, content};
use rocket::http::ContentType;
#[get("/empty")]
fn empty() -> status::NoContent {
@ -22,51 +20,57 @@ fn other() -> content::JSON<()> {
content::JSON(())
}
fn routes() -> Vec<Route> {
routes![index, empty, other]
}
#[cfg(feature = "testing")]
mod tests {
use super::*;
use rocket::testing::MockRequest;
use rocket::http::Method::*;
use rocket::http::Status;
use rocket::response::Body;
use rocket::Route;
use rocket::testing::MockRequest;
use rocket::http::Method::*;
use rocket::http::{Status, ContentType};
use rocket::response::Body;
#[test]
fn auto_head() {
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!")
fn routes() -> Vec<Route> {
routes![index, empty, other]
}
#[test]
fn auto_head() {
let rocket = rocket::ignite().mount("/", routes());
let content_type: Vec<_> = response.header_values("Content-Type").collect();
assert_eq!(content_type, vec![ContentType::Plain.to_string()]);
let mut req = MockRequest::new(Head, "/");
let mut response = req.dispatch_with(&rocket);
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()]);
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!")
}
let content_type: Vec<_> = response.header_values("Content-Type").collect();
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("/")
}
use rocket::testing::MockRequest;
use rocket::http::Method::*;
#[cfg(feature = "testing")]
mod tests {
use super::*;
use rocket::testing::MockRequest;
use rocket::http::Method::*;
#[test]
fn segments_works() {
let rocket = rocket::ignite()
.mount("/", routes![test, two, one_two, none, dual])
.mount("/point", routes![test, two, one_two, dual]);
#[test]
fn segments_works() {
let rocket = rocket::ignite()
.mount("/", routes![test, two, one_two, none, dual])
.mount("/point", routes![test, two, one_two, dual]);
// We construct a path that matches each of the routes above. We ensure the
// prefix is stripped, confirming that dynamic segments are working.
for prefix in &["", "/test", "/two", "/one/two",
"/point/test", "/point/two", "/point/one/two",
"/static", "/point/static"]
{
let path = "this/is/the/path/we/want";
let mut req = MockRequest::new(Get, format!("{}/{}", prefix, path));
// We construct a path that matches each of the routes above. We ensure the
// prefix is stripped, confirming that dynamic segments are working.
for prefix in &["", "/test", "/two", "/one/two",
"/point/test", "/point/two", "/point/one/two",
"/static", "/point/static"]
{
let path = "this/is/the/path/we/want";
let mut req = MockRequest::new(Get, format!("{}/{}", prefix, path));
let mut response = req.dispatch_with(&rocket);
let body_str = response.body().and_then(|b| b.into_string());
assert_eq!(body_str, Some(path.into()));
let mut response = req.dispatch_with(&rocket);
let body_str = response.body().and_then(|b| b.into_string());
assert_eq!(body_str, Some(path.into()));
}
}
}