memory_serve/
cache_control.rs1use axum::http::{HeaderName, HeaderValue, header::CACHE_CONTROL};
2
3#[derive(Debug, Clone, Copy)]
6pub enum CacheControl {
7 Long,
9 Medium,
11 Short,
13 NoCache,
15 Custom(&'static str),
17}
18
19impl CacheControl {
20 pub(crate) fn as_header(&self) -> (HeaderName, HeaderValue) {
22 let value = match self {
23 Self::Long => "max-age=31536000, immutable",
24 Self::Medium => "max-age=604800, stale-while-revalidate=86400",
25 Self::Short => "max-age=300, private",
26 Self::NoCache => "no-cache",
27 Self::Custom(value) => value,
28 };
29
30 (CACHE_CONTROL, HeaderValue::from_static(value))
31 }
32}
33
34#[cfg(test)]
35mod tests {
36 use super::CacheControl;
37 use axum::http::header::CACHE_CONTROL;
38
39 fn header_value(cache_control: CacheControl) -> String {
40 let (name, value) = cache_control.as_header();
41 assert_eq!(name, CACHE_CONTROL);
42 value.to_str().unwrap().to_owned()
43 }
44
45 #[test]
46 fn as_header_values() {
47 assert_eq!(
48 header_value(CacheControl::Long),
49 "max-age=31536000, immutable"
50 );
51 assert_eq!(
52 header_value(CacheControl::Medium),
53 "max-age=604800, stale-while-revalidate=86400"
54 );
55 assert_eq!(header_value(CacheControl::Short), "max-age=300, private");
56 assert_eq!(header_value(CacheControl::NoCache), "no-cache");
57 assert_eq!(
58 header_value(CacheControl::Custom("max-age=42")),
59 "max-age=42"
60 );
61 }
62}