From e230ce9b95600ac1a38aaa6b69638dec5c8c444a Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Fri, 6 Jan 2017 01:32:43 -0600 Subject: [PATCH] Don't overwrite catcher response status if it is set. Resolves #113. --- codegen/src/decorators/error.rs | 7 ++--- lib/tests/redirect_from_catcher-issue-113.rs | 31 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 lib/tests/redirect_from_catcher-issue-113.rs diff --git a/codegen/src/decorators/error.rs b/codegen/src/decorators/error.rs index 6b57d5c5..5ab9c47d 100644 --- a/codegen/src/decorators/error.rs +++ b/codegen/src/decorators/error.rs @@ -60,11 +60,10 @@ pub fn error_decorator(ecx: &mut ExtCtxt, sp: Span, meta_item: &MetaItem, fn $catch_fn_name<'_b>($err_ident: ::rocket::Error, $req_ident: &'_b ::rocket::Request) -> ::rocket::response::Result<'_b> { - let response = $user_fn_name($fn_arguments); + let user_response = $user_fn_name($fn_arguments); + let response = ::rocket::response::Responder::respond(user_response)?; let status = ::rocket::http::Status::raw($code); - ::rocket::response::Responder::respond( - ::rocket::response::status::Custom(status, response) - ) + ::rocket::response::Response::build().status(status).merge(response).ok() } ).expect("catch function")); diff --git a/lib/tests/redirect_from_catcher-issue-113.rs b/lib/tests/redirect_from_catcher-issue-113.rs new file mode 100644 index 00000000..79bae319 --- /dev/null +++ b/lib/tests/redirect_from_catcher-issue-113.rs @@ -0,0 +1,31 @@ +#![feature(plugin)] +#![plugin(rocket_codegen)] + +extern crate rocket; + +use rocket::response::Redirect; + +#[error(404)] +fn not_found() -> Redirect { + Redirect::to("/") +} + +#[cfg(feature = "testing")] +mod tests { + use super::*; + use rocket::testing::MockRequest; + use rocket::http::Method::*; + use rocket::http::Status; + + #[test] + fn error_catcher_redirect() { + let rocket = rocket::ignite().catch(errors![not_found]); + let mut req = MockRequest::new(Get, "/unknown"); + let response = req.dispatch_with(&rocket); + println!("Response:\n{:?}", response); + + let location: Vec<_> = response.header_values("location").collect(); + assert_eq!(response.status(), Status::SeeOther); + assert_eq!(location, vec!["/"]); + } +}