Don't overwrite catcher response status if it is set.

Resolves #113.
This commit is contained in:
Sergio Benitez 2017-01-06 01:32:43 -06:00
parent 21a1bde7c0
commit e230ce9b95
2 changed files with 34 additions and 4 deletions

View File

@ -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"));

View File

@ -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!["/"]);
}
}