mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
rename .middleware to .wrap
This commit is contained in:
parent
939d2e745c
commit
86a21c956c
@ -178,7 +178,7 @@ impl CookieSessionInner {
|
|||||||
/// use actix_web::{web, App, HttpResponse, HttpServer};
|
/// use actix_web::{web, App, HttpResponse, HttpServer};
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// fn main() {
|
||||||
/// let app = App::new().middleware(
|
/// let app = App::new().wrap(
|
||||||
/// CookieSession::signed(&[0; 32])
|
/// CookieSession::signed(&[0; 32])
|
||||||
/// .domain("www.rust-lang.org")
|
/// .domain("www.rust-lang.org")
|
||||||
/// .name("actix_session")
|
/// .name("actix_session")
|
||||||
@ -323,7 +323,7 @@ mod tests {
|
|||||||
fn cookie_session() {
|
fn cookie_session() {
|
||||||
let mut app = test::init_service(
|
let mut app = test::init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.middleware(CookieSession::signed(&[0; 32]).secure(false))
|
.wrap(CookieSession::signed(&[0; 32]).secure(false))
|
||||||
.service(web::resource("/").to(|ses: Session| {
|
.service(web::resource("/").to(|ses: Session| {
|
||||||
let _ = ses.set("counter", 100);
|
let _ = ses.set("counter", 100);
|
||||||
"test"
|
"test"
|
||||||
@ -342,7 +342,7 @@ mod tests {
|
|||||||
fn cookie_session_extractor() {
|
fn cookie_session_extractor() {
|
||||||
let mut app = test::init_service(
|
let mut app = test::init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.middleware(CookieSession::signed(&[0; 32]).secure(false))
|
.wrap(CookieSession::signed(&[0; 32]).secure(false))
|
||||||
.service(web::resource("/").to(|ses: Session| {
|
.service(web::resource("/").to(|ses: Session| {
|
||||||
let _ = ses.set("counter", 100);
|
let _ = ses.set("counter", 100);
|
||||||
"test"
|
"test"
|
||||||
|
@ -31,7 +31,7 @@
|
|||||||
//! fn main() -> std::io::Result<()> {
|
//! fn main() -> std::io::Result<()> {
|
||||||
//! # std::thread::spawn(||
|
//! # std::thread::spawn(||
|
||||||
//! HttpServer::new(
|
//! HttpServer::new(
|
||||||
//! || App::new().middleware(
|
//! || App::new().wrap(
|
||||||
//! CookieSession::signed(&[0; 32]) // <- create cookie based session middleware
|
//! CookieSession::signed(&[0; 32]) // <- create cookie based session middleware
|
||||||
//! .secure(false)
|
//! .secure(false)
|
||||||
//! )
|
//! )
|
||||||
|
@ -26,9 +26,9 @@ fn main() -> std::io::Result<()> {
|
|||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
.middleware(middleware::DefaultHeaders::new().header("X-Version", "0.2"))
|
.wrap(middleware::DefaultHeaders::new().header("X-Version", "0.2"))
|
||||||
.middleware(middleware::Compress::default())
|
.wrap(middleware::Compress::default())
|
||||||
.middleware(middleware::Logger::default())
|
.wrap(middleware::Logger::default())
|
||||||
.service(index)
|
.service(index)
|
||||||
.service(no_params)
|
.service(no_params)
|
||||||
.service(
|
.service(
|
||||||
|
@ -108,7 +108,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Register a middleware.
|
/// Register a middleware.
|
||||||
pub fn middleware<M, B, F>(
|
pub fn wrap<M, B, F>(
|
||||||
self,
|
self,
|
||||||
mw: F,
|
mw: F,
|
||||||
) -> AppRouter<
|
) -> AppRouter<
|
||||||
@ -322,7 +322,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Register a middleware.
|
/// Register a middleware.
|
||||||
pub fn middleware<M, B1, F>(
|
pub fn wrap<M, B1, F>(
|
||||||
self,
|
self,
|
||||||
mw: F,
|
mw: F,
|
||||||
) -> AppRouter<
|
) -> AppRouter<
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
//! Cross-origin resource sharing (CORS) for Actix applications
|
//! Cross-origin resource sharing (CORS) for Actix applications
|
||||||
//!
|
//!
|
||||||
//! CORS middleware could be used with application and with resource.
|
//! CORS middleware could be used with application and with resource.
|
||||||
//! Cors middleware could be used as parameter for `App::middleware()`,
|
//! Cors middleware could be used as parameter for `App::wrap()`,
|
||||||
//! `Resource::middleware()` or `Scope::middleware()` methods.
|
//! `Resource::wrap()` or `Scope::wrap()` methods.
|
||||||
//!
|
//!
|
||||||
//! # Example
|
//! # Example
|
||||||
//!
|
//!
|
||||||
@ -16,7 +16,7 @@
|
|||||||
//!
|
//!
|
||||||
//! fn main() -> std::io::Result<()> {
|
//! fn main() -> std::io::Result<()> {
|
||||||
//! HttpServer::new(|| App::new()
|
//! HttpServer::new(|| App::new()
|
||||||
//! .middleware(
|
//! .wrap(
|
||||||
//! Cors::new() // <- Construct CORS middleware builder
|
//! Cors::new() // <- Construct CORS middleware builder
|
||||||
//! .allowed_origin("https://www.rust-lang.org/")
|
//! .allowed_origin("https://www.rust-lang.org/")
|
||||||
//! .allowed_methods(vec!["GET", "POST"])
|
//! .allowed_methods(vec!["GET", "POST"])
|
||||||
|
@ -18,7 +18,7 @@ use crate::service::{ServiceRequest, ServiceResponse};
|
|||||||
///
|
///
|
||||||
/// fn main() {
|
/// fn main() {
|
||||||
/// let app = App::new()
|
/// let app = App::new()
|
||||||
/// .middleware(middleware::DefaultHeaders::new().header("X-Version", "0.2"))
|
/// .wrap(middleware::DefaultHeaders::new().header("X-Version", "0.2"))
|
||||||
/// .service(
|
/// .service(
|
||||||
/// web::resource("/test")
|
/// web::resource("/test")
|
||||||
/// .route(web::get().to(|| HttpResponse::Ok()))
|
/// .route(web::get().to(|| HttpResponse::Ok()))
|
||||||
|
@ -41,7 +41,7 @@ type ErrorHandler<B> = Fn(ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>>
|
|||||||
///
|
///
|
||||||
/// fn main() {
|
/// fn main() {
|
||||||
/// let app = App::new()
|
/// let app = App::new()
|
||||||
/// .middleware(
|
/// .wrap(
|
||||||
/// ErrorHandlers::new()
|
/// ErrorHandlers::new()
|
||||||
/// .handler(http::StatusCode::INTERNAL_SERVER_ERROR, render_500),
|
/// .handler(http::StatusCode::INTERNAL_SERVER_ERROR, render_500),
|
||||||
/// )
|
/// )
|
||||||
|
@ -37,7 +37,7 @@
|
|||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! fn main() {
|
//! fn main() {
|
||||||
//! let app = App::new().middleware(IdentityService::new(
|
//! let app = App::new().wrap(IdentityService::new(
|
||||||
//! // <- create identity middleware
|
//! // <- create identity middleware
|
||||||
//! CookieIdentityPolicy::new(&[0; 32]) // <- create cookie session backend
|
//! CookieIdentityPolicy::new(&[0; 32]) // <- create cookie session backend
|
||||||
//! .name("auth-cookie")
|
//! .name("auth-cookie")
|
||||||
@ -179,7 +179,7 @@ pub trait IdentityPolicy: Sized + 'static {
|
|||||||
/// use actix_web::middleware::identity::{CookieIdentityPolicy, IdentityService};
|
/// use actix_web::middleware::identity::{CookieIdentityPolicy, IdentityService};
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// fn main() {
|
||||||
/// let app = App::new().middleware(IdentityService::new(
|
/// let app = App::new().wrap(IdentityService::new(
|
||||||
/// // <- create identity middleware
|
/// // <- create identity middleware
|
||||||
/// CookieIdentityPolicy::new(&[0; 32]) // <- create cookie session backend
|
/// CookieIdentityPolicy::new(&[0; 32]) // <- create cookie session backend
|
||||||
/// .name("auth-cookie")
|
/// .name("auth-cookie")
|
||||||
@ -381,7 +381,7 @@ impl CookieIdentityInner {
|
|||||||
/// use actix_web::App;
|
/// use actix_web::App;
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// fn main() {
|
||||||
/// let app = App::new().middleware(IdentityService::new(
|
/// let app = App::new().wrap(IdentityService::new(
|
||||||
/// // <- create identity middleware
|
/// // <- create identity middleware
|
||||||
/// CookieIdentityPolicy::new(&[0; 32]) // <- construct cookie policy
|
/// CookieIdentityPolicy::new(&[0; 32]) // <- construct cookie policy
|
||||||
/// .domain("www.rust-lang.org")
|
/// .domain("www.rust-lang.org")
|
||||||
@ -473,7 +473,7 @@ mod tests {
|
|||||||
fn test_identity() {
|
fn test_identity() {
|
||||||
let mut srv = test::init_service(
|
let mut srv = test::init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.middleware(IdentityService::new(
|
.wrap(IdentityService::new(
|
||||||
CookieIdentityPolicy::new(&[0; 32])
|
CookieIdentityPolicy::new(&[0; 32])
|
||||||
.domain("www.rust-lang.org")
|
.domain("www.rust-lang.org")
|
||||||
.name("actix_auth")
|
.name("actix_auth")
|
||||||
|
@ -41,8 +41,8 @@ use crate::{HttpMessage, HttpResponse};
|
|||||||
/// env_logger::init();
|
/// env_logger::init();
|
||||||
///
|
///
|
||||||
/// let app = App::new()
|
/// let app = App::new()
|
||||||
/// .middleware(Logger::default())
|
/// .wrap(Logger::default())
|
||||||
/// .middleware(Logger::new("%a %{User-Agent}i"));
|
/// .wrap(Logger::new("%a %{User-Agent}i"));
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
@ -550,7 +550,7 @@ mod tests {
|
|||||||
use crate::test::{call_success, init_service, TestRequest};
|
use crate::test::{call_success, init_service, TestRequest};
|
||||||
use crate::{web, App, Error, HttpResponse};
|
use crate::{web, App, Error, HttpResponse};
|
||||||
|
|
||||||
fn md1<S, P, B>(
|
fn md<S, P, B>(
|
||||||
req: ServiceRequest<P>,
|
req: ServiceRequest<P>,
|
||||||
srv: &mut S,
|
srv: &mut S,
|
||||||
) -> impl IntoFuture<Item = ServiceResponse<B>, Error = Error>
|
) -> impl IntoFuture<Item = ServiceResponse<B>, Error = Error>
|
||||||
@ -573,7 +573,7 @@ mod tests {
|
|||||||
let mut srv = init_service(
|
let mut srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::resource("/test")
|
web::resource("/test")
|
||||||
.wrap(md1)
|
.wrap(md)
|
||||||
.route(web::get().to(|| HttpResponse::Ok())),
|
.route(web::get().to(|| HttpResponse::Ok())),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
43
src/scope.rs
43
src/scope.rs
@ -205,7 +205,7 @@ where
|
|||||||
/// This is similar to `App's` middlewares, but middleware get invoked on scope level.
|
/// This is similar to `App's` middlewares, but middleware get invoked on scope level.
|
||||||
/// Scope level middlewares are not allowed to change response
|
/// Scope level middlewares are not allowed to change response
|
||||||
/// type (i.e modify response's body).
|
/// type (i.e modify response's body).
|
||||||
pub fn middleware<M, F>(
|
pub fn wrap<M, F>(
|
||||||
self,
|
self,
|
||||||
mw: F,
|
mw: F,
|
||||||
) -> Scope<
|
) -> Scope<
|
||||||
@ -476,11 +476,13 @@ impl<P: 'static> NewService for ScopeEndpoint<P> {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
use futures::{Future, IntoFuture};
|
||||||
|
|
||||||
use crate::dev::{Body, ResponseBody};
|
use crate::dev::{Body, ResponseBody};
|
||||||
use crate::http::{Method, StatusCode};
|
use crate::http::{header, HeaderValue, Method, StatusCode};
|
||||||
use crate::test::{block_on, init_service, TestRequest};
|
use crate::service::{ServiceRequest, ServiceResponse};
|
||||||
use crate::{guard, web, App, HttpRequest, HttpResponse};
|
use crate::test::{block_on, call_success, init_service, TestRequest};
|
||||||
|
use crate::{guard, web, App, Error, HttpRequest, HttpResponse};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_scope() {
|
fn test_scope() {
|
||||||
@ -827,4 +829,37 @@ mod tests {
|
|||||||
let resp = block_on(srv.call(req)).unwrap();
|
let resp = block_on(srv.call(req)).unwrap();
|
||||||
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn md<S, P, B>(
|
||||||
|
req: ServiceRequest<P>,
|
||||||
|
srv: &mut S,
|
||||||
|
) -> impl IntoFuture<Item = ServiceResponse<B>, Error = Error>
|
||||||
|
where
|
||||||
|
S: Service<
|
||||||
|
Request = ServiceRequest<P>,
|
||||||
|
Response = ServiceResponse<B>,
|
||||||
|
Error = Error,
|
||||||
|
>,
|
||||||
|
{
|
||||||
|
srv.call(req).map(|mut res| {
|
||||||
|
res.headers_mut()
|
||||||
|
.insert(header::CONTENT_TYPE, HeaderValue::from_static("0001"));
|
||||||
|
res
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_middleware() {
|
||||||
|
let mut srv =
|
||||||
|
init_service(App::new().service(web::scope("app").wrap(md).service(
|
||||||
|
web::resource("/test").route(web::get().to(|| HttpResponse::Ok())),
|
||||||
|
)));
|
||||||
|
let req = TestRequest::with_uri("/app/test").to_request();
|
||||||
|
let resp = call_success(&mut srv, req);
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
assert_eq!(
|
||||||
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
|
HeaderValue::from_static("0001")
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ fn test_body_gzip() {
|
|||||||
let mut srv = TestServer::new(|| {
|
let mut srv = TestServer::new(|| {
|
||||||
h1::H1Service::new(
|
h1::H1Service::new(
|
||||||
App::new()
|
App::new()
|
||||||
.middleware(middleware::Compress::new(ContentEncoding::Gzip))
|
.wrap(middleware::Compress::new(ContentEncoding::Gzip))
|
||||||
.service(web::resource("/").route(web::to(|| Response::Ok().body(STR)))),
|
.service(web::resource("/").route(web::to(|| Response::Ok().body(STR)))),
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
@ -87,7 +87,7 @@ fn test_body_gzip_large() {
|
|||||||
let data = srv_data.clone();
|
let data = srv_data.clone();
|
||||||
h1::H1Service::new(
|
h1::H1Service::new(
|
||||||
App::new()
|
App::new()
|
||||||
.middleware(middleware::Compress::new(ContentEncoding::Gzip))
|
.wrap(middleware::Compress::new(ContentEncoding::Gzip))
|
||||||
.service(
|
.service(
|
||||||
web::resource("/")
|
web::resource("/")
|
||||||
.route(web::to(move || Response::Ok().body(data.clone()))),
|
.route(web::to(move || Response::Ok().body(data.clone()))),
|
||||||
@ -121,7 +121,7 @@ fn test_body_gzip_large_random() {
|
|||||||
let data = srv_data.clone();
|
let data = srv_data.clone();
|
||||||
h1::H1Service::new(
|
h1::H1Service::new(
|
||||||
App::new()
|
App::new()
|
||||||
.middleware(middleware::Compress::new(ContentEncoding::Gzip))
|
.wrap(middleware::Compress::new(ContentEncoding::Gzip))
|
||||||
.service(
|
.service(
|
||||||
web::resource("/")
|
web::resource("/")
|
||||||
.route(web::to(move || Response::Ok().body(data.clone()))),
|
.route(web::to(move || Response::Ok().body(data.clone()))),
|
||||||
@ -149,7 +149,7 @@ fn test_body_chunked_implicit() {
|
|||||||
let mut srv = TestServer::new(move || {
|
let mut srv = TestServer::new(move || {
|
||||||
h1::H1Service::new(
|
h1::H1Service::new(
|
||||||
App::new()
|
App::new()
|
||||||
.middleware(middleware::Compress::new(ContentEncoding::Gzip))
|
.wrap(middleware::Compress::new(ContentEncoding::Gzip))
|
||||||
.service(web::resource("/").route(web::get().to(move || {
|
.service(web::resource("/").route(web::get().to(move || {
|
||||||
Response::Ok().streaming(once(Ok::<_, Error>(Bytes::from_static(
|
Response::Ok().streaming(once(Ok::<_, Error>(Bytes::from_static(
|
||||||
STR.as_ref(),
|
STR.as_ref(),
|
||||||
@ -181,7 +181,7 @@ fn test_body_br_streaming() {
|
|||||||
let mut srv = TestServer::new(move || {
|
let mut srv = TestServer::new(move || {
|
||||||
h1::H1Service::new(
|
h1::H1Service::new(
|
||||||
App::new()
|
App::new()
|
||||||
.middleware(middleware::Compress::new(ContentEncoding::Br))
|
.wrap(middleware::Compress::new(ContentEncoding::Br))
|
||||||
.service(web::resource("/").route(web::to(move || {
|
.service(web::resource("/").route(web::to(move || {
|
||||||
Response::Ok().streaming(once(Ok::<_, Error>(Bytes::from_static(
|
Response::Ok().streaming(once(Ok::<_, Error>(Bytes::from_static(
|
||||||
STR.as_ref(),
|
STR.as_ref(),
|
||||||
@ -254,7 +254,7 @@ fn test_body_deflate() {
|
|||||||
let mut srv = TestServer::new(move || {
|
let mut srv = TestServer::new(move || {
|
||||||
h1::H1Service::new(
|
h1::H1Service::new(
|
||||||
App::new()
|
App::new()
|
||||||
.middleware(middleware::Compress::new(ContentEncoding::Deflate))
|
.wrap(middleware::Compress::new(ContentEncoding::Deflate))
|
||||||
.service(
|
.service(
|
||||||
web::resource("/").route(web::to(move || Response::Ok().body(STR))),
|
web::resource("/").route(web::to(move || Response::Ok().body(STR))),
|
||||||
),
|
),
|
||||||
@ -281,7 +281,7 @@ fn test_body_brotli() {
|
|||||||
let mut srv = TestServer::new(move || {
|
let mut srv = TestServer::new(move || {
|
||||||
h1::H1Service::new(
|
h1::H1Service::new(
|
||||||
App::new()
|
App::new()
|
||||||
.middleware(middleware::Compress::new(ContentEncoding::Br))
|
.wrap(middleware::Compress::new(ContentEncoding::Br))
|
||||||
.service(
|
.service(
|
||||||
web::resource("/").route(web::to(move || Response::Ok().body(STR))),
|
web::resource("/").route(web::to(move || Response::Ok().body(STR))),
|
||||||
),
|
),
|
||||||
|
Loading…
Reference in New Issue
Block a user