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