1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-23 16:21:06 +01:00

add async handler test with blocking call

This commit is contained in:
Nikolay Kim 2019-04-29 09:45:37 -07:00
parent f4b4875cb1
commit d2c1791067
2 changed files with 20 additions and 1 deletions

View File

@ -4,7 +4,6 @@
* Add helper function for executing futures `test::block_fn()`
### Changed
* Allow to construct `Data` instances to avoid double `Arc` for `Send + Sync` types.

View File

@ -640,4 +640,24 @@ mod tests {
let result: Person = read_response_json(&mut app, req);
assert_eq!(&result.id, "12345");
}
#[test]
fn test_async_with_block() {
fn async_with_block() -> impl Future<Item = HttpResponse, Error = Error> {
web::block(move || Some(4).ok_or("wrong")).then(|res| match res {
Ok(value) => HttpResponse::Ok()
.content_type("text/plain")
.body(format!("Async with block value: {}", value)),
Err(_) => panic!("Unexpected"),
})
}
let mut app = init_service(
App::new().service(web::resource("/index.html").to_async(async_with_block)),
);
let req = TestRequest::post().uri("/index.html").to_request();
let res = block_on(app.call(req)).unwrap();
assert!(res.status().is_success());
}
}