2022-01-05 15:02:28 +00:00
|
|
|
//! A collection of common middleware.
|
2019-03-26 22:14:32 +00:00
|
|
|
|
2021-01-05 00:22:57 +00:00
|
|
|
mod compat;
|
2019-11-21 08:52:33 +00:00
|
|
|
mod condition;
|
2021-01-05 09:51:58 +00:00
|
|
|
mod default_headers;
|
|
|
|
mod err_handlers;
|
2019-03-10 23:35:38 +00:00
|
|
|
mod logger;
|
2021-12-22 15:00:32 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod noop;
|
2021-01-05 09:51:58 +00:00
|
|
|
mod normalize;
|
2019-03-10 23:35:38 +00:00
|
|
|
|
2021-01-05 00:22:57 +00:00
|
|
|
pub use self::compat::Compat;
|
2019-11-21 08:52:33 +00:00
|
|
|
pub use self::condition::Condition;
|
2021-01-05 09:51:58 +00:00
|
|
|
pub use self::default_headers::DefaultHeaders;
|
|
|
|
pub use self::err_handlers::{ErrorHandlerResponse, ErrorHandlers};
|
2019-03-10 23:35:38 +00:00
|
|
|
pub use self::logger::Logger;
|
2021-12-22 15:00:32 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) use self::noop::Noop;
|
2021-01-05 09:51:58 +00:00
|
|
|
pub use self::normalize::{NormalizePath, TrailingSlash};
|
|
|
|
|
2021-06-19 19:21:13 +00:00
|
|
|
#[cfg(feature = "__compress")]
|
2021-01-05 09:51:58 +00:00
|
|
|
mod compress;
|
2021-06-19 19:21:13 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "__compress")]
|
2021-01-05 09:51:58 +00:00
|
|
|
pub use self::compress::Compress;
|
2021-08-31 03:18:54 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::{http::StatusCode, App};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn common_combinations() {
|
|
|
|
// ensure there's no reason that the built-in middleware cannot compose
|
|
|
|
|
|
|
|
let _ = App::new()
|
|
|
|
.wrap(Compat::new(Logger::default()))
|
|
|
|
.wrap(Condition::new(true, DefaultHeaders::new()))
|
2021-12-13 16:08:08 +00:00
|
|
|
.wrap(DefaultHeaders::new().add(("X-Test2", "X-Value2")))
|
2021-08-31 03:18:54 +00:00
|
|
|
.wrap(ErrorHandlers::new().handler(StatusCode::FORBIDDEN, |res| {
|
2021-12-14 21:17:50 +00:00
|
|
|
Ok(ErrorHandlerResponse::Response(res.map_into_left_body()))
|
2021-08-31 03:18:54 +00:00
|
|
|
}))
|
|
|
|
.wrap(Logger::default())
|
|
|
|
.wrap(NormalizePath::new(TrailingSlash::Trim));
|
|
|
|
|
|
|
|
let _ = App::new()
|
|
|
|
.wrap(NormalizePath::new(TrailingSlash::Trim))
|
|
|
|
.wrap(Logger::default())
|
|
|
|
.wrap(ErrorHandlers::new().handler(StatusCode::FORBIDDEN, |res| {
|
2021-12-14 21:17:50 +00:00
|
|
|
Ok(ErrorHandlerResponse::Response(res.map_into_left_body()))
|
2021-08-31 03:18:54 +00:00
|
|
|
}))
|
2021-12-13 16:08:08 +00:00
|
|
|
.wrap(DefaultHeaders::new().add(("X-Test2", "X-Value2")))
|
2021-08-31 03:18:54 +00:00
|
|
|
.wrap(Condition::new(true, DefaultHeaders::new()))
|
|
|
|
.wrap(Compat::new(Logger::default()));
|
|
|
|
|
|
|
|
#[cfg(feature = "__compress")]
|
|
|
|
{
|
|
|
|
let _ = App::new().wrap(Compress::default()).wrap(Logger::default());
|
|
|
|
let _ = App::new().wrap(Logger::default()).wrap(Compress::default());
|
|
|
|
let _ = App::new().wrap(Compat::new(Compress::default()));
|
|
|
|
let _ = App::new().wrap(Condition::new(true, Compat::new(Compress::default())));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|