diff --git a/CHANGES.md b/CHANGES.md index 760dbcc55..1b149db8c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,12 @@ # Changes +## 0.3.4 (2018-..-..) + +* Fix request json loader + +* Added HttpRequest::mime_type() method + + ## 0.3.3 (2018-01-25) * Stop processing any events after context stop diff --git a/src/httprequest.rs b/src/httprequest.rs index 14c252748..e16892191 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -8,6 +8,7 @@ use cookie::Cookie; use futures::{Async, Future, Stream, Poll}; use http_range::HttpRange; use serde::de::DeserializeOwned; +use mime::Mime; use url::{Url, form_urlencoded}; use http::{header, Uri, Method, Version, HeaderMap, Extensions}; @@ -371,12 +372,25 @@ impl HttpRequest { pub fn content_type(&self) -> &str { if let Some(content_type) = self.headers().get(header::CONTENT_TYPE) { if let Ok(content_type) = content_type.to_str() { - return content_type + return content_type.split(';').next().unwrap().trim() } } "" } + /// Convert the request content type to a known mime type. + pub fn mime_type(&self) -> Option { + if let Some(content_type) = self.headers().get(header::CONTENT_TYPE) { + if let Ok(content_type) = content_type.to_str() { + return match content_type.parse() { + Ok(mt) => Some(mt), + Err(_) => None + }; + } + } + None + } + /// Check if request requires connection upgrade pub(crate) fn upgrade(&self) -> bool { if let Some(conn) = self.as_ref().headers.get(header::CONNECTION) { @@ -754,6 +768,7 @@ impl Future for RequestBody { #[cfg(test)] mod tests { use super::*; + use mime; use http::Uri; use std::str::FromStr; use router::Pattern; @@ -768,6 +783,31 @@ mod tests { assert!(dbg.contains("HttpRequest")); } + #[test] + fn test_content_type() { + let req = TestRequest::with_header("content-type", "text/plain").finish(); + assert_eq!(req.content_type(), "text/plain"); + let req = TestRequest::with_header( + "content-type", "application/json; charset=utf=8").finish(); + assert_eq!(req.content_type(), "application/json"); + let req = HttpRequest::default(); + assert_eq!(req.content_type(), ""); + } + + #[test] + fn test_mime_type() { + let req = TestRequest::with_header("content-type", "application/json").finish(); + assert_eq!(req.mime_type(), Some(mime::APPLICATION_JSON)); + let req = HttpRequest::default(); + assert_eq!(req.mime_type(), None); + let req = TestRequest::with_header( + "content-type", "application/json; charset=utf-8").finish(); + let mt = req.mime_type().unwrap(); + assert_eq!(mt.get_param(mime::CHARSET), Some(mime::UTF_8)); + assert_eq!(mt.type_(), mime::APPLICATION); + assert_eq!(mt.subtype(), mime::JSON); + } + #[test] fn test_no_request_cookies() { let req = HttpRequest::default(); diff --git a/src/httpresponse.rs b/src/httpresponse.rs index 10f13687f..b9cdb60b3 100644 --- a/src/httpresponse.rs +++ b/src/httpresponse.rs @@ -898,14 +898,14 @@ mod tests { assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), header::HeaderValue::from_static("text/plain; charset=utf-8")); assert_eq!(resp.status(), StatusCode::OK); - assert_eq!(resp.body().binary().unwrap(), &Binary::from((&"test".to_owned()))); + assert_eq!(resp.body().binary().unwrap(), &Binary::from(&"test".to_owned())); let resp: HttpResponse = (&"test".to_owned()).respond_to(req.clone()).ok().unwrap(); assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), header::HeaderValue::from_static("text/plain; charset=utf-8")); assert_eq!(resp.status(), StatusCode::OK); - assert_eq!(resp.body().binary().unwrap(), &Binary::from((&"test".to_owned()))); + assert_eq!(resp.body().binary().unwrap(), &Binary::from(&"test".to_owned())); let b = Bytes::from_static(b"test"); let resp: HttpResponse = b.into();