Skip to content

Commit

Permalink
Middleware response() is not invoked if there was an error in async h…
Browse files Browse the repository at this point in the history
…andler actix#187
  • Loading branch information
fafhrd91 committed Apr 18, 2018
1 parent 022f980 commit f907be5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# 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)

* Impossible to quote slashes in path parameters #182
Expand Down
2 changes: 2 additions & 0 deletions src/httprequest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,8 @@ impl<S> fmt::Debug for HttpRequest<S> {

#[cfg(test)]
mod tests {
#![allow(deprecated)]

use super::*;
use http::{HttpTryFrom, Uri};
use resource::ResourceHandler;
Expand Down
2 changes: 1 addition & 1 deletion src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ impl<S: 'static, H> WaitingResponse<S, H> {
match self.fut.poll() {
Ok(Async::NotReady) => None,
Ok(Async::Ready(response)) => Some(RunMiddlewares::init(info, response)),
Err(err) => Some(ProcessResponse::init(err.into())),
Err(err) => Some(RunMiddlewares::init(info, err.into())),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ impl<S: 'static> WaitingResponse<S> {
match self.fut.poll() {
Ok(Async::NotReady) => None,
Ok(Async::Ready(response)) => Some(RunMiddlewares::init(info, response)),
Err(err) => Some(Response::init(err.into())),
Err(err) => Some(RunMiddlewares::init(info, err.into())),
}
}
}
Expand Down
33 changes: 32 additions & 1 deletion tests/test_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use flate2::Compression;
use flate2::read::GzDecoder;
use flate2::write::{DeflateDecoder, DeflateEncoder, GzEncoder};
use futures::stream::once;
use futures::{Future, Stream};
use futures::{future, Future, Stream};
use h2::client as h2client;
use modhttp::Request;
use rand::Rng;
Expand Down Expand Up @@ -915,3 +915,34 @@ fn test_resource_middlewares() {
assert_eq!(num2.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);
}

0 comments on commit f907be5

Please sign in to comment.