Add caching headers to unmodified static resources

per https://www.rfc-editor.org/rfc/rfc7232#section-4.1

The server generating a 304 response MUST generate any of the
following header fields that would have been sent in a 200 (OK)
response to the same request: Cache-Control, Content-Location, Date,
ETag, Expires, and Vary.

Closes gh-34614

Signed-off-by: James Yuzawa <jtyuzawa@gmail.com>
This commit is contained in:
James Yuzawa
2025-03-17 21:10:23 -04:00
committed by Brian Clozel
parent 4a46d957f3
commit aa5c0dcd72
2 changed files with 9 additions and 5 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -479,11 +479,13 @@ class ResourceHttpRequestHandlerTests {
@Test
void shouldRespondWithNotModifiedWhenModifiedSince() throws Exception {
this.handler.setCacheSeconds(3600);
this.handler.afterPropertiesSet();
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
this.request.addHeader("If-Modified-Since", resourceLastModified("test/foo.css"));
this.handler.handleRequest(this.request, this.response);
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_NOT_MODIFIED);
assertThat(this.response.getHeader("Cache-Control")).isEqualTo("max-age=3600");
}
@Test
@@ -498,12 +500,14 @@ class ResourceHttpRequestHandlerTests {
@Test
void shouldRespondWithNotModifiedWhenEtag() throws Exception {
this.handler.setCacheSeconds(3600);
this.handler.setEtagGenerator(resource -> "testEtag");
this.handler.afterPropertiesSet();
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
this.request.addHeader("If-None-Match", "\"testEtag\"");
this.handler.handleRequest(this.request, this.response);
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_NOT_MODIFIED);
assertThat(this.response.getHeader("Cache-Control")).isEqualTo("max-age=3600");
}
@Test