Skip to content

Commit

Permalink
Add methods for must-revalidate flag to CacheControl
Browse files Browse the repository at this point in the history
  • Loading branch information
allenap authored and seanmonstar committed Jun 28, 2024
1 parent 0fbde12 commit 879d2d4
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/common/cache_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ impl CacheControl {
pub fn immutable(&self) -> bool {
self.flags.contains(Flags::IMMUTABLE)
}
/// Check if the `must_understand` directive is set.

/// Check if the `must-revalidate` 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.
/// Set the `must-revalidate` 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 879d2d4

Please # to comment.