mirror of https://github.com/rwf2/Rocket.git
parent
21a1bde7c0
commit
e230ce9b95
|
@ -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"));
|
||||
|
||||
|
|
|
@ -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!["/"]);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue