mirror of
https://github.com/actix/actix-web.git
synced 2024-11-22 17:41:11 +00: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
2 changed files with 21 additions and 0 deletions
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
|
- Add `guard::GuardContext::app_data()` method.
|
||||||
- Implement `From<Box<dyn ResponseError>>` for `Error`.
|
- Implement `From<Box<dyn ResponseError>>` for `Error`.
|
||||||
|
|
||||||
## 4.6.0
|
## 4.6.0
|
||||||
|
|
|
@ -110,6 +110,12 @@ impl<'a> GuardContext<'a> {
|
||||||
pub fn header<H: Header>(&self) -> Option<H> {
|
pub fn header<H: Header>(&self) -> Option<H> {
|
||||||
H::parse(self.req).ok()
|
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.
|
/// Interface for routing guards.
|
||||||
|
@ -512,4 +518,18 @@ mod tests {
|
||||||
.to_srv_request();
|
.to_srv_request();
|
||||||
assert!(guard.check(&req.guard_ctx()));
|
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 a new issue