1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-05-19 16:58:14 +00:00

changes: guard

This commit is contained in:
Raphael C 2024-04-24 20:56:43 +02:00
parent ba7fd048b6
commit 09ff35a602
2 changed files with 21 additions and 0 deletions

View file

@ -5,6 +5,7 @@
### Added
- Add `unicode` crate feature (on-by-default) to switch between `regex` and `regex-lite` as a trade-off between full unicode support and binary size.
- Support for app_data method in a GuardContext
### Changed

View file

@ -110,6 +110,12 @@ impl<'a> GuardContext<'a> {
pub fn header<H: Header>(&self) -> Option<H> {
H::parse(self.req).ok()
}
/// Counterpart to [`HttpRequest::app_data`].
#[inline]
pub fn app_data<T: 'static>(&self) -> Option<&T> {
self.req.app_data()
}
}
/// Interface for routing guards.
@ -512,4 +518,18 @@ mod tests {
.to_srv_request();
assert!(guard.check(&req.guard_ctx()));
}
#[test]
fn app_data() {
const TEST_VALUE: u32 = 42;
let guard = fn_guard(|ctx| dbg!(ctx.app_data::<u32>()) == Some(&TEST_VALUE));
let req = TestRequest::default().app_data(TEST_VALUE).to_srv_request();
assert!(guard.check(&req.guard_ctx()));
let req = TestRequest::default()
.app_data(TEST_VALUE * 2)
.to_srv_request();
assert!(!guard.check(&req.guard_ctx()));
}
}