Make data guards eligible to be sentinels.

Prior to this commit, data guards were not being considered as eligible
to be sentinels. This commit resolves that.
This commit is contained in:
pennae 2022-06-20 12:41:46 +02:00 committed by Sergio Benitez
parent ef2538f222
commit 7275df9fdf
2 changed files with 39 additions and 0 deletions

View File

@ -17,6 +17,7 @@ impl Route {
pub fn guards(&self) -> impl Iterator<Item = &Guard> {
self.param_guards()
.chain(self.query_guards())
.chain(self.data_guard.iter())
.chain(self.request_guards.iter())
}

View File

@ -106,6 +106,44 @@ async fn state_sentinel_works() {
assert!(result.is_ok());
}
struct Data;
#[crate::async_trait]
impl<'r> data::FromData<'r> for Data {
type Error = Error;
async fn from_data(_: &'r Request<'_>, _: data::Data<'r>) -> data::Outcome<'r, Self> {
unimplemented!()
}
}
impl Sentinel for Data {
fn abort(rocket: &Rocket<Ignite>) -> bool {
rocket.state::<Data>().is_none()
}
}
#[post("/data", data = "<_data>")]
fn with_data(_data: Data) {}
#[async_test]
async fn data_sentinel_works() {
let err = rocket::build()
.configure(Config::debug_default())
.mount("/", routes![with_data])
.ignite().await
.unwrap_err();
assert!(matches!(err.kind(), SentinelAborts(vec) if vec.len() == 1));
let result = rocket::build()
.configure(Config::debug_default())
.mount("/", routes![with_data])
.manage(Data)
.ignite().await;
assert!(result.is_ok());
}
#[test]
fn inner_sentinels_detected() {
use rocket::local::blocking::Client;