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
This commit is contained in:
Phillip Webb
2025-04-17 14:20:31 -07:00
parent 7817797970
commit 12dbe801e9
2 changed files with 31 additions and 5 deletions

View File

@@ -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(