Skip to content

Commit

Permalink
omit cache-control header when it would be empty
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar committed Sep 30, 2022
1 parent fdeccb9 commit b5451e0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
17 changes: 11 additions & 6 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,17 @@ pub(crate) fn assert_cache_control(
config: &Config,
) {
assert!(config.cache_control_stale_while_revalidate.is_some());
assert_eq!(
res.headers()
.get("Cache-Control")
.expect("missing cache-control header"),
&CacheControl(cache_policy.render(config)).to_string()
);
let cache_control = res.headers().get("Cache-Control");

let expected_directives = cache_policy.render(config);
if expected_directives.is_empty() {
assert!(cache_control.is_none());
} else {
assert_eq!(
cache_control.expect("missing cache-control header"),
&CacheControl(expected_directives).to_string()
);
}
}

/// Make sure that a URL returns a status code between 200-299
Expand Down
5 changes: 4 additions & 1 deletion src/web/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ impl AfterMiddleware for CacheMiddleware {
);
}

res.headers.set(CacheControl(cache.render(config)));
let directives = cache.render(config);
if !directives.is_empty() {
res.headers.set(CacheControl(directives))
}
Ok(res)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/web/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1769,7 +1769,7 @@ mod test {
let url = format!("http://{}/dummy", web.server_addr());
let resp = client.get(url).send()?;
assert_eq!(resp.status(), StatusCode::FOUND);
assert!(resp.headers().get("Cache-Control").unwrap().is_empty());
assert!(resp.headers().get("Cache-Control").is_none());
assert!(resp
.headers()
.get("Location")
Expand Down

0 comments on commit b5451e0

Please sign in to comment.