mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 07:53:00 +01:00
fix request json loader; mime_type() method
This commit is contained in:
parent
e8e2ca1526
commit
4abb769ee5
@ -1,5 +1,12 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## 0.3.4 (2018-..-..)
|
||||||
|
|
||||||
|
* Fix request json loader
|
||||||
|
|
||||||
|
* Added HttpRequest::mime_type() method
|
||||||
|
|
||||||
|
|
||||||
## 0.3.3 (2018-01-25)
|
## 0.3.3 (2018-01-25)
|
||||||
|
|
||||||
* Stop processing any events after context stop
|
* Stop processing any events after context stop
|
||||||
|
@ -8,6 +8,7 @@ use cookie::Cookie;
|
|||||||
use futures::{Async, Future, Stream, Poll};
|
use futures::{Async, Future, Stream, Poll};
|
||||||
use http_range::HttpRange;
|
use http_range::HttpRange;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
|
use mime::Mime;
|
||||||
use url::{Url, form_urlencoded};
|
use url::{Url, form_urlencoded};
|
||||||
use http::{header, Uri, Method, Version, HeaderMap, Extensions};
|
use http::{header, Uri, Method, Version, HeaderMap, Extensions};
|
||||||
|
|
||||||
@ -371,12 +372,25 @@ impl<S> HttpRequest<S> {
|
|||||||
pub fn content_type(&self) -> &str {
|
pub fn content_type(&self) -> &str {
|
||||||
if let Some(content_type) = self.headers().get(header::CONTENT_TYPE) {
|
if let Some(content_type) = self.headers().get(header::CONTENT_TYPE) {
|
||||||
if let Ok(content_type) = content_type.to_str() {
|
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<Mime> {
|
||||||
|
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
|
/// Check if request requires connection upgrade
|
||||||
pub(crate) fn upgrade(&self) -> bool {
|
pub(crate) fn upgrade(&self) -> bool {
|
||||||
if let Some(conn) = self.as_ref().headers.get(header::CONNECTION) {
|
if let Some(conn) = self.as_ref().headers.get(header::CONNECTION) {
|
||||||
@ -754,6 +768,7 @@ impl Future for RequestBody {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use mime;
|
||||||
use http::Uri;
|
use http::Uri;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use router::Pattern;
|
use router::Pattern;
|
||||||
@ -768,6 +783,31 @@ mod tests {
|
|||||||
assert!(dbg.contains("HttpRequest"));
|
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]
|
#[test]
|
||||||
fn test_no_request_cookies() {
|
fn test_no_request_cookies() {
|
||||||
let req = HttpRequest::default();
|
let req = HttpRequest::default();
|
||||||
|
@ -898,14 +898,14 @@ mod tests {
|
|||||||
assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
header::HeaderValue::from_static("text/plain; charset=utf-8"));
|
header::HeaderValue::from_static("text/plain; charset=utf-8"));
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
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();
|
let resp: HttpResponse = (&"test".to_owned()).respond_to(req.clone()).ok().unwrap();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
header::HeaderValue::from_static("text/plain; charset=utf-8"));
|
header::HeaderValue::from_static("text/plain; charset=utf-8"));
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
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 b = Bytes::from_static(b"test");
|
||||||
let resp: HttpResponse = b.into();
|
let resp: HttpResponse = b.into();
|
||||||
|
Loading…
Reference in New Issue
Block a user