From 12dbe801e9ca96def592c757fdb3c95298fa401e Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 17 Apr 2025 14:20:31 -0700 Subject: [PATCH] Align `TestRestTemplate` redirect defaults Align redirect settings of `TestRestTemplate` with other blocking clients and deprecate `HttpClientOption.ENABLE_REDIRECTS`. A new `withRedirects` convenience method has also been added to quickly allow the setting to be changed. Closes gh-43431 --- .../test/web/client/TestRestTemplate.java | 21 +++++++++++++++++-- .../web/client/TestRestTemplateTests.java | 15 ++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) 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 b769640f990..67b15a9eb82 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 @@ -163,8 +163,9 @@ public class TestRestTemplate { if (requestFactoryBuilder instanceof HttpComponentsClientHttpRequestFactoryBuilder) { builder = builder.requestFactoryBuilder(applyHttpClientOptions( (HttpComponentsClientHttpRequestFactoryBuilder) requestFactoryBuilder, httpClientOptions)); - builder = builder.redirects(HttpClientOption.ENABLE_REDIRECTS.isPresent(httpClientOptions) - ? Redirects.FOLLOW : Redirects.DONT_FOLLOW); + if (HttpClientOption.ENABLE_REDIRECTS.isPresent(httpClientOptions)) { + builder = builder.redirects(Redirects.FOLLOW); + } } if (username != null || password != null) { builder = builder.basicAuthentication(username, password); @@ -973,6 +974,19 @@ public class TestRestTemplate { this.restTemplate.getUriTemplateHandler()); } + /** + * Creates a new {@code TestRestTemplate} with the same configuration as this one, + * except that it will apply the given {@link Redirects}. The request factory used is + * a new instance of the underlying {@link RestTemplate}'s request factory type (when + * possible). + * @param redirects the new redirect settings + * @return the new template + * @since 3.5.0 + */ + public TestRestTemplate withRedirects(Redirects redirects) { + return withRequestFactorySettings((settings) -> settings.withRedirects(redirects)); + } + /** * Creates a new {@code TestRestTemplate} with the same configuration as this one, * except that it will apply the given {@link ClientHttpRequestFactorySettings}. The @@ -1045,7 +1059,10 @@ public class TestRestTemplate { /** * Enable redirects. + * @deprecated since 3.5.0 for removal in 4.0.0 in favor of + * {@link TestRestTemplate#withRedirects(Redirects)} */ + @Deprecated(since = "3.5.0", forRemoval = true) ENABLE_REDIRECTS, /** 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 675b1a0c848..8b0d55a59c1 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 @@ -163,9 +163,9 @@ class TestRestTemplateTests { void httpComponentsAreBuiltConsideringSettingsInRestTemplateBuilder() { RestTemplateBuilder builder = new RestTemplateBuilder() .requestFactoryBuilder(ClientHttpRequestFactoryBuilder.httpComponents()); - assertThat(getRedirectStrategy((RestTemplateBuilder) null)).matches(this::isDontFollowStrategy); + assertThat(getRedirectStrategy((RestTemplateBuilder) null)).matches(this::isFollowStrategy); assertThat(getRedirectStrategy(null, HttpClientOption.ENABLE_REDIRECTS)).matches(this::isFollowStrategy); - assertThat(getRedirectStrategy(builder)).matches(this::isDontFollowStrategy); + assertThat(getRedirectStrategy(builder)).matches(this::isFollowStrategy); assertThat(getRedirectStrategy(builder, HttpClientOption.ENABLE_REDIRECTS)).matches(this::isFollowStrategy); assertThat(getRedirectStrategy(builder.redirects(Redirects.DONT_FOLLOW))).matches(this::isDontFollowStrategy); assertThat(getRedirectStrategy(builder.redirects(Redirects.DONT_FOLLOW), HttpClientOption.ENABLE_REDIRECTS)) @@ -175,7 +175,7 @@ class TestRestTemplateTests { @Test void withRequestFactorySettingsRedirectsForHttpComponents() { TestRestTemplate template = new TestRestTemplate(); - assertThat(getRedirectStrategy(template)).matches(this::isDontFollowStrategy); + assertThat(getRedirectStrategy(template)).matches(this::isFollowStrategy); assertThat(getRedirectStrategy(template .withRequestFactorySettings(ClientHttpRequestFactorySettings.defaults().withRedirects(Redirects.FOLLOW)))) .matches(this::isFollowStrategy); @@ -184,6 +184,15 @@ class TestRestTemplateTests { .matches(this::isDontFollowStrategy); } + @Test + void withRedirects() { + TestRestTemplate template = new TestRestTemplate(); + assertThat(getRedirectStrategy(template)).matches(this::isFollowStrategy); + assertThat(getRedirectStrategy(template.withRedirects(Redirects.FOLLOW))).matches(this::isFollowStrategy); + assertThat(getRedirectStrategy(template.withRedirects(Redirects.DONT_FOLLOW))) + .matches(this::isDontFollowStrategy); + } + @Test void withRequestFactorySettingsRedirectsForJdk() { TestRestTemplate template = new TestRestTemplate(