From 374593c2f0626bed6a205d0d42adb76b81289e27 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Tue, 31 Jan 2017 02:43:19 -0800 Subject: [PATCH] Use 'ContentType::Plain' for 'String' responses. Resolves #49. --- examples/content_types/src/main.rs | 14 ++++++++------ examples/errors/src/main.rs | 11 ++++++----- examples/from_request/src/main.rs | 13 ++++--------- examples/pastebin/src/main.rs | 4 ++-- lib/src/response/responder.rs | 6 +++--- lib/tests/form_value_decoding-issue-82.rs | 1 - 6 files changed, 23 insertions(+), 26 deletions(-) diff --git a/examples/content_types/src/main.rs b/examples/content_types/src/main.rs index 84b2b0ca..6a667b33 100644 --- a/examples/content_types/src/main.rs +++ b/examples/content_types/src/main.rs @@ -11,7 +11,7 @@ mod tests; use rocket::{Request, Error}; use rocket::http::ContentType; -use rocket::response::content::JSON; +use rocket::response::content; #[derive(Debug, Serialize, Deserialize)] struct Person { @@ -22,26 +22,28 @@ struct Person { // This shows how to manually serialize some JSON, but in a real application, // we'd use the JSON contrib type. #[get("//", format = "application/json")] -fn hello(content_type: ContentType, name: String, age: i8) -> JSON { +fn hello(content_type: ContentType, name: String, age: i8) -> content::JSON { let person = Person { name: name, age: age, }; println!("ContentType: {}", content_type); - JSON(serde_json::to_string(&person).unwrap()) + content::JSON(serde_json::to_string(&person).unwrap()) } #[error(404)] -fn not_found(_: Error, request: &Request) -> String { - if !request.content_type().is_json() { +fn not_found(_: Error, request: &Request) -> content::HTML { + let html = if !request.content_type().is_json() { format!("

This server only supports JSON requests, not '{}'.

", request.content_type()) } else { format!("

Sorry, '{}' is an invalid path! Try \ /hello/<name>/<age> instead.

", request.uri()) - } + }; + + content::HTML(html) } fn main() { diff --git a/examples/errors/src/main.rs b/examples/errors/src/main.rs index f4670c9d..47ebd736 100644 --- a/examples/errors/src/main.rs +++ b/examples/errors/src/main.rs @@ -3,8 +3,9 @@ extern crate rocket; -#[cfg(test)] -mod tests; +#[cfg(test)] mod tests; + +use rocket::response::content; #[get("/hello//")] fn hello(name: &str, age: i8) -> String { @@ -12,10 +13,10 @@ fn hello(name: &str, age: i8) -> String { } #[error(404)] -fn not_found(req: &rocket::Request) -> String { - format!("

Sorry, but '{}' is not a valid path!

+fn not_found(req: &rocket::Request) -> content::HTML { + content::HTML(format!("

Sorry, but '{}' is not a valid path!

Try visiting /hello/<name>/<age> instead.

", - req.uri()) + req.uri())) } fn main() { diff --git a/examples/from_request/src/main.rs b/examples/from_request/src/main.rs index cc2ea6c2..40134bea 100644 --- a/examples/from_request/src/main.rs +++ b/examples/from_request/src/main.rs @@ -43,26 +43,21 @@ mod test { let rocket = rocket::ignite() .mount("/", routes![super::header_count]); - let num_headers = headers.len(); let mut req = MockRequest::new(Get, "/"); - for header in headers { + for header in headers.iter().cloned() { req = req.header(header); } let mut response = req.dispatch_with(&rocket); - - let expect = format!("Your request contained {} headers!", num_headers); + let expect = format!("Your request contained {} headers!", headers.len()); assert_eq!(response.body().and_then(|b| b.into_string()), Some(expect)); } #[test] fn test_n_headers() { for i in 0..50 { - let mut headers = vec![]; - for j in 0..i { - let string = format!("{}", j); - headers.push(Header::new(string.clone(), string)); - } + let headers = (0..i).map(|n| Header::new(n.to_string(), n.to_string())) + .collect(); test_header_count(headers); } diff --git a/examples/pastebin/src/main.rs b/examples/pastebin/src/main.rs index 77bbc9eb..e7c08a4e 100644 --- a/examples/pastebin/src/main.rs +++ b/examples/pastebin/src/main.rs @@ -19,13 +19,13 @@ const HOST: &'static str = "http://localhost:8000"; const ID_LENGTH: usize = 3; #[post("/", data = "")] -fn upload(paste: Data) -> io::Result> { +fn upload(paste: Data) -> io::Result { let id = PasteID::new(ID_LENGTH); let filename = format!("upload/{id}", id = id); let url = format!("{host}/{id}\n", host = HOST, id = id); paste.stream_to_file(Path::new(&filename))?; - Ok(content::Plain(url)) + Ok(url) } #[get("/")] diff --git a/lib/src/response/responder.rs b/lib/src/response/responder.rs index 3d090dde..64622b08 100644 --- a/lib/src/response/responder.rs +++ b/lib/src/response/responder.rs @@ -43,7 +43,7 @@ use response::{Response, Stream}; /// /// * **String** /// -/// Sets the `Content-Type`t to `text/html`. The string is used as the body +/// Sets the `Content-Type`t to `text/plain`. The string is used as the body /// of the response, which is fixed size and not streamed. To stream a /// string, use `Stream::from(Cursor::new(string))`. /// @@ -188,12 +188,12 @@ impl<'r> Responder<'r> for &'r str { } } -/// Returns a response with Content-Type `text/html` and a fized-size body +/// Returns a response with Content-Type `text/html` and a fixed-size body /// containing the string `self`. Always returns `Ok`. impl Responder<'static> for String { fn respond(self) -> Result, Status> { Response::build() - .header(ContentType::HTML) + .header(ContentType::Plain) .sized_body(Cursor::new(self)) .ok() } diff --git a/lib/tests/form_value_decoding-issue-82.rs b/lib/tests/form_value_decoding-issue-82.rs index e1c68d39..69e8817c 100644 --- a/lib/tests/form_value_decoding-issue-82.rs +++ b/lib/tests/form_value_decoding-issue-82.rs @@ -25,7 +25,6 @@ mod tests { fn check_decoding(raw: &str, decoded: &str) { let rocket = rocket::ignite().mount("/", routes![bug]); - let mut req = MockRequest::new(Post, "/") .header(ContentType::Form) .body(format!("form_data={}", raw));