From e9c68ba66682d2aa2db2b4710590eff4dcd89b4c Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Mon, 18 Dec 2017 06:26:55 -0800 Subject: [PATCH] Add regression test for issue #505. --- .../local-request-content-type-issue-505.rs | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 lib/tests/local-request-content-type-issue-505.rs diff --git a/lib/tests/local-request-content-type-issue-505.rs b/lib/tests/local-request-content-type-issue-505.rs new file mode 100644 index 00000000..eeba0d6d --- /dev/null +++ b/lib/tests/local-request-content-type-issue-505.rs @@ -0,0 +1,93 @@ +#![feature(plugin, decl_macro)] +#![plugin(rocket_codegen)] + +extern crate rocket; + +use rocket::Outcome::*; +use rocket::{Request, Data}; +use rocket::request::{self, FromRequest}; + +struct HasContentType; + +impl<'a, 'r> FromRequest<'a, 'r> for HasContentType { + type Error = (); + + fn from_request(request: &'a Request<'r>) -> request::Outcome { + if request.content_type().is_some() { + Success(HasContentType) + } else { + Forward(()) + } + } +} + +use rocket::data::{self, FromData}; + +impl FromData for HasContentType { + type Error = (); + + fn from_data(request: &Request, data: Data) -> data::Outcome { + if request.content_type().is_some() { + Success(HasContentType) + } else { + Forward(data) + } + } +} + +#[post("/")] +fn rg_ct(ct: Option) -> &'static str { + ct.map_or("Absent", |_| "Present") +} + +#[post("/data", data = "<_ct>", rank = 1)] +fn data_has_ct(_ct: HasContentType) -> &'static str { + "Data Present" +} + +#[post("/data", rank = 2)] +fn data_no_ct() -> &'static str { + "Data Absent" +} + +mod local_request_content_type_tests { + use super::*; + + use rocket::Rocket; + use rocket::local::Client; + use rocket::http::ContentType; + + fn rocket() -> Rocket { + rocket::ignite().mount("/", routes![rg_ct, data_has_ct, data_no_ct]) + } + + #[test] + fn has_no_ct() { + let client = Client::new(rocket()).unwrap(); + + let mut req = client.post("/"); + assert_eq!(req.cloned_dispatch().body_string(), Some("Absent".to_string())); + assert_eq!(req.mut_dispatch().body_string(), Some("Absent".to_string())); + assert_eq!(req.dispatch().body_string(), Some("Absent".to_string())); + + let mut req = client.post("/data"); + assert_eq!(req.cloned_dispatch().body_string(), Some("Data Absent".to_string())); + assert_eq!(req.mut_dispatch().body_string(), Some("Data Absent".to_string())); + assert_eq!(req.dispatch().body_string(), Some("Data Absent".to_string())); + } + + #[test] + fn has_ct() { + let client = Client::new(rocket()).unwrap(); + + let mut req = client.post("/").header(ContentType::JSON); + assert_eq!(req.cloned_dispatch().body_string(), Some("Present".to_string())); + assert_eq!(req.mut_dispatch().body_string(), Some("Present".to_string())); + assert_eq!(req.dispatch().body_string(), Some("Present".to_string())); + + let mut req = client.post("/data").header(ContentType::JSON); + assert_eq!(req.cloned_dispatch().body_string(), Some("Data Present".to_string())); + assert_eq!(req.mut_dispatch().body_string(), Some("Data Present".to_string())); + assert_eq!(req.dispatch().body_string(), Some("Data Present".to_string())); + } +}