1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-07-04 04:55:48 +00:00

fix request json loader; mime_type() method

This commit is contained in:
Nikolay Kim 2018-01-25 21:50:28 -08:00
parent e8e2ca1526
commit 4abb769ee5
3 changed files with 50 additions and 3 deletions

View file

@ -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

View file

@ -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<S> HttpRequest<S> {
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<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
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();

View file

@ -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();