Skip to content

Commit b5451e0

Browse files
committed
omit cache-control header when it would be empty
1 parent fdeccb9 commit b5451e0

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/test/mod.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,17 @@ pub(crate) fn assert_cache_control(
6161
config: &Config,
6262
) {
6363
assert!(config.cache_control_stale_while_revalidate.is_some());
64-
assert_eq!(
65-
res.headers()
66-
.get("Cache-Control")
67-
.expect("missing cache-control header"),
68-
&CacheControl(cache_policy.render(config)).to_string()
69-
);
64+
let cache_control = res.headers().get("Cache-Control");
65+
66+
let expected_directives = cache_policy.render(config);
67+
if expected_directives.is_empty() {
68+
assert!(cache_control.is_none());
69+
} else {
70+
assert_eq!(
71+
cache_control.expect("missing cache-control header"),
72+
&CacheControl(expected_directives).to_string()
73+
);
74+
}
7075
}
7176

7277
/// Make sure that a URL returns a status code between 200-299

src/web/cache.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ impl AfterMiddleware for CacheMiddleware {
104104
);
105105
}
106106

107-
res.headers.set(CacheControl(cache.render(config)));
107+
let directives = cache.render(config);
108+
if !directives.is_empty() {
109+
res.headers.set(CacheControl(directives))
110+
}
108111
Ok(res)
109112
}
110113
}

src/web/rustdoc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1769,7 +1769,7 @@ mod test {
17691769
let url = format!("http://{}/dummy", web.server_addr());
17701770
let resp = client.get(url).send()?;
17711771
assert_eq!(resp.status(), StatusCode::FOUND);
1772-
assert!(resp.headers().get("Cache-Control").unwrap().is_empty());
1772+
assert!(resp.headers().get("Cache-Control").is_none());
17731773
assert!(resp
17741774
.headers()
17751775
.get("Location")

0 commit comments

Comments
 (0)