From 3833e0dfd67d81dbd36929f53fb114ad102449aa Mon Sep 17 00:00:00 2001 From: alwayys-afk Date: Fri, 10 Oct 2025 15:00:56 -0400 Subject: [PATCH] Standardize async callback patterns using AsyncFnOnce --- loco-gen/src/templates/controller/api/test.t | 4 +- loco-gen/src/templates/scaffold/api/test.t | 2 +- src/testing/request.rs | 52 ++++++++------------ 3 files changed, 24 insertions(+), 34 deletions(-) diff --git a/loco-gen/src/templates/controller/api/test.t b/loco-gen/src/templates/controller/api/test.t index 9611bfb79..9e4771475 100644 --- a/loco-gen/src/templates/controller/api/test.t +++ b/loco-gen/src/templates/controller/api/test.t @@ -15,7 +15,7 @@ use serial_test::serial; #[tokio::test] #[serial] async fn can_get_{{ name | plural | snake_case }}() { - request::(|request, _ctx| async move { + request::(|request, _ctx| async move { let res = request.get("/api/{{ name | plural | snake_case }}/").await; assert_eq!(res.status_code(), 200); @@ -29,7 +29,7 @@ async fn can_get_{{ name | plural | snake_case }}() { #[tokio::test] #[serial] async fn can_get_{{action}}() { - request::(|request, _ctx| async move { + request::(|request, _ctx| async move { let res = request.get("/{{ name | plural | snake_case }}/{{action}}").await; assert_eq!(res.status_code(), 200); }) diff --git a/loco-gen/src/templates/scaffold/api/test.t b/loco-gen/src/templates/scaffold/api/test.t index f20cfa0c6..fb0876db6 100644 --- a/loco-gen/src/templates/scaffold/api/test.t +++ b/loco-gen/src/templates/scaffold/api/test.t @@ -15,7 +15,7 @@ use serial_test::serial; #[tokio::test] #[serial] async fn can_get_{{ name | plural | snake_case }}() { - request::(|request, _ctx| async move { + request::(|request, _ctx| async move { let res = request.get("/api/{{ name | plural | snake_case }}/").await; assert_eq!(res.status_code(), 200); diff --git a/src/testing/request.rs b/src/testing/request.rs index 29e318da4..fe7ec649d 100644 --- a/src/testing/request.rs +++ b/src/testing/request.rs @@ -256,11 +256,11 @@ pub async fn boot_test_unique_port(port: Option) -> Result(callback: F, boot: &BootResult, test_server_config: RequestConfig) -where - F: FnOnce(TestServer, AppContext) -> Fut, - Fut: std::future::Future, -{ +async fn request_internal( + callback: impl AsyncFnOnce(TestServer, AppContext), + boot: &BootResult, + test_server_config: RequestConfig, +) { let routes = boot.router.clone().unwrap(); let server = TestServer::new_with_config( routes.into_make_service_with_connect_info::(), @@ -292,19 +292,15 @@ where /// #[tokio::test] /// #[serial] /// async fn can_register() { -/// request::(|request, ctx| async move { +/// request::(|request, ctx| async move { /// let response = request.post("/auth/register").json(&serde_json::json!({})).await; /// }) /// .await; /// } /// ``` #[allow(clippy::future_not_send)] -pub async fn request(callback: F) -where - F: FnOnce(TestServer, AppContext) -> Fut, - Fut: std::future::Future, -{ - request_with_config::(RequestConfig::default(), callback).await; +pub async fn request(callback: impl AsyncFnOnce(TestServer, AppContext)) { + request_with_config::(RequestConfig::default(), callback).await; } /// Executes a test server request with a created database using the provided callback. /// @@ -316,7 +312,7 @@ where /// /// #[tokio::test] /// async fn can_register() { -/// request_with_create_db::(|request, ctx| async move { +/// request_with_create_db::(|request, ctx| async move { /// let response = request.post("/auth/register").json(&serde_json::json!({})).await; /// }) /// .await; @@ -328,12 +324,8 @@ where /// initialize the test app #[allow(clippy::future_not_send)] #[cfg(feature = "with-db")] -pub async fn request_with_create_db(callback: F) -where - F: FnOnce(TestServer, AppContext) -> Fut, - Fut: std::future::Future, -{ - request_config_with_create_db::(RequestConfig::default(), callback).await; +pub async fn request_with_create_db(callback: impl AsyncFnOnce(TestServer, AppContext)) { + request_config_with_create_db::(RequestConfig::default(), callback).await; } /// Executes a test server request using a custom [`RequestConfig`]. @@ -352,13 +344,12 @@ where /// let response = request.get("/endpoint").await; /// }); /// ``` -pub async fn request_with_config(config: RequestConfig, callback: F) -where - F: FnOnce(TestServer, AppContext) -> Fut, - Fut: std::future::Future, -{ +pub async fn request_with_config( + config: RequestConfig, + callback: impl AsyncFnOnce(TestServer, AppContext), +) { let boot: BootResult = boot_test::().await.unwrap(); - request_internal::(callback, &boot, config).await; + request_internal(callback, &boot, config).await; } /// Executes a test server request with a created database using a custom [`RequestConfig`]. @@ -379,11 +370,10 @@ where /// ``` #[allow(clippy::future_not_send)] #[cfg(feature = "with-db")] -pub async fn request_config_with_create_db(config: RequestConfig, callback: F) -where - F: FnOnce(TestServer, AppContext) -> Fut, - Fut: std::future::Future, -{ +pub async fn request_config_with_create_db( + config: RequestConfig, + callback: impl AsyncFnOnce(TestServer, AppContext), +) { let boot_wrapper: BootResultWrapper = boot_test_with_create_db::().await.unwrap(); - request_internal::(callback, &boot_wrapper.inner, config).await; + request_internal(callback, &boot_wrapper.inner, config).await; }