Skip to content

Commit 12dbe80

Browse files
committed
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
1 parent 7817797 commit 12dbe80

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,9 @@ private static RestTemplateBuilder createInitialBuilder(RestTemplateBuilder buil
163163
if (requestFactoryBuilder instanceof HttpComponentsClientHttpRequestFactoryBuilder) {
164164
builder = builder.requestFactoryBuilder(applyHttpClientOptions(
165165
(HttpComponentsClientHttpRequestFactoryBuilder) requestFactoryBuilder, httpClientOptions));
166-
builder = builder.redirects(HttpClientOption.ENABLE_REDIRECTS.isPresent(httpClientOptions)
167-
? Redirects.FOLLOW : Redirects.DONT_FOLLOW);
166+
if (HttpClientOption.ENABLE_REDIRECTS.isPresent(httpClientOptions)) {
167+
builder = builder.redirects(Redirects.FOLLOW);
168+
}
168169
}
169170
if (username != null || password != null) {
170171
builder = builder.basicAuthentication(username, password);
@@ -973,6 +974,19 @@ public TestRestTemplate withBasicAuth(String username, String password) {
973974
this.restTemplate.getUriTemplateHandler());
974975
}
975976

977+
/**
978+
* Creates a new {@code TestRestTemplate} with the same configuration as this one,
979+
* except that it will apply the given {@link Redirects}. The request factory used is
980+
* a new instance of the underlying {@link RestTemplate}'s request factory type (when
981+
* possible).
982+
* @param redirects the new redirect settings
983+
* @return the new template
984+
* @since 3.5.0
985+
*/
986+
public TestRestTemplate withRedirects(Redirects redirects) {
987+
return withRequestFactorySettings((settings) -> settings.withRedirects(redirects));
988+
}
989+
976990
/**
977991
* Creates a new {@code TestRestTemplate} with the same configuration as this one,
978992
* except that it will apply the given {@link ClientHttpRequestFactorySettings}. The
@@ -1045,7 +1059,10 @@ public enum HttpClientOption {
10451059

10461060
/**
10471061
* Enable redirects.
1062+
* @deprecated since 3.5.0 for removal in 4.0.0 in favor of
1063+
* {@link TestRestTemplate#withRedirects(Redirects)}
10481064
*/
1065+
@Deprecated(since = "3.5.0", forRemoval = true)
10491066
ENABLE_REDIRECTS,
10501067

10511068
/**

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ void jdkBuilderCanBeSpecifiedWithSpecificRedirects() {
163163
void httpComponentsAreBuiltConsideringSettingsInRestTemplateBuilder() {
164164
RestTemplateBuilder builder = new RestTemplateBuilder()
165165
.requestFactoryBuilder(ClientHttpRequestFactoryBuilder.httpComponents());
166-
assertThat(getRedirectStrategy((RestTemplateBuilder) null)).matches(this::isDontFollowStrategy);
166+
assertThat(getRedirectStrategy((RestTemplateBuilder) null)).matches(this::isFollowStrategy);
167167
assertThat(getRedirectStrategy(null, HttpClientOption.ENABLE_REDIRECTS)).matches(this::isFollowStrategy);
168-
assertThat(getRedirectStrategy(builder)).matches(this::isDontFollowStrategy);
168+
assertThat(getRedirectStrategy(builder)).matches(this::isFollowStrategy);
169169
assertThat(getRedirectStrategy(builder, HttpClientOption.ENABLE_REDIRECTS)).matches(this::isFollowStrategy);
170170
assertThat(getRedirectStrategy(builder.redirects(Redirects.DONT_FOLLOW))).matches(this::isDontFollowStrategy);
171171
assertThat(getRedirectStrategy(builder.redirects(Redirects.DONT_FOLLOW), HttpClientOption.ENABLE_REDIRECTS))
@@ -175,7 +175,7 @@ void httpComponentsAreBuiltConsideringSettingsInRestTemplateBuilder() {
175175
@Test
176176
void withRequestFactorySettingsRedirectsForHttpComponents() {
177177
TestRestTemplate template = new TestRestTemplate();
178-
assertThat(getRedirectStrategy(template)).matches(this::isDontFollowStrategy);
178+
assertThat(getRedirectStrategy(template)).matches(this::isFollowStrategy);
179179
assertThat(getRedirectStrategy(template
180180
.withRequestFactorySettings(ClientHttpRequestFactorySettings.defaults().withRedirects(Redirects.FOLLOW))))
181181
.matches(this::isFollowStrategy);
@@ -184,6 +184,15 @@ void withRequestFactorySettingsRedirectsForHttpComponents() {
184184
.matches(this::isDontFollowStrategy);
185185
}
186186

187+
@Test
188+
void withRedirects() {
189+
TestRestTemplate template = new TestRestTemplate();
190+
assertThat(getRedirectStrategy(template)).matches(this::isFollowStrategy);
191+
assertThat(getRedirectStrategy(template.withRedirects(Redirects.FOLLOW))).matches(this::isFollowStrategy);
192+
assertThat(getRedirectStrategy(template.withRedirects(Redirects.DONT_FOLLOW)))
193+
.matches(this::isDontFollowStrategy);
194+
}
195+
187196
@Test
188197
void withRequestFactorySettingsRedirectsForJdk() {
189198
TestRestTemplate template = new TestRestTemplate(

0 commit comments

Comments
 (0)