mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-30 10:42:55 +01:00
allow any body type in Scope (#2523)
This commit is contained in:
parent
cd025f5c0b
commit
7b1512d863
@ -2,8 +2,10 @@
|
|||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
### Changed
|
### Changed
|
||||||
|
- No longer require `Scope` service body type to be boxed. [#2523]
|
||||||
- No longer require `Resource` service body type to be boxed. [#2526]
|
- No longer require `Resource` service body type to be boxed. [#2526]
|
||||||
|
|
||||||
|
[#2523]: https://github.com/actix/actix-web/pull/2523
|
||||||
[#2526]: https://github.com/actix/actix-web/pull/2526
|
[#2526]: https://github.com/actix/actix-web/pull/2526
|
||||||
|
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
/// Middleware for enabling any middleware to be used in [`Resource::wrap`](crate::Resource::wrap),
|
/// Middleware for enabling any middleware to be used in [`Resource::wrap`](crate::Resource::wrap),
|
||||||
/// [`Scope::wrap`](crate::Scope::wrap) and [`Condition`](super::Condition).
|
/// and [`Condition`](super::Condition).
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```
|
/// ```
|
||||||
|
37
src/scope.rs
37
src/scope.rs
@ -1,6 +1,9 @@
|
|||||||
use std::{cell::RefCell, fmt, future::Future, marker::PhantomData, mem, rc::Rc};
|
use std::{cell::RefCell, fmt, future::Future, marker::PhantomData, mem, rc::Rc};
|
||||||
|
|
||||||
use actix_http::{body::BoxBody, Extensions};
|
use actix_http::{
|
||||||
|
body::{BoxBody, MessageBody},
|
||||||
|
Extensions,
|
||||||
|
};
|
||||||
use actix_router::{ResourceDef, Router};
|
use actix_router::{ResourceDef, Router};
|
||||||
use actix_service::{
|
use actix_service::{
|
||||||
apply, apply_fn_factory, boxed, IntoServiceFactory, Service, ServiceFactory,
|
apply, apply_fn_factory, boxed, IntoServiceFactory, Service, ServiceFactory,
|
||||||
@ -399,15 +402,16 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> HttpServiceFactory for Scope<T>
|
impl<T, B> HttpServiceFactory for Scope<T, B>
|
||||||
where
|
where
|
||||||
T: ServiceFactory<
|
T: ServiceFactory<
|
||||||
ServiceRequest,
|
ServiceRequest,
|
||||||
Config = (),
|
Config = (),
|
||||||
Response = ServiceResponse,
|
Response = ServiceResponse<B>,
|
||||||
Error = Error,
|
Error = Error,
|
||||||
InitError = (),
|
InitError = (),
|
||||||
> + 'static,
|
> + 'static,
|
||||||
|
B: MessageBody + 'static,
|
||||||
{
|
{
|
||||||
fn register(mut self, config: &mut AppService) {
|
fn register(mut self, config: &mut AppService) {
|
||||||
// update default resource if needed
|
// update default resource if needed
|
||||||
@ -457,7 +461,9 @@ where
|
|||||||
req.add_data_container(Rc::clone(data));
|
req.add_data_container(Rc::clone(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
srv.call(req)
|
let fut = srv.call(req);
|
||||||
|
|
||||||
|
async { Ok(fut.await?.map_into_boxed_body()) }
|
||||||
});
|
});
|
||||||
|
|
||||||
// register final service
|
// register final service
|
||||||
@ -980,6 +986,29 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_middleware_body_type() {
|
||||||
|
// Compile test that Scope accepts any body type; test for `EitherBody`
|
||||||
|
let srv = init_service(
|
||||||
|
App::new().service(
|
||||||
|
web::scope("app")
|
||||||
|
.wrap_fn(|req, srv| {
|
||||||
|
let fut = srv.call(req);
|
||||||
|
async { Ok(fut.await?.map_into_right_body::<()>()) }
|
||||||
|
})
|
||||||
|
.service(web::resource("/test").route(web::get().to(|| async { "hello" }))),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
// test if `MessageBody::try_into_bytes()` is preserved across scope layer
|
||||||
|
use actix_http::body::MessageBody as _;
|
||||||
|
let req = TestRequest::with_uri("/app/test").to_request();
|
||||||
|
let resp = call_service(&srv, req).await;
|
||||||
|
let body = resp.into_body();
|
||||||
|
assert_eq!(body.try_into_bytes().unwrap(), b"hello".as_ref());
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_middleware_fn() {
|
async fn test_middleware_fn() {
|
||||||
let srv = init_service(
|
let srv = init_service(
|
||||||
|
Loading…
Reference in New Issue
Block a user