diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java index 46c9067f8df3..c715b79fcd94 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java @@ -25,6 +25,7 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; +import java.util.function.UnaryOperator; import javax.net.ssl.SSLContext; @@ -974,6 +975,22 @@ public TestRestTemplate withRequestFactorySettings(ClientHttpRequestFactorySetti this.restTemplate.getUriTemplateHandler()); } + /** + * Creates a new {@code TestRestTemplate} with the same configuration as this one, + * except that it will customize the {@link ClientHttpRequestFactorySettings}. The + * request factory used is a new instance of the underlying {@link RestTemplate}'s + * request factory type (when possible). + * @param requestFactorySettingsCustomizer a {@link UnaryOperator} to update the + * settings + * @return the new template + * @since 3.4.1 + */ + public TestRestTemplate withRequestFactorySettings( + UnaryOperator requestFactorySettingsCustomizer) { + return new TestRestTemplate(this.builder.requestFactorySettings(requestFactorySettingsCustomizer), + this.restTemplate.getUriTemplateHandler()); + } + @SuppressWarnings({ "rawtypes", "unchecked" }) private RequestEntity createRequestEntityWithRootAppliedUri(RequestEntity requestEntity) { return new RequestEntity(requestEntity.getBody(), requestEntity.getHeaders(), requestEntity.getMethod(), diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java index dc99a59174a3..af69217f1f8e 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java @@ -168,7 +168,7 @@ void httpComponentsAreBuildConsideringSettingsInRestTemplateBuilder() { } @Test - void withSettingsUpdatesRedirectsForHttpComponents() { + void withRequestFactorySettingsRedirectsForHttpComponents() { TestRestTemplate template = new TestRestTemplate(); assertThat(getRequestConfig(template).isRedirectsEnabled()).isFalse(); assertThat(getRequestConfig(template @@ -177,7 +177,7 @@ void withSettingsUpdatesRedirectsForHttpComponents() { } @Test - void withSettingsUpdatesRedirectsForJdk() { + void withRequestFactorySettingsRedirectsForJdk() { TestRestTemplate template = new TestRestTemplate( new RestTemplateBuilder().requestFactoryBuilder(ClientHttpRequestFactoryBuilder.jdk())); assertThat(getJdkHttpClient(template).followRedirects()).isEqualTo(Redirect.NORMAL); @@ -186,6 +186,16 @@ void withSettingsUpdatesRedirectsForJdk() { .followRedirects()).isEqualTo(Redirect.NEVER); } + @Test + void withRequestFactorySettingsUpdateRedirectsForJdk() { + TestRestTemplate template = new TestRestTemplate( + new RestTemplateBuilder().requestFactoryBuilder(ClientHttpRequestFactoryBuilder.jdk())); + assertThat(getJdkHttpClient(template).followRedirects()).isEqualTo(Redirect.NORMAL); + assertThat(getJdkHttpClient( + template.withRequestFactorySettings((settings) -> settings.withRedirects(Redirects.DONT_FOLLOW))) + .followRedirects()).isEqualTo(Redirect.NEVER); + } + private RequestConfig getRequestConfig(RestTemplateBuilder builder, HttpClientOption... httpClientOptions) { builder = (builder != null) ? builder : new RestTemplateBuilder(); TestRestTemplate template = new TestRestTemplate(builder, null, null, httpClientOptions); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java index d8f574b506ef..5c9821e7dcf1 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java @@ -29,6 +29,7 @@ import java.util.Set; import java.util.function.Function; import java.util.function.Supplier; +import java.util.function.UnaryOperator; import org.springframework.beans.BeanUtils; import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder; @@ -451,6 +452,22 @@ public RestTemplateBuilder requestFactorySettings(ClientHttpRequestFactorySettin this.requestCustomizers); } + /** + * Update the {@link ClientHttpRequestFactorySettings} using the given customizer. + * @param requestFactorySettingsCustomizer a {@link UnaryOperator} to update request + * factory settings + * @return a new builder instance + * @since 3.4.1 + */ + public RestTemplateBuilder requestFactorySettings( + UnaryOperator requestFactorySettingsCustomizer) { + Assert.notNull(requestFactorySettingsCustomizer, "ClientHttpRequestFactorySettingsCustomizer must not be null"); + return new RestTemplateBuilder(requestFactorySettingsCustomizer.apply(this.requestFactorySettings), + this.detectRequestFactory, this.rootUri, this.messageConverters, this.interceptors, + this.requestFactoryBuilder, this.uriTemplateHandler, this.errorHandler, this.basicAuthentication, + this.defaultHeaders, this.customizers, this.requestCustomizers); + } + /** * Sets the connection timeout on the underlying {@link ClientHttpRequestFactory}. * @param connectTimeout the connection timeout