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

remove impl Future for HttpResponse (#2601)

This commit is contained in:
Rob Ede 2022-01-24 11:56:01 +00:00 committed by GitHub
parent 50894e392e
commit d7c5c966d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 182 additions and 139 deletions

View File

@ -1,6 +1,10 @@
# Changes # Changes
## Unreleased - 2021-xx-xx ## 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 ## 4.0.0-beta.21 - 2022-01-21

View File

@ -106,7 +106,7 @@ mod tests {
let req = TestRequest::default() let req = TestRequest::default()
.insert_header((header::IF_MODIFIED_SINCE, since)) .insert_header((header::IF_MODIFIED_SINCE, since))
.to_http_request(); .to_http_request();
let resp = file.respond_to(&req).await.unwrap(); let resp = file.respond_to(&req);
assert_eq!(resp.status(), StatusCode::NOT_MODIFIED); assert_eq!(resp.status(), StatusCode::NOT_MODIFIED);
} }
@ -118,7 +118,7 @@ mod tests {
let req = TestRequest::default() let req = TestRequest::default()
.insert_header((header::IF_MODIFIED_SINCE, since)) .insert_header((header::IF_MODIFIED_SINCE, since))
.to_http_request(); .to_http_request();
let resp = file.respond_to(&req).await.unwrap(); let resp = file.respond_to(&req);
assert_eq!(resp.status(), StatusCode::NOT_MODIFIED); 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_NONE_MATCH, "miss_etag"))
.insert_header((header::IF_MODIFIED_SINCE, since)) .insert_header((header::IF_MODIFIED_SINCE, since))
.to_http_request(); .to_http_request();
let resp = file.respond_to(&req).await.unwrap(); let resp = file.respond_to(&req);
assert_ne!(resp.status(), StatusCode::NOT_MODIFIED); assert_ne!(resp.status(), StatusCode::NOT_MODIFIED);
} }
@ -143,7 +143,7 @@ mod tests {
let req = TestRequest::default() let req = TestRequest::default()
.insert_header((header::IF_UNMODIFIED_SINCE, since)) .insert_header((header::IF_UNMODIFIED_SINCE, since))
.to_http_request(); .to_http_request();
let resp = file.respond_to(&req).await.unwrap(); let resp = file.respond_to(&req);
assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.status(), StatusCode::OK);
} }
@ -155,7 +155,7 @@ mod tests {
let req = TestRequest::default() let req = TestRequest::default()
.insert_header((header::IF_UNMODIFIED_SINCE, since)) .insert_header((header::IF_UNMODIFIED_SINCE, since))
.to_http_request(); .to_http_request();
let resp = file.respond_to(&req).await.unwrap(); let resp = file.respond_to(&req);
assert_eq!(resp.status(), StatusCode::PRECONDITION_FAILED); assert_eq!(resp.status(), StatusCode::PRECONDITION_FAILED);
} }
@ -172,7 +172,7 @@ mod tests {
} }
let req = TestRequest::default().to_http_request(); let req = TestRequest::default().to_http_request();
let resp = file.respond_to(&req).await.unwrap(); let resp = file.respond_to(&req);
assert_eq!( assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(), resp.headers().get(header::CONTENT_TYPE).unwrap(),
"text/x-toml" "text/x-toml"
@ -196,7 +196,7 @@ mod tests {
} }
let req = TestRequest::default().to_http_request(); let req = TestRequest::default().to_http_request();
let resp = file.respond_to(&req).await.unwrap(); let resp = file.respond_to(&req);
assert_eq!( assert_eq!(
resp.headers().get(header::CONTENT_DISPOSITION).unwrap(), resp.headers().get(header::CONTENT_DISPOSITION).unwrap(),
"inline; filename=\"Cargo.toml\"" "inline; filename=\"Cargo.toml\""
@ -207,7 +207,7 @@ mod tests {
.unwrap() .unwrap()
.disable_content_disposition(); .disable_content_disposition();
let req = TestRequest::default().to_http_request(); 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()); assert!(resp.headers().get(header::CONTENT_DISPOSITION).is_none());
} }
@ -235,7 +235,7 @@ mod tests {
} }
let req = TestRequest::default().to_http_request(); let req = TestRequest::default().to_http_request();
let resp = file.respond_to(&req).await.unwrap(); let resp = file.respond_to(&req);
assert_eq!( assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(), resp.headers().get(header::CONTENT_TYPE).unwrap(),
"text/x-toml" "text/x-toml"
@ -261,7 +261,7 @@ mod tests {
} }
let req = TestRequest::default().to_http_request(); let req = TestRequest::default().to_http_request();
let resp = file.respond_to(&req).await.unwrap(); let resp = file.respond_to(&req);
assert_eq!( assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(), resp.headers().get(header::CONTENT_TYPE).unwrap(),
"text/xml" "text/xml"
@ -284,7 +284,7 @@ mod tests {
} }
let req = TestRequest::default().to_http_request(); let req = TestRequest::default().to_http_request();
let resp = file.respond_to(&req).await.unwrap(); let resp = file.respond_to(&req);
assert_eq!( assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(), resp.headers().get(header::CONTENT_TYPE).unwrap(),
"image/png" "image/png"
@ -300,7 +300,7 @@ mod tests {
let file = NamedFile::open_async("tests/test.js").await.unwrap(); let file = NamedFile::open_async("tests/test.js").await.unwrap();
let req = TestRequest::default().to_http_request(); let req = TestRequest::default().to_http_request();
let resp = file.respond_to(&req).await.unwrap(); let resp = file.respond_to(&req);
assert_eq!( assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(), resp.headers().get(header::CONTENT_TYPE).unwrap(),
"application/javascript; charset=utf-8" "application/javascript; charset=utf-8"
@ -330,7 +330,7 @@ mod tests {
} }
let req = TestRequest::default().to_http_request(); let req = TestRequest::default().to_http_request();
let resp = file.respond_to(&req).await.unwrap(); let resp = file.respond_to(&req);
assert_eq!( assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(), resp.headers().get(header::CONTENT_TYPE).unwrap(),
"image/png" "image/png"
@ -353,7 +353,7 @@ mod tests {
} }
let req = TestRequest::default().to_http_request(); let req = TestRequest::default().to_http_request();
let resp = file.respond_to(&req).await.unwrap(); let resp = file.respond_to(&req);
assert_eq!( assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(), resp.headers().get(header::CONTENT_TYPE).unwrap(),
"application/octet-stream" "application/octet-stream"
@ -379,7 +379,7 @@ mod tests {
} }
let req = TestRequest::default().to_http_request(); let req = TestRequest::default().to_http_request();
let resp = file.respond_to(&req).await.unwrap(); let resp = file.respond_to(&req);
assert_eq!( assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(), resp.headers().get(header::CONTENT_TYPE).unwrap(),
"text/x-toml" "text/x-toml"
@ -633,7 +633,7 @@ mod tests {
async fn test_named_file_allowed_method() { async fn test_named_file_allowed_method() {
let req = TestRequest::default().method(Method::GET).to_http_request(); let req = TestRequest::default().method(Method::GET).to_http_request();
let file = NamedFile::open_async("Cargo.toml").await.unwrap(); 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); assert_eq!(resp.status(), StatusCode::OK);
} }

View File

@ -228,8 +228,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
async fn test_default_resource() { async fn test_default_resource() {
let srv = let srv = init_service(App::new().service(web::resource("/test").to(|| async {
init_service(App::new().service(web::resource("/test").to(|| {
HttpResponse::Ok().streaming(HttpContext::create(MyActor { count: 0 })) HttpResponse::Ok().streaming(HttpContext::create(MyActor { count: 0 }))
}))) })))
.await; .await;

View File

@ -30,7 +30,9 @@ const STR: &str = const_str::repeat!(S, 100);
#[actix_rt::test] #[actix_rt::test]
async fn test_simple() { async fn test_simple() {
let srv = actix_test::start(|| { 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(); let request = srv.get("/").insert_header(("x-test", "111")).send();
@ -93,7 +95,7 @@ async fn test_timeout() {
let srv = actix_test::start(|| { let srv = actix_test::start(|| {
App::new().service(web::resource("/").route(web::to(|| async { App::new().service(web::resource("/").route(web::to(|| async {
actix_rt::time::sleep(Duration::from_millis(200)).await; 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(|| { let srv = actix_test::start(|| {
App::new().service(web::resource("/").route(web::to(|| async { App::new().service(web::resource("/").route(web::to(|| async {
actix_rt::time::sleep(Duration::from_millis(200)).await; 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( .and_then(
HttpService::new(map_config( HttpService::new(map_config(
App::new().service( App::new().service(web::resource("/").route(web::to(|| async {
web::resource("/") HttpResponse::Ok().force_close().finish()
.route(web::to(|| HttpResponse::Ok().force_close().finish())), }))),
),
|_| AppConfig::default(), |_| AppConfig::default(),
)) ))
.tcp(), .tcp(),
@ -336,7 +337,8 @@ async fn test_connection_wait_queue() {
.and_then( .and_then(
HttpService::new(map_config( HttpService::new(map_config(
App::new().service( App::new().service(
web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))), web::resource("/")
.route(web::to(|| async { HttpResponse::Ok().body(STR) })),
), ),
|_| AppConfig::default(), |_| AppConfig::default(),
)) ))
@ -383,10 +385,9 @@ async fn test_connection_wait_queue_force_close() {
}) })
.and_then( .and_then(
HttpService::new(map_config( HttpService::new(map_config(
App::new().service( App::new().service(web::resource("/").route(web::to(|| async {
web::resource("/") HttpResponse::Ok().force_close().body(STR)
.route(web::to(|| HttpResponse::Ok().force_close().body(STR))), }))),
),
|_| AppConfig::default(), |_| AppConfig::default(),
)) ))
.tcp(), .tcp(),
@ -445,7 +446,9 @@ async fn test_no_decompress() {
let srv = actix_test::start(|| { let srv = actix_test::start(|| {
App::new() App::new()
.wrap(actix_web::middleware::Compress::default()) .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() let mut res = awc::Client::new()
@ -479,7 +482,7 @@ async fn test_no_decompress() {
#[actix_rt::test] #[actix_rt::test]
async fn test_client_gzip_encoding() { async fn test_client_gzip_encoding() {
let srv = actix_test::start(|| { let srv = actix_test::start(|| {
App::new().service(web::resource("/").route(web::to(|| { App::new().service(web::resource("/").route(web::to(|| async {
HttpResponse::Ok() HttpResponse::Ok()
.insert_header(header::ContentEncoding::Gzip) .insert_header(header::ContentEncoding::Gzip)
.body(utils::gzip::encode(STR)) .body(utils::gzip::encode(STR))
@ -499,7 +502,7 @@ async fn test_client_gzip_encoding() {
#[actix_rt::test] #[actix_rt::test]
async fn test_client_gzip_encoding_large() { async fn test_client_gzip_encoding_large() {
let srv = actix_test::start(|| { let srv = actix_test::start(|| {
App::new().service(web::resource("/").route(web::to(|| { App::new().service(web::resource("/").route(web::to(|| async {
HttpResponse::Ok() HttpResponse::Ok()
.insert_header(header::ContentEncoding::Gzip) .insert_header(header::ContentEncoding::Gzip)
.body(utils::gzip::encode(STR.repeat(10))) .body(utils::gzip::encode(STR.repeat(10)))
@ -525,7 +528,7 @@ async fn test_client_gzip_encoding_large_random() {
.collect::<String>(); .collect::<String>();
let srv = actix_test::start(|| { 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() HttpResponse::Ok()
.insert_header(header::ContentEncoding::Gzip) .insert_header(header::ContentEncoding::Gzip)
.body(utils::gzip::encode(data)) .body(utils::gzip::encode(data))
@ -545,7 +548,7 @@ async fn test_client_gzip_encoding_large_random() {
#[actix_rt::test] #[actix_rt::test]
async fn test_client_brotli_encoding() { async fn test_client_brotli_encoding() {
let srv = actix_test::start(|| { 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() HttpResponse::Ok()
.insert_header(("content-encoding", "br")) .insert_header(("content-encoding", "br"))
.body(utils::brotli::encode(data)) .body(utils::brotli::encode(data))
@ -571,10 +574,10 @@ async fn test_client_brotli_encoding_large_random() {
.collect::<String>(); .collect::<String>();
let srv = actix_test::start(|| { 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() HttpResponse::Ok()
.insert_header(header::ContentEncoding::Brotli) .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] #[actix_rt::test]
async fn test_client_deflate_encoding() { async fn test_client_deflate_encoding() {
let srv = actix_test::start(|| { 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 let req = srv
@ -614,7 +619,9 @@ async fn test_client_deflate_encoding_large_random() {
.collect::<String>(); .collect::<String>();
let srv = actix_test::start(|| { 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 let req = srv
@ -632,7 +639,7 @@ async fn test_client_deflate_encoding_large_random() {
#[actix_rt::test] #[actix_rt::test]
async fn test_client_streaming_explicit() { async fn test_client_streaming_explicit() {
let srv = actix_test::start(|| { 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) HttpResponse::Ok().streaming(body)
})) }))
}); });
@ -654,7 +661,7 @@ async fn test_client_streaming_explicit() {
#[actix_rt::test] #[actix_rt::test]
async fn test_body_streaming_implicit() { async fn test_body_streaming_implicit() {
let srv = actix_test::start(|| { let srv = actix_test::start(|| {
App::new().default_service(web::to(|| { App::new().default_service(web::to(|| async {
let body = let body =
stream::once(async { Ok::<_, Infallible>(Bytes::from_static(STR.as_bytes())) }); stream::once(async { Ok::<_, Infallible>(Bytes::from_static(STR.as_bytes())) });
HttpResponse::Ok().streaming(body) HttpResponse::Ok().streaming(body)

View File

@ -33,8 +33,9 @@ fn bench_async_burst(c: &mut Criterion) {
let srv = rt.block_on(async { let srv = rt.block_on(async {
actix_test::start(|| { actix_test::start(|| {
App::new() App::new().service(
.service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR)))) web::resource("/").route(web::to(|| async { HttpResponse::Ok().body(STR) })),
)
}) })
}); });

View File

@ -373,7 +373,9 @@ impl Guard for HeaderGuard {
/// ///
/// web::scope("/admin") /// web::scope("/admin")
/// .guard(Host("admin.rust-lang.org").scheme("https")) /// .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. /// 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( /// .service(
/// web::scope("") /// web::scope("")
/// .guard(guard::Host("www.rust-lang.org")) /// .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( /// .service(
/// web::scope("") /// web::scope("")
/// .guard(guard::Host("play.rust-lang.org")) /// .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")
/// })),
/// ); /// );
/// ``` /// ```
/// ///

View File

@ -52,7 +52,7 @@ use crate::{
/// ///
/// let app = App::new() /// let app = App::new()
/// .wrap(middleware::Compress::default()) /// .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: /// Pre-compressed Gzip file being served from disk with correct headers added to bypass middleware:

View File

@ -206,10 +206,10 @@ where
/// Register a new route and add handler. This route matches all requests. /// 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 { /// async fn index(req: HttpRequest) -> HttpResponse {
/// unimplemented!() /// todo!()
/// } /// }
/// ///
/// App::new().service(web::resource("/").to(index)); /// App::new().service(web::resource("/").to(index));
@ -219,7 +219,7 @@ where
/// ///
/// ``` /// ```
/// # use actix_web::*; /// # 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))); /// App::new().service(web::resource("/").route(web::route().to(index)));
/// ``` /// ```
pub fn to<F, Args>(mut self, handler: F) -> Self pub fn to<F, Args>(mut self, handler: F) -> Self

View File

@ -1,10 +1,6 @@
use std::{ use std::{
cell::{Ref, RefMut}, cell::{Ref, RefMut},
fmt, fmt,
future::Future,
mem,
pin::Pin,
task::{Context, Poll},
}; };
use actix_http::{ use actix_http::{
@ -337,13 +333,27 @@ impl<B> From<HttpResponse<B>> for Response<B> {
} }
} }
// Future is only implemented for BoxBody payload type because it's the most useful for making // Rationale for cfg(test): this impl causes false positives on a clippy lint (async_yields_async)
// simple handlers without async blocks. Making it generic over all MessageBody types requires a // when returning an HttpResponse from an async function/closure and it's not very useful outside of
// future impl on Response which would cause it's body field to be, undesirably, Option<B>. // tests anyway.
// #[cfg(test)]
// This impl is not particularly efficient due to the Response construction and should probably mod response_fut_impl {
// not be invoked if performance is important. Prefer an async fn/block in such cases. use std::{
impl Future for HttpResponse<BoxBody> { future::Future,
mem,
pin::Pin,
task::{Context, Poll},
};
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<B>.
//
// 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<BoxBody> {
type Output = Result<Response<BoxBody>, Error>; type Output = Result<Response<BoxBody>, Error>;
fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
@ -356,6 +366,7 @@ impl Future for HttpResponse<BoxBody> {
Response::new(StatusCode::default()), Response::new(StatusCode::default()),
))) )))
} }
}
} }
impl<B> Responder for HttpResponse<B> impl<B> Responder for HttpResponse<B>

View File

@ -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 { pub fn limit(mut self, limit: usize) -> Self {
self.limit = limit; self.limit = limit;
self self
@ -261,14 +261,14 @@ impl PayloadConfig {
} }
} }
const DEFAULT_CONFIG_LIMIT: usize = 262_144; // 2^18 bytes (~256kB)
/// Allow shared refs used as defaults. /// Allow shared refs used as defaults.
const DEFAULT_CONFIG: PayloadConfig = PayloadConfig { const DEFAULT_CONFIG: PayloadConfig = PayloadConfig {
limit: DEFAULT_CONFIG_LIMIT, limit: DEFAULT_CONFIG_LIMIT,
mimetype: None, mimetype: None,
}; };
const DEFAULT_CONFIG_LIMIT: usize = 262_144; // 2^18 bytes (~256kB)
impl Default for PayloadConfig { impl Default for PayloadConfig {
fn default() -> Self { fn default() -> Self {
DEFAULT_CONFIG.clone() DEFAULT_CONFIG.clone()

View File

@ -19,10 +19,13 @@ macro_rules! test_server {
actix_test::start(|| { actix_test::start(|| {
App::new() App::new()
.wrap(Compress::default()) .wrap(Compress::default())
.route("/static", web::to(|| HttpResponse::Ok().body(LOREM))) .route(
"/static",
web::to(|| async { HttpResponse::Ok().body(LOREM) }),
)
.route( .route(
"/static-gzip", "/static-gzip",
web::to(|| { web::to(|| async {
HttpResponse::Ok() HttpResponse::Ok()
// signal to compressor that content should not be altered // signal to compressor that content should not be altered
// signal to client that content is encoded // signal to client that content is encoded
@ -32,7 +35,7 @@ macro_rules! test_server {
) )
.route( .route(
"/static-br", "/static-br",
web::to(|| { web::to(|| async {
HttpResponse::Ok() HttpResponse::Ok()
// signal to compressor that content should not be altered // signal to compressor that content should not be altered
// signal to client that content is encoded // signal to client that content is encoded
@ -42,7 +45,7 @@ macro_rules! test_server {
) )
.route( .route(
"/static-zstd", "/static-zstd",
web::to(|| { web::to(|| async {
HttpResponse::Ok() HttpResponse::Ok()
// signal to compressor that content should not be altered // signal to compressor that content should not be altered
// signal to client that content is encoded // signal to client that content is encoded
@ -52,7 +55,7 @@ macro_rules! test_server {
) )
.route( .route(
"/static-xz", "/static-xz",
web::to(|| { web::to(|| async {
HttpResponse::Ok() HttpResponse::Ok()
// signal to compressor that content should not be altered // signal to compressor that content should not be altered
// signal to client that content is encoded as 7zip // signal to client that content is encoded as 7zip
@ -62,7 +65,7 @@ macro_rules! test_server {
) )
.route( .route(
"/echo", "/echo",
web::to(|body: Bytes| HttpResponse::Ok().body(body)), web::to(|body: Bytes| async move { HttpResponse::Ok().body(body) }),
) )
}) })
}; };

View File

@ -18,7 +18,8 @@ async fn test_start() {
.block_on(async { .block_on(async {
let srv = HttpServer::new(|| { let srv = HttpServer::new(|| {
App::new().service( App::new().service(
web::resource("/").route(web::to(|| HttpResponse::Ok().body("test"))), web::resource("/")
.route(web::to(|| async { HttpResponse::Ok().body("test") })),
) )
}) })
.workers(1) .workers(1)
@ -93,7 +94,7 @@ async fn test_start_ssl() {
let srv = HttpServer::new(|| { let srv = HttpServer::new(|| {
App::new().service(web::resource("/").route(web::to(|req: HttpRequest| { App::new().service(web::resource("/").route(web::to(|req: HttpRequest| {
assert!(req.app_config().secure()); assert!(req.app_config().secure());
HttpResponse::Ok().body("test") async { HttpResponse::Ok().body("test") }
}))) })))
}) })
.workers(1) .workers(1)

View File

@ -93,7 +93,9 @@ impl futures_core::stream::Stream for TestBody {
#[actix_rt::test] #[actix_rt::test]
async fn test_body() { async fn test_body() {
let srv = actix_test::start(|| { 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(); 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 srv = actix_test::start_with(actix_test::config().h1(), move || {
let data = srv_data.clone(); let data = srv_data.clone();
App::new().wrap(Compress::default()).service( App::new()
web::resource("/").route(web::to(move || HttpResponse::Ok().body(data.clone()))), .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 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 srv = actix_test::start_with(actix_test::config().h1(), move || {
let data = srv_data.clone(); let data = srv_data.clone();
App::new().wrap(Compress::default()).service( App::new()
web::resource("/").route(web::to(move || HttpResponse::Ok().body(data.clone()))), .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 let mut res = srv
@ -216,7 +224,7 @@ async fn test_body_chunked_implicit() {
let srv = actix_test::start_with(actix_test::config().h1(), || { let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new() App::new()
.wrap(Compress::default()) .wrap(Compress::default())
.service(web::resource("/").route(web::get().to(move || { .service(web::resource("/").route(web::get().to(|| async {
HttpResponse::Ok() HttpResponse::Ok()
.streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24)) .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(), || { let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new() App::new()
.wrap(Compress::default()) .wrap(Compress::default())
.service(web::resource("/").route(web::to(move || { .service(web::resource("/").route(web::to(|| async {
HttpResponse::Ok() HttpResponse::Ok()
.streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24)) .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() { async fn test_head_binary() {
let srv = actix_test::start_with(actix_test::config().h1(), || { let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().service( 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] #[actix_rt::test]
async fn test_no_chunking() { async fn test_no_chunking() {
let srv = actix_test::start_with(actix_test::config().h1(), || { 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() HttpResponse::Ok()
.no_chunking(STR.len() as u64) .no_chunking(STR.len() as u64)
.streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24)) .streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24))
@ -310,9 +319,9 @@ async fn test_no_chunking() {
#[actix_rt::test] #[actix_rt::test]
async fn test_body_deflate() { async fn test_body_deflate() {
let srv = actix_test::start_with(actix_test::config().h1(), || { let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new() App::new().wrap(Compress::default()).service(
.wrap(Compress::default()) web::resource("/").route(web::to(move || async { HttpResponse::Ok().body(STR) })),
.service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR)))) )
}); });
let mut res = srv let mut res = srv
@ -333,9 +342,9 @@ async fn test_body_deflate() {
#[actix_rt::test] #[actix_rt::test]
async fn test_body_brotli() { async fn test_body_brotli() {
let srv = actix_test::start_with(actix_test::config().h1(), || { let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new() App::new().wrap(Compress::default()).service(
.wrap(Compress::default()) web::resource("/").route(web::to(move || async { HttpResponse::Ok().body(STR) })),
.service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR)))) )
}); });
let mut res = srv let mut res = srv
@ -356,9 +365,9 @@ async fn test_body_brotli() {
#[actix_rt::test] #[actix_rt::test]
async fn test_body_zstd() { async fn test_body_zstd() {
let srv = actix_test::start_with(actix_test::config().h1(), || { let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new() App::new().wrap(Compress::default()).service(
.wrap(Compress::default()) web::resource("/").route(web::to(move || async { HttpResponse::Ok().body(STR) })),
.service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR)))) )
}); });
let mut res = srv let mut res = srv
@ -381,7 +390,7 @@ async fn test_body_zstd_streaming() {
let srv = actix_test::start_with(actix_test::config().h1(), || { let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new() App::new()
.wrap(Compress::default()) .wrap(Compress::default())
.service(web::resource("/").route(web::to(move || { .service(web::resource("/").route(web::to(move || async {
HttpResponse::Ok() HttpResponse::Ok()
.streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24)) .streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24))
}))) })))
@ -405,9 +414,9 @@ async fn test_body_zstd_streaming() {
#[actix_rt::test] #[actix_rt::test]
async fn test_zstd_encoding() { async fn test_zstd_encoding() {
let srv = actix_test::start_with(actix_test::config().h1(), || { let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().service( App::new().service(web::resource("/").route(web::to(move |body: Bytes| async {
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))), HttpResponse::Ok().body(body)
) })))
}); });
let request = srv let request = srv
@ -435,7 +444,7 @@ async fn test_zstd_encoding_large() {
App::new().service( App::new().service(
web::resource("/") web::resource("/")
.app_data(web::PayloadConfig::new(320_000)) .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)) HttpResponse::Ok().streaming(TestBody::new(body, 10240))
})), })),
) )
@ -457,9 +466,11 @@ async fn test_zstd_encoding_large() {
#[actix_rt::test] #[actix_rt::test]
async fn test_encoding() { async fn test_encoding() {
let srv = actix_test::start_with(actix_test::config().h1(), || { let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().wrap(Compress::default()).service( App::new()
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))), .wrap(Compress::default())
) .service(web::resource("/").route(web::to(move |body: Bytes| async {
HttpResponse::Ok().body(body)
})))
}); });
let request = srv let request = srv
@ -478,9 +489,9 @@ async fn test_encoding() {
#[actix_rt::test] #[actix_rt::test]
async fn test_gzip_encoding() { async fn test_gzip_encoding() {
let srv = actix_test::start_with(actix_test::config().h1(), || { let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().service( App::new().service(web::resource("/").route(web::to(move |body: Bytes| async {
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))), HttpResponse::Ok().body(body)
) })))
}); });
let request = srv let request = srv
@ -500,9 +511,9 @@ async fn test_gzip_encoding() {
async fn test_gzip_encoding_large() { async fn test_gzip_encoding_large() {
let data = STR.repeat(10); let data = STR.repeat(10);
let srv = actix_test::start_with(actix_test::config().h1(), || { let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().service( App::new().service(web::resource("/").route(web::to(move |body: Bytes| async {
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))), HttpResponse::Ok().body(body)
) })))
}); });
let req = srv let req = srv
@ -527,9 +538,9 @@ async fn test_reading_gzip_encoding_large_random() {
.collect::<String>(); .collect::<String>();
let srv = actix_test::start_with(actix_test::config().h1(), || { let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().service( App::new().service(web::resource("/").route(web::to(move |body: Bytes| async {
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))), HttpResponse::Ok().body(body)
) })))
}); });
let request = srv let request = srv
@ -548,9 +559,9 @@ async fn test_reading_gzip_encoding_large_random() {
#[actix_rt::test] #[actix_rt::test]
async fn test_reading_deflate_encoding() { async fn test_reading_deflate_encoding() {
let srv = actix_test::start_with(actix_test::config().h1(), || { let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().service( App::new().service(web::resource("/").route(web::to(move |body: Bytes| async {
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))), HttpResponse::Ok().body(body)
) })))
}); });
let request = srv let request = srv
@ -570,9 +581,9 @@ async fn test_reading_deflate_encoding() {
async fn test_reading_deflate_encoding_large() { async fn test_reading_deflate_encoding_large() {
let data = STR.repeat(10); let data = STR.repeat(10);
let srv = actix_test::start_with(actix_test::config().h1(), || { let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().service( App::new().service(web::resource("/").route(web::to(move |body: Bytes| async {
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))), HttpResponse::Ok().body(body)
) })))
}); });
let request = srv let request = srv
@ -597,9 +608,9 @@ async fn test_reading_deflate_encoding_large_random() {
.collect::<String>(); .collect::<String>();
let srv = actix_test::start_with(actix_test::config().h1(), || { let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().service( App::new().service(web::resource("/").route(web::to(move |body: Bytes| async {
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))), HttpResponse::Ok().body(body)
) })))
}); });
let request = srv let request = srv
@ -619,9 +630,9 @@ async fn test_reading_deflate_encoding_large_random() {
#[actix_rt::test] #[actix_rt::test]
async fn test_brotli_encoding() { async fn test_brotli_encoding() {
let srv = actix_test::start_with(actix_test::config().h1(), || { let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().service( App::new().service(web::resource("/").route(web::to(move |body: Bytes| async {
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))), HttpResponse::Ok().body(body)
) })))
}); });
let request = srv let request = srv
@ -649,7 +660,7 @@ async fn test_brotli_encoding_large() {
App::new().service( App::new().service(
web::resource("/") web::resource("/")
.app_data(web::PayloadConfig::new(320_000)) .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)) HttpResponse::Ok().streaming(TestBody::new(body, 10240))
})), })),
) )
@ -676,7 +687,7 @@ async fn test_brotli_encoding_large_openssl() {
let data = STR.repeat(10); let data = STR.repeat(10);
let srv = let srv =
actix_test::start_with(actix_test::config().openssl(openssl_config()), move || { 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 // echo decompressed request body back in response
HttpResponse::Ok() HttpResponse::Ok()
.insert_header(header::ContentEncoding::Identity) .insert_header(header::ContentEncoding::Identity)
@ -738,7 +749,7 @@ mod plus_rustls {
.collect::<String>(); .collect::<String>();
let srv = actix_test::start_with(actix_test::config().rustls(tls_config()), || { 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 // echo decompressed request body back in response
HttpResponse::Ok() HttpResponse::Ok()
.insert_header(header::ContentEncoding::Identity) .insert_header(header::ContentEncoding::Identity)
@ -770,7 +781,7 @@ async fn test_server_cookies() {
use actix_web::http; use actix_web::http;
let srv = actix_test::start(|| { let srv = actix_test::start(|| {
App::new().default_service(web::to(|| { App::new().default_service(web::to(|| async {
HttpResponse::Ok() HttpResponse::Ok()
.cookie( .cookie(
Cookie::build("first", "first_value") 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(), || { let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new() App::new()
.wrap(Compress::default()) .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 let mut res = srv