1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 09:42:57 +01:00

feat: expose Identity middleware (#3390)

This commit is contained in:
Rob Ede 2024-06-08 05:26:26 +01:00 committed by GitHub
parent 3db7891303
commit 7c4c26d2df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 24 additions and 32 deletions

View File

@ -89,5 +89,5 @@ jobs:
- name: Generate API diff
run: |
for f in $(find -mindepth 2 -maxdepth 2 -name Cargo.toml); do
cargo public-api --manifest-path "$f" diff ${{ github.event.pull_request.base.sha }}..${{ github.sha }}
cargo public-api --manifest-path "$f" --simplified diff ${{ github.event.pull_request.base.sha }}..${{ github.sha }}
done

View File

@ -4,6 +4,7 @@
### Added
- Add `middleware::Identity` type.
- Add `CustomizeResponder::add_cookie()` method.
- Add `guard::GuardContext::app_data()` method.
- Implement `From<Box<dyn ResponseError>>` for `Error`.

View File

@ -38,15 +38,6 @@ pub struct Compat<T> {
transform: T,
}
#[cfg(test)]
impl Compat<super::Noop> {
pub(crate) fn noop() -> Self {
Self {
transform: super::Noop,
}
}
}
impl<T> Compat<T> {
/// Wrap a middleware to give it broader compatibility.
pub fn new(middleware: T) -> Self {
@ -152,7 +143,7 @@ mod tests {
use crate::{
dev::ServiceRequest,
http::StatusCode,
middleware::{self, Condition, Logger},
middleware::{self, Condition, Identity, Logger},
test::{self, call_service, init_service, TestRequest},
web, App, HttpResponse,
};
@ -225,7 +216,7 @@ mod tests {
async fn compat_noop_is_noop() {
let srv = test::ok_service();
let mw = Compat::noop()
let mw = Compat::new(Identity)
.new_transform(srv.into_service())
.await
.unwrap();

View File

@ -141,7 +141,7 @@ mod tests {
header::{HeaderValue, CONTENT_TYPE},
StatusCode,
},
middleware::{self, ErrorHandlerResponse, ErrorHandlers},
middleware::{self, ErrorHandlerResponse, ErrorHandlers, Identity},
test::{self, TestRequest},
web::Bytes,
HttpResponse,
@ -158,7 +158,7 @@ mod tests {
#[test]
fn compat_with_builtin_middleware() {
let _ = Condition::new(true, middleware::Compat::noop());
let _ = Condition::new(true, middleware::Compat::new(Identity));
let _ = Condition::new(true, middleware::Logger::default());
let _ = Condition::new(true, middleware::Compress::default());
let _ = Condition::new(true, middleware::NormalizePath::trim());

View File

@ -2,35 +2,39 @@
use actix_utils::future::{ready, Ready};
use crate::dev::{Service, Transform};
use crate::dev::{forward_ready, Service, Transform};
/// A no-op middleware that passes through request and response untouched.
pub(crate) struct Noop;
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct Identity;
impl<S: Service<Req>, Req> Transform<S, Req> for Noop {
impl<S: Service<Req>, Req> Transform<S, Req> for Identity {
type Response = S::Response;
type Error = S::Error;
type Transform = NoopService<S>;
type Transform = IdentityMiddleware<S>;
type InitError = ();
type Future = Ready<Result<Self::Transform, Self::InitError>>;
#[inline]
fn new_transform(&self, service: S) -> Self::Future {
ready(Ok(NoopService { service }))
ready(Ok(IdentityMiddleware { service }))
}
}
#[doc(hidden)]
pub(crate) struct NoopService<S> {
pub struct IdentityMiddleware<S> {
service: S,
}
impl<S: Service<Req>, Req> Service<Req> for NoopService<S> {
impl<S: Service<Req>, Req> Service<Req> for IdentityMiddleware<S> {
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
crate::dev::forward_ready!(service);
forward_ready!(service);
#[inline]
fn call(&self, req: Req) -> Self::Future {
self.service.call(req)
}

View File

@ -218,31 +218,27 @@
//! [lab_from_fn]: https://docs.rs/actix-web-lab/latest/actix_web_lab/middleware/fn.from_fn.html
mod compat;
#[cfg(feature = "__compress")]
mod compress;
mod condition;
mod default_headers;
mod err_handlers;
mod identity;
mod logger;
#[cfg(test)]
mod noop;
mod normalize;
#[cfg(test)]
pub(crate) use self::noop::Noop;
#[cfg(feature = "__compress")]
pub use self::compress::Compress;
pub use self::{
compat::Compat,
condition::Condition,
default_headers::DefaultHeaders,
err_handlers::{ErrorHandlerResponse, ErrorHandlers},
identity::Identity,
logger::Logger,
normalize::{NormalizePath, TrailingSlash},
};
#[cfg(feature = "__compress")]
mod compress;
#[cfg(feature = "__compress")]
pub use self::compress::Compress;
#[cfg(test)]
mod tests {
use super::*;