Support ETag generation on ResourceWebHandler

This commit replicates the ETag generation option now available on
`ResourceHttpRequestHandler` but for its WebFlux counterpart.

See gh-29031
This commit is contained in:
Brian Clozel
2023-10-25 16:01:15 +02:00
parent ff14c5121d
commit 9ac5eb0f55
2 changed files with 72 additions and 2 deletions

View File

@@ -500,7 +500,48 @@ class ResourceWebHandlerTests {
}
@Test
// SPR-14005
void shouldRespondWithNotModifiedWhenEtag() throws Exception {
this.handler.setEtagGenerator(resource -> "testEtag");
this.handler.afterPropertiesSet();
MockServerWebExchange exchange = MockServerWebExchange.from(
MockServerHttpRequest.get("").ifNoneMatch( "\"testEtag\""));
setPathWithinHandlerMapping(exchange, "foo.css");
setBestMachingPattern(exchange, "/**");
this.handler.handle(exchange).block(TIMEOUT);
assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.NOT_MODIFIED);
}
@Test
void shouldRespondWithModifiedResourceWhenEtagNoMatch() throws Exception {
this.handler.setEtagGenerator(resource -> "noMatch");
this.handler.afterPropertiesSet();
MockServerWebExchange exchange = MockServerWebExchange.from(
MockServerHttpRequest.get("").ifNoneMatch( "\"testEtag\""));
setPathWithinHandlerMapping(exchange, "foo.css");
setBestMachingPattern(exchange, "/**");
this.handler.handle(exchange).block(TIMEOUT);
assertThat((Object) exchange.getResponse().getStatusCode()).isNull();
assertResponseBody(exchange, "h1 { color:red; }");
}
@Test
void shouldRespondWithNotModifiedWhenEtagAndLastModified() throws Exception {
this.handler.setEtagGenerator(resource -> "testEtag");
this.handler.afterPropertiesSet();
MockServerWebExchange exchange = MockServerWebExchange.from(
MockServerHttpRequest.get("")
.ifModifiedSince(resourceLastModified("test/foo.css"))
.ifNoneMatch( "\"testEtag\""));
setPathWithinHandlerMapping(exchange, "foo.css");
setBestMachingPattern(exchange, "/**");
this.handler.handle(exchange).block(TIMEOUT);
assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.NOT_MODIFIED);
}
@Test // SPR-14005
void doOverwriteExistingCacheControlHeaders() throws Exception {
this.handler.setCacheControl(CacheControl.maxAge(3600, TimeUnit.SECONDS));
this.handler.afterPropertiesSet();