Browse Source

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
pull/45309/head
Phillip Webb 8 months ago
parent
commit
12dbe801e9
  1. 21
      spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java
  2. 15
      spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java

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

@ -163,8 +163,9 @@ public class TestRestTemplate { @@ -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 { @@ -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 { @@ -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,
/**

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

@ -163,9 +163,9 @@ class TestRestTemplateTests { @@ -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 { @@ -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 { @@ -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(

Loading…
Cancel
Save