From 40a016207461d2ec2167f5614709da85994216e3 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Wed, 22 Dec 2021 07:58:37 +0000 Subject: [PATCH] add tests to scope and resource for returning from fns --- src/app.rs | 36 ++++++++++++++++++------------------ src/resource.rs | 32 +++++++++++++++++++++++++++++++- src/scope.rs | 31 ++++++++++++++++++++++++++++++- 3 files changed, 79 insertions(+), 20 deletions(-) diff --git a/src/app.rs b/src/app.rs index 6bccc1ff1..b4b952734 100644 --- a/src/app.rs +++ b/src/app.rs @@ -709,24 +709,24 @@ mod tests { assert_eq!(body, Bytes::from_static(b"https://youtube.com/watch/12345")); } - /// compile-only test for returning app type from function - pub fn foreign_app_type() -> App< - impl ServiceFactory< - ServiceRequest, - Response = ServiceResponse, - Config = (), - InitError = (), - Error = Error, - >, - > { - App::new() - // logger can be removed without affecting the return type - .wrap(crate::middleware::Logger::default()) - .route("/", web::to(|| async { "hello" })) - } - #[test] - fn return_foreign_app_type() { - let _app = foreign_app_type(); + fn can_be_returned_from_fn() { + /// compile-only test for returning app type from function + pub fn my_app() -> App< + impl ServiceFactory< + ServiceRequest, + Response = ServiceResponse, + Config = (), + InitError = (), + Error = Error, + >, + > { + App::new() + // logger can be removed without affecting the return type + .wrap(crate::middleware::Logger::default()) + .route("/", web::to(|| async { "hello" })) + } + + let _ = init_service(my_app()); } } diff --git a/src/resource.rs b/src/resource.rs index 53104930a..c13544063 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -505,18 +505,48 @@ mod tests { use actix_service::Service; use actix_utils::future::ok; + use super::*; use crate::{ guard, http::{ header::{self, HeaderValue}, Method, StatusCode, }, - middleware::DefaultHeaders, + middleware::{Compat, DefaultHeaders}, service::{ServiceRequest, ServiceResponse}, test::{call_service, init_service, TestRequest}, web, App, Error, HttpMessage, HttpResponse, }; + #[test] + fn can_be_returned_from_fn() { + fn my_resource() -> Resource { + web::resource("/test").route(web::get().to(|| async { "hello" })) + } + + fn my_compat_resource() -> Resource< + impl ServiceFactory< + ServiceRequest, + Config = (), + Response = ServiceResponse, + Error = Error, + InitError = (), + >, + > { + web::resource("/test-compat") + // .wrap_fn(|req, srv| { + // let fut = srv.call(req); + // async { Ok(fut.await?.map_into_right_body::<()>()) } + // }) + .wrap(Compat::new(DefaultHeaders::new())) + .route(web::get().to(|| async { "hello" })) + } + + App::new() + .service(my_resource()) + .service(my_compat_resource()); + } + #[actix_rt::test] async fn test_middleware() { let srv = init_service( diff --git a/src/scope.rs b/src/scope.rs index c35584770..35bbb50ba 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -586,18 +586,47 @@ mod tests { use actix_utils::future::ok; use bytes::Bytes; + use super::*; use crate::{ guard, http::{ header::{self, HeaderValue}, Method, StatusCode, }, - middleware::DefaultHeaders, + middleware::{Compat, DefaultHeaders}, service::{ServiceRequest, ServiceResponse}, test::{assert_body_eq, call_service, init_service, read_body, TestRequest}, web, App, HttpMessage, HttpRequest, HttpResponse, }; + #[test] + fn can_be_returned_from_fn() { + fn my_scope() -> Scope { + web::scope("/test") + .service(web::resource("").route(web::get().to(|| async { "hello" }))) + } + + fn my_compat_scope() -> Scope< + impl ServiceFactory< + ServiceRequest, + Config = (), + Response = ServiceResponse, + Error = Error, + InitError = (), + >, + > { + web::scope("/test-compat") + // .wrap_fn(|req, srv| { + // let fut = srv.call(req); + // async { Ok(fut.await?.map_into_right_body::<()>()) } + // }) + .wrap(Compat::new(DefaultHeaders::new())) + .service(web::resource("").route(web::get().to(|| async { "hello" }))) + } + + App::new().service(my_scope()).service(my_compat_scope()); + } + #[actix_rt::test] async fn test_scope() { let srv =