mirror of
https://github.com/fafhrd91/actix-web
synced 2025-02-17 10:13:30 +01:00
add tests to scope and resource for returning from fns
This commit is contained in:
parent
f8488aff1e
commit
40a0162074
36
src/app.rs
36
src/app.rs
@ -709,24 +709,24 @@ mod tests {
|
|||||||
assert_eq!(body, Bytes::from_static(b"https://youtube.com/watch/12345"));
|
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]
|
#[test]
|
||||||
fn return_foreign_app_type() {
|
fn can_be_returned_from_fn() {
|
||||||
let _app = foreign_app_type();
|
/// 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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -505,18 +505,48 @@ mod tests {
|
|||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
use actix_utils::future::ok;
|
use actix_utils::future::ok;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
use crate::{
|
use crate::{
|
||||||
guard,
|
guard,
|
||||||
http::{
|
http::{
|
||||||
header::{self, HeaderValue},
|
header::{self, HeaderValue},
|
||||||
Method, StatusCode,
|
Method, StatusCode,
|
||||||
},
|
},
|
||||||
middleware::DefaultHeaders,
|
middleware::{Compat, DefaultHeaders},
|
||||||
service::{ServiceRequest, ServiceResponse},
|
service::{ServiceRequest, ServiceResponse},
|
||||||
test::{call_service, init_service, TestRequest},
|
test::{call_service, init_service, TestRequest},
|
||||||
web, App, Error, HttpMessage, HttpResponse,
|
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]
|
#[actix_rt::test]
|
||||||
async fn test_middleware() {
|
async fn test_middleware() {
|
||||||
let srv = init_service(
|
let srv = init_service(
|
||||||
|
31
src/scope.rs
31
src/scope.rs
@ -586,18 +586,47 @@ mod tests {
|
|||||||
use actix_utils::future::ok;
|
use actix_utils::future::ok;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
use crate::{
|
use crate::{
|
||||||
guard,
|
guard,
|
||||||
http::{
|
http::{
|
||||||
header::{self, HeaderValue},
|
header::{self, HeaderValue},
|
||||||
Method, StatusCode,
|
Method, StatusCode,
|
||||||
},
|
},
|
||||||
middleware::DefaultHeaders,
|
middleware::{Compat, DefaultHeaders},
|
||||||
service::{ServiceRequest, ServiceResponse},
|
service::{ServiceRequest, ServiceResponse},
|
||||||
test::{assert_body_eq, call_service, init_service, read_body, TestRequest},
|
test::{assert_body_eq, call_service, init_service, read_body, TestRequest},
|
||||||
web, App, HttpMessage, HttpRequest, HttpResponse,
|
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]
|
#[actix_rt::test]
|
||||||
async fn test_scope() {
|
async fn test_scope() {
|
||||||
let srv =
|
let srv =
|
||||||
|
Loading…
x
Reference in New Issue
Block a user