From 8434a98d5c37e8889cad76fbd5426736e476f3b6 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Thu, 23 May 2019 17:16:59 -0700 Subject: [PATCH] Set Content-Type on 'MsgPack' responses. Fixes #1009. --- contrib/lib/src/msgpack.rs | 10 ++++------ examples/msgpack/src/tests.rs | 1 + 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/contrib/lib/src/msgpack.rs b/contrib/lib/src/msgpack.rs index 62b90fbe..8be0fe9f 100644 --- a/contrib/lib/src/msgpack.rs +++ b/contrib/lib/src/msgpack.rs @@ -16,13 +16,13 @@ extern crate serde; extern crate rmp_serde; +use std::io::Read; use std::ops::{Deref, DerefMut}; -use std::io::{Cursor, Read}; use rocket::request::Request; use rocket::outcome::Outcome::*; use rocket::data::{Outcome, Transform, Transform::*, Transformed, Data, FromData}; -use rocket::response::{self, Responder, Response}; +use rocket::response::{self, Responder, content}; use rocket::http::Status; use self::serde::Serialize; @@ -153,14 +153,12 @@ impl<'a, T: Deserialize<'a>> FromData<'a> for MsgPack { /// Content-Type `MsgPack` and a fixed-size body with the serialization. If /// serialization fails, an `Err` of `Status::InternalServerError` is returned. impl Responder<'static> for MsgPack { - fn respond_to(self, _: &Request) -> response::Result<'static> { + fn respond_to(self, req: &Request) -> response::Result<'static> { rmp_serde::to_vec(&self.0).map_err(|e| { error_!("MsgPack failed to serialize: {:?}", e); Status::InternalServerError }).and_then(|buf| { - Response::build() - .sized_body(Cursor::new(buf)) - .ok() + content::MsgPack(buf).respond_to(req) }) } } diff --git a/examples/msgpack/src/tests.rs b/examples/msgpack/src/tests.rs index de508ce4..188e0a20 100644 --- a/examples/msgpack/src/tests.rs +++ b/examples/msgpack/src/tests.rs @@ -13,6 +13,7 @@ fn msgpack_get() { let client = Client::new(rocket()).unwrap(); let mut res = client.get("/message/1").header(ContentType::MsgPack).dispatch(); assert_eq!(res.status(), Status::Ok); + assert_eq!(res.content_type(), Some(ContentType::MsgPack)); // Check that the message is `[1, "Hello, world!"]` assert_eq!(&res.body_bytes().unwrap(),