From d7c5c966d2271add3babdbb63cbe9aa423c87909 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 24 Jan 2022 11:56:01 +0000 Subject: [PATCH] remove impl Future for HttpResponse (#2601) --- CHANGES.md | 4 ++ actix-files/src/lib.rs | 32 ++++----- actix-web-actors/src/context.rs | 9 ++- awc/tests/test_client.rs | 53 ++++++++------ benches/server.rs | 5 +- src/guard.rs | 12 +++- src/middleware/compress.rs | 2 +- src/resource.rs | 8 +-- src/response/response.rs | 51 ++++++++------ src/types/payload.rs | 6 +- tests/compression.rs | 15 ++-- tests/test_httpserver.rs | 5 +- tests/test_server.rs | 119 +++++++++++++++++--------------- 13 files changed, 182 insertions(+), 139 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index d95bebaaa..8c3997663 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,10 @@ # Changes ## Unreleased - 2021-xx-xx +### Removed +- `impl Future for HttpResponse`. [#2601] + +[#2601]: https://github.com/actix/actix-web/pull/2601 ## 4.0.0-beta.21 - 2022-01-21 diff --git a/actix-files/src/lib.rs b/actix-files/src/lib.rs index 43b06a858..41113f2ab 100644 --- a/actix-files/src/lib.rs +++ b/actix-files/src/lib.rs @@ -106,7 +106,7 @@ mod tests { let req = TestRequest::default() .insert_header((header::IF_MODIFIED_SINCE, since)) .to_http_request(); - let resp = file.respond_to(&req).await.unwrap(); + let resp = file.respond_to(&req); assert_eq!(resp.status(), StatusCode::NOT_MODIFIED); } @@ -118,7 +118,7 @@ mod tests { let req = TestRequest::default() .insert_header((header::IF_MODIFIED_SINCE, since)) .to_http_request(); - let resp = file.respond_to(&req).await.unwrap(); + let resp = file.respond_to(&req); assert_eq!(resp.status(), StatusCode::NOT_MODIFIED); } @@ -131,7 +131,7 @@ mod tests { .insert_header((header::IF_NONE_MATCH, "miss_etag")) .insert_header((header::IF_MODIFIED_SINCE, since)) .to_http_request(); - let resp = file.respond_to(&req).await.unwrap(); + let resp = file.respond_to(&req); assert_ne!(resp.status(), StatusCode::NOT_MODIFIED); } @@ -143,7 +143,7 @@ mod tests { let req = TestRequest::default() .insert_header((header::IF_UNMODIFIED_SINCE, since)) .to_http_request(); - let resp = file.respond_to(&req).await.unwrap(); + let resp = file.respond_to(&req); assert_eq!(resp.status(), StatusCode::OK); } @@ -155,7 +155,7 @@ mod tests { let req = TestRequest::default() .insert_header((header::IF_UNMODIFIED_SINCE, since)) .to_http_request(); - let resp = file.respond_to(&req).await.unwrap(); + let resp = file.respond_to(&req); assert_eq!(resp.status(), StatusCode::PRECONDITION_FAILED); } @@ -172,7 +172,7 @@ mod tests { } let req = TestRequest::default().to_http_request(); - let resp = file.respond_to(&req).await.unwrap(); + let resp = file.respond_to(&req); assert_eq!( resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/x-toml" @@ -196,7 +196,7 @@ mod tests { } let req = TestRequest::default().to_http_request(); - let resp = file.respond_to(&req).await.unwrap(); + let resp = file.respond_to(&req); assert_eq!( resp.headers().get(header::CONTENT_DISPOSITION).unwrap(), "inline; filename=\"Cargo.toml\"" @@ -207,7 +207,7 @@ mod tests { .unwrap() .disable_content_disposition(); let req = TestRequest::default().to_http_request(); - let resp = file.respond_to(&req).await.unwrap(); + let resp = file.respond_to(&req); assert!(resp.headers().get(header::CONTENT_DISPOSITION).is_none()); } @@ -235,7 +235,7 @@ mod tests { } let req = TestRequest::default().to_http_request(); - let resp = file.respond_to(&req).await.unwrap(); + let resp = file.respond_to(&req); assert_eq!( resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/x-toml" @@ -261,7 +261,7 @@ mod tests { } let req = TestRequest::default().to_http_request(); - let resp = file.respond_to(&req).await.unwrap(); + let resp = file.respond_to(&req); assert_eq!( resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/xml" @@ -284,7 +284,7 @@ mod tests { } let req = TestRequest::default().to_http_request(); - let resp = file.respond_to(&req).await.unwrap(); + let resp = file.respond_to(&req); assert_eq!( resp.headers().get(header::CONTENT_TYPE).unwrap(), "image/png" @@ -300,7 +300,7 @@ mod tests { let file = NamedFile::open_async("tests/test.js").await.unwrap(); let req = TestRequest::default().to_http_request(); - let resp = file.respond_to(&req).await.unwrap(); + let resp = file.respond_to(&req); assert_eq!( resp.headers().get(header::CONTENT_TYPE).unwrap(), "application/javascript; charset=utf-8" @@ -330,7 +330,7 @@ mod tests { } let req = TestRequest::default().to_http_request(); - let resp = file.respond_to(&req).await.unwrap(); + let resp = file.respond_to(&req); assert_eq!( resp.headers().get(header::CONTENT_TYPE).unwrap(), "image/png" @@ -353,7 +353,7 @@ mod tests { } let req = TestRequest::default().to_http_request(); - let resp = file.respond_to(&req).await.unwrap(); + let resp = file.respond_to(&req); assert_eq!( resp.headers().get(header::CONTENT_TYPE).unwrap(), "application/octet-stream" @@ -379,7 +379,7 @@ mod tests { } let req = TestRequest::default().to_http_request(); - let resp = file.respond_to(&req).await.unwrap(); + let resp = file.respond_to(&req); assert_eq!( resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/x-toml" @@ -633,7 +633,7 @@ mod tests { async fn test_named_file_allowed_method() { let req = TestRequest::default().method(Method::GET).to_http_request(); let file = NamedFile::open_async("Cargo.toml").await.unwrap(); - let resp = file.respond_to(&req).await.unwrap(); + let resp = file.respond_to(&req); assert_eq!(resp.status(), StatusCode::OK); } diff --git a/actix-web-actors/src/context.rs b/actix-web-actors/src/context.rs index d7459aea4..d83969ff7 100644 --- a/actix-web-actors/src/context.rs +++ b/actix-web-actors/src/context.rs @@ -228,11 +228,10 @@ mod tests { #[actix_rt::test] async fn test_default_resource() { - let srv = - init_service(App::new().service(web::resource("/test").to(|| { - HttpResponse::Ok().streaming(HttpContext::create(MyActor { count: 0 })) - }))) - .await; + let srv = init_service(App::new().service(web::resource("/test").to(|| async { + HttpResponse::Ok().streaming(HttpContext::create(MyActor { count: 0 })) + }))) + .await; let req = TestRequest::with_uri("/test").to_request(); let resp = call_service(&srv, req).await; diff --git a/awc/tests/test_client.rs b/awc/tests/test_client.rs index c1378157b..dceaf467d 100644 --- a/awc/tests/test_client.rs +++ b/awc/tests/test_client.rs @@ -30,7 +30,9 @@ const STR: &str = const_str::repeat!(S, 100); #[actix_rt::test] async fn test_simple() { let srv = actix_test::start(|| { - App::new().service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR)))) + App::new().service( + web::resource("/").route(web::to(|| async { HttpResponse::Ok().body(STR) })), + ) }); let request = srv.get("/").insert_header(("x-test", "111")).send(); @@ -93,7 +95,7 @@ async fn test_timeout() { let srv = actix_test::start(|| { App::new().service(web::resource("/").route(web::to(|| async { actix_rt::time::sleep(Duration::from_millis(200)).await; - Ok::<_, Error>(HttpResponse::Ok().body(STR)) + HttpResponse::Ok().body(STR) }))) }); @@ -118,7 +120,7 @@ async fn test_timeout_override() { let srv = actix_test::start(|| { App::new().service(web::resource("/").route(web::to(|| async { actix_rt::time::sleep(Duration::from_millis(200)).await; - Ok::<_, Error>(HttpResponse::Ok().body(STR)) + HttpResponse::Ok().body(STR) }))) }); @@ -295,10 +297,9 @@ async fn test_connection_server_close() { }) .and_then( HttpService::new(map_config( - App::new().service( - web::resource("/") - .route(web::to(|| HttpResponse::Ok().force_close().finish())), - ), + App::new().service(web::resource("/").route(web::to(|| async { + HttpResponse::Ok().force_close().finish() + }))), |_| AppConfig::default(), )) .tcp(), @@ -336,7 +337,8 @@ async fn test_connection_wait_queue() { .and_then( HttpService::new(map_config( App::new().service( - web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))), + web::resource("/") + .route(web::to(|| async { HttpResponse::Ok().body(STR) })), ), |_| AppConfig::default(), )) @@ -383,10 +385,9 @@ async fn test_connection_wait_queue_force_close() { }) .and_then( HttpService::new(map_config( - App::new().service( - web::resource("/") - .route(web::to(|| HttpResponse::Ok().force_close().body(STR))), - ), + App::new().service(web::resource("/").route(web::to(|| async { + HttpResponse::Ok().force_close().body(STR) + }))), |_| AppConfig::default(), )) .tcp(), @@ -445,7 +446,9 @@ async fn test_no_decompress() { let srv = actix_test::start(|| { App::new() .wrap(actix_web::middleware::Compress::default()) - .service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR)))) + .service( + web::resource("/").route(web::to(|| async { HttpResponse::Ok().body(STR) })), + ) }); let mut res = awc::Client::new() @@ -479,7 +482,7 @@ async fn test_no_decompress() { #[actix_rt::test] async fn test_client_gzip_encoding() { let srv = actix_test::start(|| { - App::new().service(web::resource("/").route(web::to(|| { + App::new().service(web::resource("/").route(web::to(|| async { HttpResponse::Ok() .insert_header(header::ContentEncoding::Gzip) .body(utils::gzip::encode(STR)) @@ -499,7 +502,7 @@ async fn test_client_gzip_encoding() { #[actix_rt::test] async fn test_client_gzip_encoding_large() { let srv = actix_test::start(|| { - App::new().service(web::resource("/").route(web::to(|| { + App::new().service(web::resource("/").route(web::to(|| async { HttpResponse::Ok() .insert_header(header::ContentEncoding::Gzip) .body(utils::gzip::encode(STR.repeat(10))) @@ -525,7 +528,7 @@ async fn test_client_gzip_encoding_large_random() { .collect::(); let srv = actix_test::start(|| { - App::new().service(web::resource("/").route(web::to(|data: Bytes| { + App::new().service(web::resource("/").route(web::to(|data: Bytes| async { HttpResponse::Ok() .insert_header(header::ContentEncoding::Gzip) .body(utils::gzip::encode(data)) @@ -545,7 +548,7 @@ async fn test_client_gzip_encoding_large_random() { #[actix_rt::test] async fn test_client_brotli_encoding() { let srv = actix_test::start(|| { - App::new().service(web::resource("/").route(web::to(|data: Bytes| { + App::new().service(web::resource("/").route(web::to(|data: Bytes| async { HttpResponse::Ok() .insert_header(("content-encoding", "br")) .body(utils::brotli::encode(data)) @@ -571,10 +574,10 @@ async fn test_client_brotli_encoding_large_random() { .collect::(); let srv = actix_test::start(|| { - App::new().service(web::resource("/").route(web::to(|data: Bytes| { + App::new().service(web::resource("/").route(web::to(|data: Bytes| async { HttpResponse::Ok() .insert_header(header::ContentEncoding::Brotli) - .body(utils::brotli::encode(&data)) + .body(utils::brotli::encode(data)) }))) }); @@ -590,7 +593,9 @@ async fn test_client_brotli_encoding_large_random() { #[actix_rt::test] async fn test_client_deflate_encoding() { let srv = actix_test::start(|| { - App::new().default_service(web::to(|body: Bytes| HttpResponse::Ok().body(body))) + App::new().default_service(web::to(|body: Bytes| async { + HttpResponse::Ok().body(body) + })) }); let req = srv @@ -614,7 +619,9 @@ async fn test_client_deflate_encoding_large_random() { .collect::(); let srv = actix_test::start(|| { - App::new().default_service(web::to(|body: Bytes| HttpResponse::Ok().body(body))) + App::new().default_service(web::to(|body: Bytes| async { + HttpResponse::Ok().body(body) + })) }); let req = srv @@ -632,7 +639,7 @@ async fn test_client_deflate_encoding_large_random() { #[actix_rt::test] async fn test_client_streaming_explicit() { let srv = actix_test::start(|| { - App::new().default_service(web::to(|body: web::Payload| { + App::new().default_service(web::to(|body: web::Payload| async { HttpResponse::Ok().streaming(body) })) }); @@ -654,7 +661,7 @@ async fn test_client_streaming_explicit() { #[actix_rt::test] async fn test_body_streaming_implicit() { let srv = actix_test::start(|| { - App::new().default_service(web::to(|| { + App::new().default_service(web::to(|| async { let body = stream::once(async { Ok::<_, Infallible>(Bytes::from_static(STR.as_bytes())) }); HttpResponse::Ok().streaming(body) diff --git a/benches/server.rs b/benches/server.rs index 139e24abd..0d45c9403 100644 --- a/benches/server.rs +++ b/benches/server.rs @@ -33,8 +33,9 @@ fn bench_async_burst(c: &mut Criterion) { let srv = rt.block_on(async { actix_test::start(|| { - App::new() - .service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR)))) + App::new().service( + web::resource("/").route(web::to(|| async { HttpResponse::Ok().body(STR) })), + ) }) }); diff --git a/src/guard.rs b/src/guard.rs index 596b9f9fe..9f7514644 100644 --- a/src/guard.rs +++ b/src/guard.rs @@ -373,7 +373,9 @@ impl Guard for HeaderGuard { /// /// web::scope("/admin") /// .guard(Host("admin.rust-lang.org").scheme("https")) -/// .default_service(web::to(|| HttpResponse::Ok().body("admin connection is secure"))); +/// .default_service(web::to(|| async { +/// HttpResponse::Ok().body("admin connection is secure") +/// })); /// ``` /// /// The `Host` guard can be used to set up some form of [virtual hosting] within a single app. @@ -388,12 +390,16 @@ impl Guard for HeaderGuard { /// .service( /// web::scope("") /// .guard(guard::Host("www.rust-lang.org")) -/// .default_service(web::to(|| HttpResponse::Ok().body("marketing site"))), +/// .default_service(web::to(|| async { +/// HttpResponse::Ok().body("marketing site") +/// })), /// ) /// .service( /// web::scope("") /// .guard(guard::Host("play.rust-lang.org")) -/// .default_service(web::to(|| HttpResponse::Ok().body("playground frontend"))), +/// .default_service(web::to(|| async { +/// HttpResponse::Ok().body("playground frontend") +/// })), /// ); /// ``` /// diff --git a/src/middleware/compress.rs b/src/middleware/compress.rs index 16af4c2cd..4fdd74779 100644 --- a/src/middleware/compress.rs +++ b/src/middleware/compress.rs @@ -52,7 +52,7 @@ use crate::{ /// /// let app = App::new() /// .wrap(middleware::Compress::default()) -/// .default_service(web::to(|| HttpResponse::Ok().body("hello world"))); +/// .default_service(web::to(|| async { HttpResponse::Ok().body("hello world") })); /// ``` /// /// Pre-compressed Gzip file being served from disk with correct headers added to bypass middleware: diff --git a/src/resource.rs b/src/resource.rs index dd7d4b0d5..a0fc19faf 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -206,10 +206,10 @@ where /// Register a new route and add handler. This route matches all requests. /// /// ``` - /// use actix_web::*; + /// use actix_web::{App, HttpRequest, HttpResponse, web}; /// - /// fn index(req: HttpRequest) -> HttpResponse { - /// unimplemented!() + /// async fn index(req: HttpRequest) -> HttpResponse { + /// todo!() /// } /// /// App::new().service(web::resource("/").to(index)); @@ -219,7 +219,7 @@ where /// /// ``` /// # use actix_web::*; - /// # fn index(req: HttpRequest) -> HttpResponse { unimplemented!() } + /// # async fn index(req: HttpRequest) -> HttpResponse { todo!() } /// App::new().service(web::resource("/").route(web::route().to(index))); /// ``` pub fn to(mut self, handler: F) -> Self diff --git a/src/response/response.rs b/src/response/response.rs index 6a326fa61..0469c5ce9 100644 --- a/src/response/response.rs +++ b/src/response/response.rs @@ -1,10 +1,6 @@ use std::{ cell::{Ref, RefMut}, fmt, - future::Future, - mem, - pin::Pin, - task::{Context, Poll}, }; use actix_http::{ @@ -337,24 +333,39 @@ impl From> for Response { } } -// Future is only implemented for BoxBody payload type because it's the most useful for making -// simple handlers without async blocks. Making it generic over all MessageBody types requires a -// future impl on Response which would cause it's body field to be, undesirably, Option. -// -// This impl is not particularly efficient due to the Response construction and should probably -// not be invoked if performance is important. Prefer an async fn/block in such cases. -impl Future for HttpResponse { - type Output = Result, Error>; +// Rationale for cfg(test): this impl causes false positives on a clippy lint (async_yields_async) +// when returning an HttpResponse from an async function/closure and it's not very useful outside of +// tests anyway. +#[cfg(test)] +mod response_fut_impl { + use std::{ + future::Future, + mem, + pin::Pin, + task::{Context, Poll}, + }; - fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll { - if let Some(err) = self.error.take() { - return Poll::Ready(Err(err)); + use super::*; + + // Future is only implemented for BoxBody payload type because it's the most useful for making + // simple handlers without async blocks. Making it generic over all MessageBody types requires a + // future impl on Response which would cause it's body field to be, undesirably, Option. + // + // This impl is not particularly efficient due to the Response construction and should probably + // not be invoked if performance is important. Prefer an async fn/block in such cases. + impl Future for HttpResponse { + type Output = Result, Error>; + + fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll { + if let Some(err) = self.error.take() { + return Poll::Ready(Err(err)); + } + + Poll::Ready(Ok(mem::replace( + &mut self.res, + Response::new(StatusCode::default()), + ))) } - - Poll::Ready(Ok(mem::replace( - &mut self.res, - Response::new(StatusCode::default()), - ))) } } diff --git a/src/types/payload.rs b/src/types/payload.rs index d2ab29639..b47a39e97 100644 --- a/src/types/payload.rs +++ b/src/types/payload.rs @@ -219,7 +219,7 @@ impl PayloadConfig { } } - /// Set maximum accepted payload size in bytes. The default limit is 256kB. + /// Set maximum accepted payload size in bytes. The default limit is 256KiB. pub fn limit(mut self, limit: usize) -> Self { self.limit = limit; self @@ -261,14 +261,14 @@ impl PayloadConfig { } } +const DEFAULT_CONFIG_LIMIT: usize = 262_144; // 2^18 bytes (~256kB) + /// Allow shared refs used as defaults. const DEFAULT_CONFIG: PayloadConfig = PayloadConfig { limit: DEFAULT_CONFIG_LIMIT, mimetype: None, }; -const DEFAULT_CONFIG_LIMIT: usize = 262_144; // 2^18 bytes (~256kB) - impl Default for PayloadConfig { fn default() -> Self { DEFAULT_CONFIG.clone() diff --git a/tests/compression.rs b/tests/compression.rs index 88c462f60..b911b9d1f 100644 --- a/tests/compression.rs +++ b/tests/compression.rs @@ -19,10 +19,13 @@ macro_rules! test_server { actix_test::start(|| { App::new() .wrap(Compress::default()) - .route("/static", web::to(|| HttpResponse::Ok().body(LOREM))) + .route( + "/static", + web::to(|| async { HttpResponse::Ok().body(LOREM) }), + ) .route( "/static-gzip", - web::to(|| { + web::to(|| async { HttpResponse::Ok() // signal to compressor that content should not be altered // signal to client that content is encoded @@ -32,7 +35,7 @@ macro_rules! test_server { ) .route( "/static-br", - web::to(|| { + web::to(|| async { HttpResponse::Ok() // signal to compressor that content should not be altered // signal to client that content is encoded @@ -42,7 +45,7 @@ macro_rules! test_server { ) .route( "/static-zstd", - web::to(|| { + web::to(|| async { HttpResponse::Ok() // signal to compressor that content should not be altered // signal to client that content is encoded @@ -52,7 +55,7 @@ macro_rules! test_server { ) .route( "/static-xz", - web::to(|| { + web::to(|| async { HttpResponse::Ok() // signal to compressor that content should not be altered // signal to client that content is encoded as 7zip @@ -62,7 +65,7 @@ macro_rules! test_server { ) .route( "/echo", - web::to(|body: Bytes| HttpResponse::Ok().body(body)), + web::to(|body: Bytes| async move { HttpResponse::Ok().body(body) }), ) }) }; diff --git a/tests/test_httpserver.rs b/tests/test_httpserver.rs index 464a650a2..6ea8e520c 100644 --- a/tests/test_httpserver.rs +++ b/tests/test_httpserver.rs @@ -18,7 +18,8 @@ async fn test_start() { .block_on(async { let srv = HttpServer::new(|| { App::new().service( - web::resource("/").route(web::to(|| HttpResponse::Ok().body("test"))), + web::resource("/") + .route(web::to(|| async { HttpResponse::Ok().body("test") })), ) }) .workers(1) @@ -93,7 +94,7 @@ async fn test_start_ssl() { let srv = HttpServer::new(|| { App::new().service(web::resource("/").route(web::to(|req: HttpRequest| { assert!(req.app_config().secure()); - HttpResponse::Ok().body("test") + async { HttpResponse::Ok().body("test") } }))) }) .workers(1) diff --git a/tests/test_server.rs b/tests/test_server.rs index ade48a485..b8193a004 100644 --- a/tests/test_server.rs +++ b/tests/test_server.rs @@ -93,7 +93,9 @@ impl futures_core::stream::Stream for TestBody { #[actix_rt::test] async fn test_body() { let srv = actix_test::start(|| { - App::new().service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR)))) + App::new().service( + web::resource("/").route(web::to(|| async { HttpResponse::Ok().body(STR) })), + ) }); let mut res = srv.get("/").send().await.unwrap(); @@ -160,9 +162,12 @@ async fn body_gzip_large() { let srv = actix_test::start_with(actix_test::config().h1(), move || { let data = srv_data.clone(); - App::new().wrap(Compress::default()).service( - web::resource("/").route(web::to(move || HttpResponse::Ok().body(data.clone()))), - ) + App::new() + .wrap(Compress::default()) + .service(web::resource("/").route(web::to(move || { + let data = data.clone(); + async move { HttpResponse::Ok().body(data.clone()) } + }))) }); let mut res = srv @@ -191,9 +196,12 @@ async fn test_body_gzip_large_random() { let srv = actix_test::start_with(actix_test::config().h1(), move || { let data = srv_data.clone(); - App::new().wrap(Compress::default()).service( - web::resource("/").route(web::to(move || HttpResponse::Ok().body(data.clone()))), - ) + App::new() + .wrap(Compress::default()) + .service(web::resource("/").route(web::to(move || { + let data = data.clone(); + async move { HttpResponse::Ok().body(data.clone()) } + }))) }); let mut res = srv @@ -216,7 +224,7 @@ async fn test_body_chunked_implicit() { let srv = actix_test::start_with(actix_test::config().h1(), || { App::new() .wrap(Compress::default()) - .service(web::resource("/").route(web::get().to(move || { + .service(web::resource("/").route(web::get().to(|| async { HttpResponse::Ok() .streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24)) }))) @@ -246,7 +254,7 @@ async fn test_body_br_streaming() { let srv = actix_test::start_with(actix_test::config().h1(), || { App::new() .wrap(Compress::default()) - .service(web::resource("/").route(web::to(move || { + .service(web::resource("/").route(web::to(|| async { HttpResponse::Ok() .streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24)) }))) @@ -271,7 +279,8 @@ async fn test_body_br_streaming() { async fn test_head_binary() { let srv = actix_test::start_with(actix_test::config().h1(), || { App::new().service( - web::resource("/").route(web::head().to(move || HttpResponse::Ok().body(STR))), + web::resource("/") + .route(web::head().to(move || async { HttpResponse::Ok().body(STR) })), ) }); @@ -290,7 +299,7 @@ async fn test_head_binary() { #[actix_rt::test] async fn test_no_chunking() { let srv = actix_test::start_with(actix_test::config().h1(), || { - App::new().service(web::resource("/").route(web::to(move || { + App::new().service(web::resource("/").route(web::to(move || async { HttpResponse::Ok() .no_chunking(STR.len() as u64) .streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24)) @@ -310,9 +319,9 @@ async fn test_no_chunking() { #[actix_rt::test] async fn test_body_deflate() { let srv = actix_test::start_with(actix_test::config().h1(), || { - App::new() - .wrap(Compress::default()) - .service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR)))) + App::new().wrap(Compress::default()).service( + web::resource("/").route(web::to(move || async { HttpResponse::Ok().body(STR) })), + ) }); let mut res = srv @@ -333,9 +342,9 @@ async fn test_body_deflate() { #[actix_rt::test] async fn test_body_brotli() { let srv = actix_test::start_with(actix_test::config().h1(), || { - App::new() - .wrap(Compress::default()) - .service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR)))) + App::new().wrap(Compress::default()).service( + web::resource("/").route(web::to(move || async { HttpResponse::Ok().body(STR) })), + ) }); let mut res = srv @@ -356,9 +365,9 @@ async fn test_body_brotli() { #[actix_rt::test] async fn test_body_zstd() { let srv = actix_test::start_with(actix_test::config().h1(), || { - App::new() - .wrap(Compress::default()) - .service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR)))) + App::new().wrap(Compress::default()).service( + web::resource("/").route(web::to(move || async { HttpResponse::Ok().body(STR) })), + ) }); let mut res = srv @@ -381,7 +390,7 @@ async fn test_body_zstd_streaming() { let srv = actix_test::start_with(actix_test::config().h1(), || { App::new() .wrap(Compress::default()) - .service(web::resource("/").route(web::to(move || { + .service(web::resource("/").route(web::to(move || async { HttpResponse::Ok() .streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24)) }))) @@ -405,9 +414,9 @@ async fn test_body_zstd_streaming() { #[actix_rt::test] async fn test_zstd_encoding() { let srv = actix_test::start_with(actix_test::config().h1(), || { - App::new().service( - web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))), - ) + App::new().service(web::resource("/").route(web::to(move |body: Bytes| async { + HttpResponse::Ok().body(body) + }))) }); let request = srv @@ -435,7 +444,7 @@ async fn test_zstd_encoding_large() { App::new().service( web::resource("/") .app_data(web::PayloadConfig::new(320_000)) - .route(web::to(move |body: Bytes| { + .route(web::to(move |body: Bytes| async { HttpResponse::Ok().streaming(TestBody::new(body, 10240)) })), ) @@ -457,9 +466,11 @@ async fn test_zstd_encoding_large() { #[actix_rt::test] async fn test_encoding() { let srv = actix_test::start_with(actix_test::config().h1(), || { - App::new().wrap(Compress::default()).service( - web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))), - ) + App::new() + .wrap(Compress::default()) + .service(web::resource("/").route(web::to(move |body: Bytes| async { + HttpResponse::Ok().body(body) + }))) }); let request = srv @@ -478,9 +489,9 @@ async fn test_encoding() { #[actix_rt::test] async fn test_gzip_encoding() { let srv = actix_test::start_with(actix_test::config().h1(), || { - App::new().service( - web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))), - ) + App::new().service(web::resource("/").route(web::to(move |body: Bytes| async { + HttpResponse::Ok().body(body) + }))) }); let request = srv @@ -500,9 +511,9 @@ async fn test_gzip_encoding() { async fn test_gzip_encoding_large() { let data = STR.repeat(10); let srv = actix_test::start_with(actix_test::config().h1(), || { - App::new().service( - web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))), - ) + App::new().service(web::resource("/").route(web::to(move |body: Bytes| async { + HttpResponse::Ok().body(body) + }))) }); let req = srv @@ -527,9 +538,9 @@ async fn test_reading_gzip_encoding_large_random() { .collect::(); let srv = actix_test::start_with(actix_test::config().h1(), || { - App::new().service( - web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))), - ) + App::new().service(web::resource("/").route(web::to(move |body: Bytes| async { + HttpResponse::Ok().body(body) + }))) }); let request = srv @@ -548,9 +559,9 @@ async fn test_reading_gzip_encoding_large_random() { #[actix_rt::test] async fn test_reading_deflate_encoding() { let srv = actix_test::start_with(actix_test::config().h1(), || { - App::new().service( - web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))), - ) + App::new().service(web::resource("/").route(web::to(move |body: Bytes| async { + HttpResponse::Ok().body(body) + }))) }); let request = srv @@ -570,9 +581,9 @@ async fn test_reading_deflate_encoding() { async fn test_reading_deflate_encoding_large() { let data = STR.repeat(10); let srv = actix_test::start_with(actix_test::config().h1(), || { - App::new().service( - web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))), - ) + App::new().service(web::resource("/").route(web::to(move |body: Bytes| async { + HttpResponse::Ok().body(body) + }))) }); let request = srv @@ -597,9 +608,9 @@ async fn test_reading_deflate_encoding_large_random() { .collect::(); let srv = actix_test::start_with(actix_test::config().h1(), || { - App::new().service( - web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))), - ) + App::new().service(web::resource("/").route(web::to(move |body: Bytes| async { + HttpResponse::Ok().body(body) + }))) }); let request = srv @@ -619,9 +630,9 @@ async fn test_reading_deflate_encoding_large_random() { #[actix_rt::test] async fn test_brotli_encoding() { let srv = actix_test::start_with(actix_test::config().h1(), || { - App::new().service( - web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))), - ) + App::new().service(web::resource("/").route(web::to(move |body: Bytes| async { + HttpResponse::Ok().body(body) + }))) }); let request = srv @@ -649,7 +660,7 @@ async fn test_brotli_encoding_large() { App::new().service( web::resource("/") .app_data(web::PayloadConfig::new(320_000)) - .route(web::to(move |body: Bytes| { + .route(web::to(move |body: Bytes| async { HttpResponse::Ok().streaming(TestBody::new(body, 10240)) })), ) @@ -676,7 +687,7 @@ async fn test_brotli_encoding_large_openssl() { let data = STR.repeat(10); let srv = actix_test::start_with(actix_test::config().openssl(openssl_config()), move || { - App::new().service(web::resource("/").route(web::to(|bytes: Bytes| { + App::new().service(web::resource("/").route(web::to(|bytes: Bytes| async { // echo decompressed request body back in response HttpResponse::Ok() .insert_header(header::ContentEncoding::Identity) @@ -738,7 +749,7 @@ mod plus_rustls { .collect::(); let srv = actix_test::start_with(actix_test::config().rustls(tls_config()), || { - App::new().service(web::resource("/").route(web::to(|bytes: Bytes| { + App::new().service(web::resource("/").route(web::to(|bytes: Bytes| async { // echo decompressed request body back in response HttpResponse::Ok() .insert_header(header::ContentEncoding::Identity) @@ -770,7 +781,7 @@ async fn test_server_cookies() { use actix_web::http; let srv = actix_test::start(|| { - App::new().default_service(web::to(|| { + App::new().default_service(web::to(|| async { HttpResponse::Ok() .cookie( Cookie::build("first", "first_value") @@ -911,7 +922,7 @@ async fn test_accept_encoding_no_match() { let srv = actix_test::start_with(actix_test::config().h1(), || { App::new() .wrap(Compress::default()) - .service(web::resource("/").route(web::to(move || HttpResponse::Ok().finish()))) + .service(web::resource("/").route(web::to(HttpResponse::Ok))) }); let mut res = srv