mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-23 16:21:06 +01:00
Add app_data method to GuardContext (#3341)
* changes: guard * fix(guard): docs link to app_data * docs: fix changelog --------- Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
parent
b2d0196f34
commit
8fdf358954
@ -4,6 +4,7 @@
|
||||
|
||||
### Added
|
||||
|
||||
- Add `guard::GuardContext::app_data()` method.
|
||||
- Implement `From<Box<dyn ResponseError>>` for `Error`.
|
||||
|
||||
## 4.6.0
|
||||
|
@ -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.
|
||||
@ -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()));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user