Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods for must-revalidate flag to CacheControl #186

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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