Support ETag generation on ResourceHttpRequestHandler
Prior to this commit, the `ResourceHttpRequestHandler` would support HTTP caching when serving resources, but only driving it through the `Resource#lastModified()` information. This commit introduces an ETag generator function that can be configured on the `ResourceHttpRequestHandler` to dynamically generate an ETag value for the Resource that is going to be served. Closes gh-29031
This commit is contained in:
@@ -478,7 +478,7 @@ class ResourceHttpRequestHandlerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRespondWithNotModified() throws Exception {
|
||||
void shouldRespondWithNotModifiedWhenModifiedSince() throws Exception {
|
||||
this.handler.afterPropertiesSet();
|
||||
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
|
||||
this.request.addHeader("If-Modified-Since", resourceLastModified("test/foo.css"));
|
||||
@@ -496,6 +496,38 @@ class ResourceHttpRequestHandlerTests {
|
||||
assertThat(this.response.getContentAsString()).isEqualTo("h1 { color:red; }");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRespondWithNotModifiedWhenEtag() throws Exception {
|
||||
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);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRespondWithModifiedResourceWhenEtagNoMatch() throws Exception {
|
||||
this.handler.setEtagGenerator(resource -> "noMatch");
|
||||
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_OK);
|
||||
assertThat(this.response.getContentAsString()).isEqualTo("h1 { color:red; }");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRespondWithNotModifiedWhenEtagAndLastModified() throws Exception {
|
||||
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.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);
|
||||
}
|
||||
|
||||
@Test // SPR-14005
|
||||
void overwritesExistingCacheControlHeaders() throws Exception {
|
||||
this.handler.setCacheSeconds(3600);
|
||||
|
||||
Reference in New Issue
Block a user