feat: add has_mime in request
@@ -11,6 +11,7 @@ pub use http::request::Parts;
use http::version::Version;
use http::{self, Extensions, Uri};
pub use hyper::Body as ReqBody;
+use mime;
use multimap::MultiMap;
use once_cell::sync::OnceCell;
use serde::de::Deserialize;
@@ -386,6 +387,16 @@ impl Request {
.and_then(|v| v.parse().ok())
}
+ /// Get content type.
+ #[inline]
+ pub fn has_mime(&self, mime: mime::Name) -> bool {
+ if let Some(ctype) = self.content_type() {
+ ctype.subtype() == mime
+ } else {
+ false
+ }
+ }
+
cfg_feature! {
#![feature = "cookie"]
/// Get `CookieJar` reference.
@@ -775,4 +786,13 @@ file content\r\n\
let files = req.files("file1").await.unwrap();
assert_eq!(files[0].name().unwrap(), "err.txt");
}
+
+ #[tokio::test]
+ async fn test_mime() {
+ let req = TestClient::get("http://127.0.0.1:7979/hello.json")
+ .add_header("content-type", "application/json", true)
+ .build();
+
+ assert_eq!(req.has_mime(mime::JSON), true);
+ }
}
