Rocket

A web framework for Rust. https://rocket.rs (mirror)
git clone git://git.thc420.xyz/Rocket
Log | Files | Refs | README

commit 8434a98d5c37e8889cad76fbd5426736e476f3b6
parent 6bf751fb226016cee7efda4056a1f9dfcbbea339
Author: Sergio Benitez <sb@sergio.bz>
Date:   Thu, 23 May 2019 17:16:59 -0700

Set Content-Type on 'MsgPack' responses.

Fixes #1009.

Diffstat:
Mcontrib/lib/src/msgpack.rs | 10++++------
Mexamples/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 @@ -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<T> { /// Content-Type `MsgPack` and a fixed-size body with the serialization. If /// serialization fails, an `Err` of `Status::InternalServerError` is returned. impl<T: Serialize> Responder<'static> for MsgPack<T> { - 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 @@ -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(),