Skip to content

Commit

Permalink
Add methods for with-revalidate flag to CacheControl
Browse files Browse the repository at this point in the history
  • Loading branch information
allenap committed Jun 16, 2024
1 parent 0fbde12 commit 449f251
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/common/cache_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ impl CacheControl {
pub fn immutable(&self) -> bool {
self.flags.contains(Flags::IMMUTABLE)
}

/// Check if the `must_understand` directive is set.
pub fn must_revalidate(&self) -> bool {
self.flags.contains(Flags::MUST_REVALIDATE)
}

/// Check if the `must_understand` directive is set.
pub fn must_understand(&self) -> bool {
self.flags.contains(Flags::MUST_UNDERSTAND)
Expand Down Expand Up @@ -192,11 +198,18 @@ impl CacheControl {
self
}

/// Set the `must_understand` directive.
pub fn with_must_revalidate(mut self) -> Self {
self.flags.insert(Flags::MUST_REVALIDATE);
self
}

/// Set the `must_understand` directive.
pub fn with_must_understand(mut self) -> Self {
self.flags.insert(Flags::MUST_UNDERSTAND);
self
}

/// Set the `max-age` directive.
pub fn with_max_age(mut self, duration: Duration) -> Self {
self.max_age = Some(duration.into());
Expand Down Expand Up @@ -490,6 +503,18 @@ mod tests {
assert!(cc.immutable());
}

#[test]
fn test_must_revalidate() {
let cc = CacheControl::new().with_must_revalidate();
let headers = test_encode(cc.clone());
assert_eq!(headers["cache-control"], "must-revalidate");
assert_eq!(
test_decode::<CacheControl>(&["must-revalidate"]).unwrap(),
cc
);
assert!(cc.must_revalidate());
}

#[test]
fn test_must_understand() {
let cc = CacheControl::new().with_must_understand();
Expand Down

0 comments on commit 449f251

Please sign in to comment.