diff --git a/actix-cors/src/lib.rs b/actix-cors/src/lib.rs index 40f9fdf99..db7e4cc4f 100644 --- a/actix-cors/src/lib.rs +++ b/actix-cors/src/lib.rs @@ -11,7 +11,7 @@ //! use actix_cors::Cors; //! use actix_web::{http, web, App, HttpRequest, HttpResponse, HttpServer}; //! -//! fn index(req: HttpRequest) -> &'static str { +//! async fn index(req: HttpRequest) -> &'static str { //! "Hello world" //! } //! diff --git a/actix-files/src/lib.rs b/actix-files/src/lib.rs index 72db16957..2f8a5c49c 100644 --- a/actix-files/src/lib.rs +++ b/actix-files/src/lib.rs @@ -1176,9 +1176,11 @@ mod tests { let mut srv = test::init_service(App::new().wrap(Compress::default()).service( web::resource("/").to(|| { - NamedFile::open("Cargo.toml") - .unwrap() - .set_content_encoding(header::ContentEncoding::Identity) + async { + NamedFile::open("Cargo.toml") + .unwrap() + .set_content_encoding(header::ContentEncoding::Identity) + } }), )) .await; @@ -1199,9 +1201,11 @@ mod tests { let mut srv = test::init_service(App::new().wrap(Compress::default()).service( web::resource("/").to(|| { - NamedFile::open("Cargo.toml") - .unwrap() - .set_content_encoding(header::ContentEncoding::Gzip) + async { + NamedFile::open("Cargo.toml") + .unwrap() + .set_content_encoding(header::ContentEncoding::Gzip) + } }), )) .await; diff --git a/actix-http/src/message.rs b/actix-http/src/message.rs index 316df2611..5994ed39e 100644 --- a/actix-http/src/message.rs +++ b/actix-http/src/message.rs @@ -388,6 +388,12 @@ impl BoxedResponseHead { pub fn new(status: StatusCode) -> Self { RESPONSE_POOL.with(|p| p.get_message(status)) } + + pub(crate) fn take(&mut self) -> Self { + BoxedResponseHead { + head: self.head.take(), + } + } } impl std::ops::Deref for BoxedResponseHead { @@ -406,7 +412,9 @@ impl std::ops::DerefMut for BoxedResponseHead { impl Drop for BoxedResponseHead { fn drop(&mut self) { - RESPONSE_POOL.with(|p| p.release(self.head.take().unwrap())) + if let Some(head) = self.head.take() { + RESPONSE_POOL.with(move |p| p.release(head)) + } } } diff --git a/actix-http/src/response.rs b/actix-http/src/response.rs index 31876813b..a5f18cc79 100644 --- a/actix-http/src/response.rs +++ b/actix-http/src/response.rs @@ -288,10 +288,7 @@ impl Future for Response { fn poll(mut self: Pin<&mut Self>, _: &mut Context) -> Poll { Poll::Ready(Ok(Response { - head: std::mem::replace( - &mut self.head, - BoxedResponseHead::new(StatusCode::OK), - ), + head: self.head.take(), body: self.body.take_body(), error: self.error.take(), })) diff --git a/actix-identity/src/lib.rs b/actix-identity/src/lib.rs index 30761d872..2980a7753 100644 --- a/actix-identity/src/lib.rs +++ b/actix-identity/src/lib.rs @@ -16,7 +16,7 @@ //! use actix_web::*; //! use actix_identity::{Identity, CookieIdentityPolicy, IdentityService}; //! -//! fn index(id: Identity) -> String { +//! async fn index(id: Identity) -> String { //! // access request identity //! if let Some(id) = id.identity() { //! format!("Welcome! {}", id) @@ -25,12 +25,12 @@ //! } //! } //! -//! fn login(id: Identity) -> HttpResponse { +//! async fn login(id: Identity) -> HttpResponse { //! id.remember("User1".to_owned()); // <- remember identity //! HttpResponse::Ok().finish() //! } //! -//! fn logout(id: Identity) -> HttpResponse { +//! async fn logout(id: Identity) -> HttpResponse { //! id.forget(); // <- remove identity //! HttpResponse::Ok().finish() //! } @@ -764,11 +764,13 @@ mod tests { .secure(false) .name(COOKIE_NAME)))) .service(web::resource("/").to(|id: Identity| { - let identity = id.identity(); - if identity.is_none() { - id.remember(COOKIE_LOGIN.to_string()) + async move { + let identity = id.identity(); + if identity.is_none() { + id.remember(COOKIE_LOGIN.to_string()) + } + web::Json(identity) } - web::Json(identity) })), ) .await diff --git a/actix-session/src/cookie.rs b/actix-session/src/cookie.rs index 9a486cce2..bb5fba97b 100644 --- a/actix-session/src/cookie.rs +++ b/actix-session/src/cookie.rs @@ -371,8 +371,10 @@ mod tests { App::new() .wrap(CookieSession::signed(&[0; 32]).secure(false)) .service(web::resource("/").to(|ses: Session| { - let _ = ses.set("counter", 100); - "test" + async move { + let _ = ses.set("counter", 100); + "test" + } })), ) .await; @@ -394,8 +396,10 @@ mod tests { App::new() .wrap(CookieSession::private(&[0; 32]).secure(false)) .service(web::resource("/").to(|ses: Session| { - let _ = ses.set("counter", 100); - "test" + async move { + let _ = ses.set("counter", 100); + "test" + } })), ) .await; @@ -417,8 +421,10 @@ mod tests { App::new() .wrap(CookieSession::signed(&[0; 32]).secure(false)) .service(web::resource("/").to(|ses: Session| { - let _ = ses.set("counter", 100); - "test" + async move { + let _ = ses.set("counter", 100); + "test" + } })), ) .await; @@ -448,12 +454,16 @@ mod tests { .max_age(100), ) .service(web::resource("/").to(|ses: Session| { - let _ = ses.set("counter", 100); - "test" + async move { + let _ = ses.set("counter", 100); + "test" + } })) .service(web::resource("/test/").to(|ses: Session| { - let val: usize = ses.get("counter").unwrap().unwrap(); - format!("counter: {}", val) + async move { + let val: usize = ses.get("counter").unwrap().unwrap(); + format!("counter: {}", val) + } })), ) .await; diff --git a/actix-web-codegen/tests/test_macro.rs b/actix-web-codegen/tests/test_macro.rs index 953de9cd5..18c01f374 100644 --- a/actix-web-codegen/tests/test_macro.rs +++ b/actix-web-codegen/tests/test_macro.rs @@ -5,42 +5,42 @@ use actix_web_codegen::{connect, delete, get, head, options, patch, post, put, t use futures::{future, Future}; #[get("/test")] -fn test() -> impl Responder { +async fn test() -> impl Responder { HttpResponse::Ok() } #[put("/test")] -fn put_test() -> impl Responder { +async fn put_test() -> impl Responder { HttpResponse::Created() } #[patch("/test")] -fn patch_test() -> impl Responder { +async fn patch_test() -> impl Responder { HttpResponse::Ok() } #[post("/test")] -fn post_test() -> impl Responder { +async fn post_test() -> impl Responder { HttpResponse::NoContent() } #[head("/test")] -fn head_test() -> impl Responder { +async fn head_test() -> impl Responder { HttpResponse::Ok() } #[connect("/test")] -fn connect_test() -> impl Responder { +async fn connect_test() -> impl Responder { HttpResponse::Ok() } #[options("/test")] -fn options_test() -> impl Responder { +async fn options_test() -> impl Responder { HttpResponse::Ok() } #[trace("/test")] -fn trace_test() -> impl Responder { +async fn trace_test() -> impl Responder { HttpResponse::Ok() } @@ -55,17 +55,17 @@ fn auto_sync() -> impl Future> { } #[put("/test/{param}")] -fn put_param_test(_: Path) -> impl Responder { +async fn put_param_test(_: Path) -> impl Responder { HttpResponse::Created() } #[delete("/test/{param}")] -fn delete_param_test(_: Path) -> impl Responder { +async fn delete_param_test(_: Path) -> impl Responder { HttpResponse::NoContent() } #[get("/test/{param}")] -fn get_param_test(_: Path) -> impl Responder { +async fn get_param_test(_: Path) -> impl Responder { HttpResponse::Ok() } diff --git a/awc/tests/test_client.rs b/awc/tests/test_client.rs index 959380306..9e1948f79 100644 --- a/awc/tests/test_client.rs +++ b/awc/tests/test_client.rs @@ -115,14 +115,14 @@ fn test_form() { fn test_timeout() { block_on(async { let srv = TestServer::start(|| { - HttpService::new(App::new().service(web::resource("/").route( - web::to_async(|| { + HttpService::new(App::new().service(web::resource("/").route(web::to( + || { async { tokio_timer::delay_for(Duration::from_millis(200)).await; Ok::<_, Error>(HttpResponse::Ok().body(STR)) } - }), - ))) + }, + )))) }); let connector = awc::Connector::new() @@ -149,14 +149,14 @@ fn test_timeout() { fn test_timeout_override() { block_on(async { let srv = TestServer::start(|| { - HttpService::new(App::new().service(web::resource("/").route( - web::to_async(|| { + HttpService::new(App::new().service(web::resource("/").route(web::to( + || { async { tokio_timer::delay_for(Duration::from_millis(200)).await; Ok::<_, Error>(HttpResponse::Ok().body(STR)) } - }), - ))) + }, + )))) }); let client = awc::Client::build() @@ -693,12 +693,9 @@ fn test_client_brotli_encoding() { #[test] fn test_client_cookie_handling() { + use std::io::{Error as IoError, ErrorKind}; + block_on(async { - fn err() -> Error { - use std::io::{Error as IoError, ErrorKind}; - // stub some generic error - Error::from(IoError::from(ErrorKind::NotFound)) - } let cookie1 = Cookie::build("cookie1", "value1").finish(); let cookie2 = Cookie::build("cookie2", "value2") .domain("www.example.org") @@ -717,31 +714,45 @@ fn test_client_cookie_handling() { HttpService::new(App::new().route( "/", web::to(move |req: HttpRequest| { - // Check cookies were sent correctly - req.cookie("cookie1") - .ok_or_else(err) - .and_then(|c1| { - if c1.value() == "value1" { - Ok(()) - } else { - Err(err()) - } - }) - .and_then(|()| req.cookie("cookie2").ok_or_else(err)) - .and_then(|c2| { - if c2.value() == "value2" { - Ok(()) - } else { - Err(err()) - } - }) - // Send some cookies back - .map(|_| { - HttpResponse::Ok() - .cookie(cookie1.clone()) - .cookie(cookie2.clone()) - .finish() - }) + let cookie1 = cookie1.clone(); + let cookie2 = cookie2.clone(); + + async move { + // Check cookies were sent correctly + let res: Result<(), Error> = req + .cookie("cookie1") + .ok_or(()) + .and_then(|c1| { + if c1.value() == "value1" { + Ok(()) + } else { + Err(()) + } + }) + .and_then(|()| req.cookie("cookie2").ok_or(())) + .and_then(|c2| { + if c2.value() == "value2" { + Ok(()) + } else { + Err(()) + } + }) + .map_err(|_| { + Error::from(IoError::from(ErrorKind::NotFound)) + }); + + if let Err(e) = res { + Err(e) + } else { + // Send some cookies back + Ok::<_, Error>( + HttpResponse::Ok() + .cookie(cookie1) + .cookie(cookie2) + .finish(), + ) + } + } }), )) }); diff --git a/test-server/src/lib.rs b/test-server/src/lib.rs index 1ec69b100..1911c75d6 100644 --- a/test-server/src/lib.rs +++ b/test-server/src/lib.rs @@ -35,7 +35,7 @@ pub use actix_testing::*; /// let mut srv = TestServer::start( /// || HttpService::new( /// App::new().service( -/// web::resource("/").to_async(my_handler)) +/// web::resource("/").to(my_handler)) /// ) /// ); ///