Skip to content

Commit

Permalink
Add app_data method to GuardContext (#3341)
Browse files Browse the repository at this point in the history
* changes: guard

* fix(guard): docs link to app_data

* docs: fix changelog

---------

Co-authored-by: Rob Ede <[email protected]>
  • Loading branch information
GreeFine and robjtede authored Jun 7, 2024
1 parent b2d0196 commit 8fdf358
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Added

- Add `guard::GuardContext::app_data()` method.
- Implement `From<Box<dyn ResponseError>>` for `Error`.

## 4.6.0
Expand Down
20 changes: 20 additions & 0 deletions actix-web/src/guard/mod.rs
Original file line number Diff line number Diff line change
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](crate::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 8fdf358

Please sign in to comment.