Skip to content

Commit

Permalink
changes: guard
Browse files Browse the repository at this point in the history
  • Loading branch information
GreeFine committed Apr 24, 2024
1 parent ba7fd04 commit 09ff35a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions actix-web/CHANGES.md
Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions actix-web/src/guard/mod.rs
Expand Up @@ -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`].

Check failure on line 114 in actix-web/src/guard/mod.rs

View workflow job for this annotation

GitHub Actions / lint-docs

unresolved link to `HttpRequest::app_data`
#[inline]
pub fn app_data<T: 'static>(&self) -> Option<&T> {
self.req.app_data()
}
}

/// Interface for routing guards.
Expand Down Expand Up @@ -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()));
}
}

0 comments on commit 09ff35a

Please sign in to comment.