1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-05-20 01:08:10 +00:00

add tests to scope and resource for returning from fns

This commit is contained in:
Rob Ede 2021-12-22 07:58:37 +00:00
parent f8488aff1e
commit 40a0162074
No known key found for this signature in database
GPG key ID: 97C636207D3EF933
3 changed files with 79 additions and 20 deletions

View file

@ -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<impl MessageBody>,
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<impl MessageBody>,
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());
}
}

View file

@ -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(

View file

@ -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 =