Fix codegen tests for new lib. Make UTF8 charset the default for text content types.

This commit is contained in:
Sergio Benitez 2016-11-02 17:39:41 +01:00
parent c98d047038
commit 004cae7627
4 changed files with 33 additions and 19 deletions

View File

@ -4,6 +4,7 @@
extern crate rocket; extern crate rocket;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::Utf8Error;
#[post("/<a>/<b..>")] #[post("/<a>/<b..>")]
fn get(a: String, b: PathBuf) -> String { fn get(a: String, b: PathBuf) -> String {
@ -11,7 +12,7 @@ fn get(a: String, b: PathBuf) -> String {
} }
#[post("/<a>/<b..>")] #[post("/<a>/<b..>")]
fn get2(a: String, b: Result<PathBuf, ()>) -> String { fn get2(a: String, b: Result<PathBuf, Utf8Error>) -> String {
format!("{}/{}", a, b.unwrap().to_string_lossy()) format!("{}/{}", a, b.unwrap().to_string_lossy())
} }

View File

@ -4,7 +4,7 @@ use std::str::FromStr;
use std::borrow::Borrow; use std::borrow::Borrow;
use std::fmt; use std::fmt;
use http::mime::{Mime, Param, TopLevel, SubLevel}; use http::mime::{Mime, Param, Attr, Value, TopLevel, SubLevel};
use router::Collider; use router::Collider;
/// Typed representation of HTTP Content-Types. /// Typed representation of HTTP Content-Types.
@ -45,7 +45,7 @@ impl ContentType {
} }
/// Constructs a new content type of the given top level and sub level /// Constructs a new content type of the given top level and sub level
/// types. /// types. If the top-level type is `Text`, a charset of UTF-8 is set.
/// ///
/// # Examples /// # Examples
/// ///
@ -53,12 +53,26 @@ impl ContentType {
/// use rocket::http::ContentType; /// use rocket::http::ContentType;
/// use rocket::http::mime::{TopLevel, SubLevel}; /// use rocket::http::mime::{TopLevel, SubLevel};
/// ///
/// let html = ContentType::of(TopLevel::Application, SubLevel::Html); /// let ct = ContentType::of(TopLevel::Text, SubLevel::Html);
/// assert!(html.is_html()); /// assert_eq!(ct.to_string(), "text/html; charset=utf-8".to_string());
/// assert!(ct.is_html());
/// ```
///
/// ```rust
/// use rocket::http::ContentType;
/// use rocket::http::mime::{TopLevel, SubLevel};
///
/// let ct = ContentType::of(TopLevel::Application, SubLevel::Json);
/// assert_eq!(ct.to_string(), "application/json".to_string());
/// assert!(ct.is_json());
/// ``` /// ```
#[inline(always)] #[inline(always)]
pub fn of(t: TopLevel, s: SubLevel) -> ContentType { pub fn of(t: TopLevel, s: SubLevel) -> ContentType {
ContentType(t, s, None) if t == TopLevel::Text {
ContentType(t, s, Some(vec![(Attr::Charset, Value::Utf8)]))
} else {
ContentType(t, s, None)
}
} }
/// Returns the Content-Type associated with the extension `ext`. Not all /// Returns the Content-Type associated with the extension `ext`. Not all
@ -88,7 +102,7 @@ impl ContentType {
let (top_level, sub_level) = match ext { let (top_level, sub_level) = match ext {
"txt" => (TopLevel::Text, SubLevel::Plain), "txt" => (TopLevel::Text, SubLevel::Plain),
"html" | "htm" => (TopLevel::Text, SubLevel::Html), "html" | "htm" => (TopLevel::Text, SubLevel::Html),
"xml" => (TopLevel::Application, SubLevel::Xml), "xml" => (TopLevel::Text, SubLevel::Xml),
"js" => (TopLevel::Application, SubLevel::Javascript), "js" => (TopLevel::Application, SubLevel::Javascript),
"css" => (TopLevel::Text, SubLevel::Css), "css" => (TopLevel::Text, SubLevel::Css),
"json" => (TopLevel::Application, SubLevel::Json), "json" => (TopLevel::Application, SubLevel::Json),
@ -111,11 +125,11 @@ impl ContentType {
/// Returns a `ContentType` representing JSON, i.e, `application/json`. /// Returns a `ContentType` representing JSON, i.e, `application/json`.
| json: Application/Json, | json: Application/Json,
/// Returns a `ContentType` representing XML, i.e, `application/xml`. /// Returns a `ContentType` representing XML, i.e, `text/xml`.
| xml: Application/Xml, | xml: Text/Xml,
/// Returns a `ContentType` representing HTML, i.e, `application/html`. /// Returns a `ContentType` representing HTML, i.e, `text/html`.
| html: Application/Html | html: Text/Html
} }
/// Returns true if this content type is not one of the standard content /// Returns true if this content type is not one of the standard content
@ -137,14 +151,14 @@ impl ContentType {
/// Returns true if the content type is JSON, i.e: `application/json`. /// Returns true if the content type is JSON, i.e: `application/json`.
| is_json: Application/Json, | is_json: Application/Json,
/// Returns true if the content type is XML, i.e: `application/xml`. /// Returns true if the content type is XML, i.e: `text/xml`.
| is_xml: Application/Xml, | is_xml: Text/Xml,
/// Returns true if the content type is any, i.e.: `*/*`. /// Returns true if the content type is any, i.e.: `*/*`.
| is_any: Star/Star, | is_any: Star/Star,
/// Returns true if the content type is HTML, i.e.: `application/html`. /// Returns true if the content type is HTML, i.e.: `text/html`.
| is_html: Application/Html, | is_html: Text/Html,
/// Returns true if the content type is that for non-data HTTP forms, /// Returns true if the content type is that for non-data HTTP forms,
/// i.e.: `application/x-www-form-urlencoded`. /// i.e.: `application/x-www-form-urlencoded`.
@ -287,8 +301,8 @@ impl fmt::Display for ContentType {
/// ```rust /// ```rust
/// use rocket::http::ContentType; /// use rocket::http::ContentType;
/// ///
/// let http_ct = format!("{}", ContentType::xml()); /// let ct = format!("{}", ContentType::json());
/// assert_eq!(http_ct, "application/xml".to_string()); /// assert_eq!(ct, "application/json".to_string());
/// ``` /// ```
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}/{}", self.0.as_str(), self.1.as_str())?; write!(f, "{}/{}", self.0.as_str(), self.1.as_str())?;

View File

@ -19,5 +19,4 @@ pub use self::method::Method;
pub use self::hyper::StatusCode; pub use self::hyper::StatusCode;
pub use self::content_type::ContentType; pub use self::content_type::ContentType;
/// Can I document it here?
pub use self::cookies::{Cookie, Cookies}; pub use self::cookies::{Cookie, Cookies};

View File

@ -20,7 +20,7 @@ impl<'a, T, E> IntoOutcome<(), (), (StatusCode, FreshHyperResponse<'a>)> for Res
} }
pub trait Responder { pub trait Responder {
fn respond<'a>(&mut self, mut res: FreshHyperResponse<'a>) -> Outcome<'a>; fn respond<'a>(&mut self, res: FreshHyperResponse<'a>) -> Outcome<'a>;
} }
impl<'a> Responder for &'a str { impl<'a> Responder for &'a str {