Polishing external contribution

See gh-29985
This commit is contained in:
Arjen Poutsma
2023-10-03 16:44:20 +02:00
parent ad50de169c
commit 307a2c7d7b
14 changed files with 237 additions and 123 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -50,7 +50,7 @@ public class ResourceHandlerFunctionTests {
private final Resource resource = new ClassPathResource("response.txt", getClass());
private final ResourceHandlerFunction handlerFunction = new ResourceHandlerFunction(this.resource);
private final ResourceHandlerFunction handlerFunction = new ResourceHandlerFunction(this.resource, (r, h) -> {});
private ServerResponse.Context context;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -17,6 +17,7 @@
package org.springframework.web.servlet.function;
import java.io.IOException;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -27,6 +28,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
@@ -118,6 +120,23 @@ class RouterFunctionBuilderTests {
assertThat(responseStatus).isEmpty();
}
@Test
public void resourcesCaching() {
Resource resource = new ClassPathResource("/org/springframework/web/servlet/function/");
assertThat(resource.exists()).isTrue();
RouterFunction<ServerResponse> route = RouterFunctions.route()
.resources("/resources/**", resource, (r, headers) -> headers.setCacheControl(CacheControl.maxAge(Duration.ofSeconds(60))))
.build();
ServerRequest resourceRequest = initRequest("GET", "/resources/response.txt");
Optional<String> responseCacheControl = route.route(resourceRequest)
.map(handlerFunction -> handle(handlerFunction, resourceRequest))
.map(response -> response.headers().getCacheControl());
assertThat(responseCacheControl).contains("max-age=60");
}
@Test
void nest() {
RouterFunction<ServerResponse> route = RouterFunctions.route()