1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-20 08:31:09 +00:00

Middleware response() is not invoked if there was an error in async handler #187

This commit is contained in:
Nikolay Kim 2018-04-18 14:15:53 -07:00
parent 022f9800ed
commit f907be585e
5 changed files with 41 additions and 3 deletions

View file

@ -1,5 +1,10 @@
# Changes # Changes
## 0.5.4 (2018-04-xx)
* Middleware response() is not invoked if there was an error in async handler #187
## 0.5.3 (2018-04-18) ## 0.5.3 (2018-04-18)
* Impossible to quote slashes in path parameters #182 * Impossible to quote slashes in path parameters #182

View file

@ -590,6 +590,8 @@ impl<S> fmt::Debug for HttpRequest<S> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#![allow(deprecated)]
use super::*; use super::*;
use http::{HttpTryFrom, Uri}; use http::{HttpTryFrom, Uri};
use resource::ResourceHandler; use resource::ResourceHandler;

View file

@ -328,7 +328,7 @@ impl<S: 'static, H> WaitingResponse<S, H> {
match self.fut.poll() { match self.fut.poll() {
Ok(Async::NotReady) => None, Ok(Async::NotReady) => None,
Ok(Async::Ready(response)) => Some(RunMiddlewares::init(info, response)), Ok(Async::Ready(response)) => Some(RunMiddlewares::init(info, response)),
Err(err) => Some(ProcessResponse::init(err.into())), Err(err) => Some(RunMiddlewares::init(info, err.into())),
} }
} }
} }

View file

@ -420,7 +420,7 @@ impl<S: 'static> WaitingResponse<S> {
match self.fut.poll() { match self.fut.poll() {
Ok(Async::NotReady) => None, Ok(Async::NotReady) => None,
Ok(Async::Ready(response)) => Some(RunMiddlewares::init(info, response)), Ok(Async::Ready(response)) => Some(RunMiddlewares::init(info, response)),
Err(err) => Some(Response::init(err.into())), Err(err) => Some(RunMiddlewares::init(info, err.into())),
} }
} }
} }

View file

@ -18,7 +18,7 @@ use flate2::Compression;
use flate2::read::GzDecoder; use flate2::read::GzDecoder;
use flate2::write::{DeflateDecoder, DeflateEncoder, GzEncoder}; use flate2::write::{DeflateDecoder, DeflateEncoder, GzEncoder};
use futures::stream::once; use futures::stream::once;
use futures::{Future, Stream}; use futures::{future, Future, Stream};
use h2::client as h2client; use h2::client as h2client;
use modhttp::Request; use modhttp::Request;
use rand::Rng; use rand::Rng;
@ -915,3 +915,34 @@ fn test_resource_middlewares() {
assert_eq!(num2.load(Ordering::Relaxed), 1); assert_eq!(num2.load(Ordering::Relaxed), 1);
// assert_eq!(num3.load(Ordering::Relaxed), 1); // assert_eq!(num3.load(Ordering::Relaxed), 1);
} }
fn index_test_middleware_async_error(_: HttpRequest) -> FutureResponse<HttpResponse> {
future::result(Err(error::ErrorBadRequest("TEST"))).responder()
}
#[test]
fn test_middleware_async_error() {
let req = Arc::new(AtomicUsize::new(0));
let resp = Arc::new(AtomicUsize::new(0));
let fin = Arc::new(AtomicUsize::new(0));
let act_req = Arc::clone(&req);
let act_resp = Arc::clone(&resp);
let act_fin = Arc::clone(&fin);
let mut srv = test::TestServer::new(move |app| {
app.middleware(MiddlewareTest {
start: Arc::clone(&act_req),
response: Arc::clone(&act_resp),
finish: Arc::clone(&act_fin),
}).handler(index_test_middleware_async_error)});
let request = srv.get().finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), http::StatusCode::BAD_REQUEST);
assert_eq!(req.load(Ordering::Relaxed), 1);
assert_eq!(resp.load(Ordering::Relaxed), 1);
assert_eq!(fin.load(Ordering::Relaxed), 1);
}