From 32742d07155957d9a6a4eed9eae43adede614a16 Mon Sep 17 00:00:00 2001 From: Ali MJ Al-Nasrawy Date: Fri, 14 Jan 2022 22:45:32 +0300 Subject: [PATCH] support opaque app in test helpers (#2584) --- CHANGES.md | 2 ++ src/test/test_utils.rs | 47 +++++++++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a82d0eddc..8ab756100 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,12 +10,14 @@ - `Result` extractor wrapper can now convert error types. [#2581] - Associated types in `FromRequest` impl for `Option` and `Result` has changed. [#2581] - Maximim number of extractors has changed from 10 to 12. [#2582] +- Removed bound `::Error: Debug` in test utility functions in order to support returning opaque apps. [#2584] [#1988]: https://github.com/actix/actix-web/pull/1988 [#2567]: https://github.com/actix/actix-web/pull/2567 [#2569]: https://github.com/actix/actix-web/pull/2569 [#2581]: https://github.com/actix/actix-web/pull/2581 [#2582]: https://github.com/actix/actix-web/pull/2582 +[#2584]: https://github.com/actix/actix-web/pull/2584 ## 4.0.0-beta.19 - 2022-01-04 diff --git a/src/test/test_utils.rs b/src/test/test_utils.rs index 02d4c9bf3..8207ce270 100644 --- a/src/test/test_utils.rs +++ b/src/test/test_utils.rs @@ -1,4 +1,4 @@ -use std::fmt; +use std::error::Error as StdError; use actix_http::Request; use actix_service::IntoServiceFactory; @@ -135,7 +135,6 @@ pub async fn call_and_read_body(app: &S, req: Request) -> Bytes where S: Service, Error = Error>, B: MessageBody, - B::Error: fmt::Debug, { let res = call_service(app, req).await; read_body(res).await @@ -147,7 +146,6 @@ pub async fn read_response(app: &S, req: Request) -> Bytes where S: Service, Error = Error>, B: MessageBody, - B::Error: fmt::Debug, { let res = call_service(app, req).await; read_body(res).await @@ -186,11 +184,11 @@ where pub async fn read_body(res: ServiceResponse) -> Bytes where B: MessageBody, - B::Error: fmt::Debug, { let body = res.into_body(); body::to_bytes(body) .await + .map_err(Into::>::into) .expect("error reading test response body") } @@ -240,7 +238,6 @@ where pub async fn read_body_json(res: ServiceResponse) -> T where B: MessageBody, - B::Error: fmt::Debug, T: DeserializeOwned, { let body = read_body(res).await; @@ -300,7 +297,6 @@ pub async fn call_and_read_body_json(app: &S, req: Request) -> T where S: Service, Error = Error>, B: MessageBody, - B::Error: fmt::Debug, T: DeserializeOwned, { let res = call_service(app, req).await; @@ -313,7 +309,6 @@ pub async fn read_response_json(app: &S, req: Request) -> T where S: Service, Error = Error>, B: MessageBody, - B::Error: fmt::Debug, T: DeserializeOwned, { call_and_read_body_json(app, req).await @@ -325,7 +320,10 @@ mod tests { use serde::{Deserialize, Serialize}; use super::*; - use crate::{http::header, test::TestRequest, web, App, HttpMessage, HttpResponse}; + use crate::{ + dev::ServiceRequest, http::header, test::TestRequest, web, App, HttpMessage, + HttpResponse, + }; #[actix_rt::test] async fn test_request_methods() { @@ -471,4 +469,37 @@ mod tests { assert_eq!(&result.id, "12345"); assert_eq!(&result.name, "User name"); } + + #[actix_rt::test] + #[allow(dead_code)] + async fn return_opaque_types() { + fn test_app() -> App< + impl ServiceFactory< + ServiceRequest, + Config = (), + Response = ServiceResponse, + Error = crate::Error, + InitError = (), + >, + > { + App::new().service(web::resource("/people").route( + web::post().to(|person: web::Json| HttpResponse::Ok().json(person)), + )) + } + + async fn test_service( + ) -> impl Service, Error = crate::Error> + { + init_service(test_app()).await + } + + async fn compile_test(mut req: Vec) { + let svc = test_service().await; + call_service(&svc, req.pop().unwrap()).await; + call_and_read_body(&svc, req.pop().unwrap()).await; + read_body(call_service(&svc, req.pop().unwrap()).await).await; + let _: String = call_and_read_body_json(&svc, req.pop().unwrap()).await; + let _: String = read_body_json(call_service(&svc, req.pop().unwrap()).await).await; + } + } }