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

support opaque app in test helpers (#2584)

This commit is contained in:
Ali MJ Al-Nasrawy 2022-01-14 22:45:32 +03:00 committed by GitHub
parent d90c1a2331
commit 32742d0715
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 8 deletions

View File

@ -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 `<B as MessageBody>::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

View File

@ -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<S, B>(app: &S, req: Request) -> Bytes
where
S: Service<Request, Response = ServiceResponse<B>, 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<S, B>(app: &S, req: Request) -> Bytes
where
S: Service<Request, Response = ServiceResponse<B>, 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<B>(res: ServiceResponse<B>) -> Bytes
where
B: MessageBody,
B::Error: fmt::Debug,
{
let body = res.into_body();
body::to_bytes(body)
.await
.map_err(Into::<Box<dyn StdError>>::into)
.expect("error reading test response body")
}
@ -240,7 +238,6 @@ where
pub async fn read_body_json<T, B>(res: ServiceResponse<B>) -> 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<S, B, T>(app: &S, req: Request) -> T
where
S: Service<Request, Response = ServiceResponse<B>, 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<S, B, T>(app: &S, req: Request) -> T
where
S: Service<Request, Response = ServiceResponse<B>, 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<impl MessageBody>,
Error = crate::Error,
InitError = (),
>,
> {
App::new().service(web::resource("/people").route(
web::post().to(|person: web::Json<Person>| HttpResponse::Ok().json(person)),
))
}
async fn test_service(
) -> impl Service<Request, Response = ServiceResponse<impl MessageBody>, Error = crate::Error>
{
init_service(test_app()).await
}
async fn compile_test(mut req: Vec<Request>) {
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;
}
}
}