From 8ad12d76646982e2e05fe25187973009092923a6 Mon Sep 17 00:00:00 2001 From: Ali MJ Al-Nasrawy Date: Fri, 14 Jan 2022 21:44:02 +0300 Subject: [PATCH] fix test helpers for opaque app --- src/test/test_utils.rs | 46 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/src/test/test_utils.rs b/src/test/test_utils.rs index 02d4c9bf3..18317bca5 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,36 @@ mod tests { assert_eq!(&result.id, "12345"); assert_eq!(&result.name, "User name"); } + + #[actix_rt::test] + 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()); + call_and_read_body(&svc, req.pop().unwrap()); + read_body(call_service(&svc, req.pop().unwrap()).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; + } + } }