use axum::http::{HeaderName, HeaderValue, header::CACHE_CONTROL};
#[derive(Debug, Clone, Copy)]
pub enum CacheControl {
Long,
Medium,
Short,
NoCache,
Custom(&'static str),
}
impl CacheControl {
pub(crate) fn as_header(&self) -> (HeaderName, HeaderValue) {
let value = match self {
Self::Long => "max-age=31536000, immutable",
Self::Medium => "max-age=604800, stale-while-revalidate=86400",
Self::Short => "max-age=300, private",
Self::NoCache => "no-cache",
Self::Custom(value) => value,
};
(CACHE_CONTROL, HeaderValue::from_static(value))
}
}
#[cfg(test)]
mod tests {
use super::CacheControl;
use axum::http::header::CACHE_CONTROL;
fn header_value(cache_control: CacheControl) -> String {
let (name, value) = cache_control.as_header();
assert_eq!(name, CACHE_CONTROL);
value.to_str().unwrap().to_owned()
}
#[test]
fn as_header_values() {
assert_eq!(
header_value(CacheControl::Long),
"max-age=31536000, immutable"
);
assert_eq!(
header_value(CacheControl::Medium),
"max-age=604800, stale-while-revalidate=86400"
);
assert_eq!(header_value(CacheControl::Short), "max-age=300, private");
assert_eq!(header_value(CacheControl::NoCache), "no-cache");
assert_eq!(
header_value(CacheControl::Custom("max-age=42")),
"max-age=42"
);
}
}