Replace ClientHttpRequestFactorySettings.Redirects with HttpRedirects

Closes gh-45505
This commit is contained in:
Phillip Webb
2025-05-12 21:57:26 -07:00
parent 8f6a9b9440
commit 7b08c07e7e
25 changed files with 78 additions and 132 deletions

View File

@@ -49,8 +49,8 @@ import org.apache.hc.core5.ssl.TrustStrategy;
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects;
import org.springframework.boot.http.client.HttpComponentsClientHttpRequestFactoryBuilder;
import org.springframework.boot.http.client.HttpRedirects;
import org.springframework.boot.ssl.SslBundle;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.client.RootUriTemplateHandler;
@@ -164,7 +164,7 @@ public class TestRestTemplate {
builder = builder.requestFactoryBuilder(applyHttpClientOptions(
(HttpComponentsClientHttpRequestFactoryBuilder) requestFactoryBuilder, httpClientOptions));
if (HttpClientOption.ENABLE_REDIRECTS.isPresent(httpClientOptions)) {
builder = builder.redirects(Redirects.FOLLOW);
builder = builder.redirects(HttpRedirects.FOLLOW);
}
}
if (username != null || password != null) {
@@ -976,14 +976,14 @@ public class TestRestTemplate {
/**
* 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).
* except that it will apply the given {@link HttpRedirects}. 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) {
public TestRestTemplate withRedirects(HttpRedirects redirects) {
return withRequestFactorySettings((settings) -> settings.withRedirects(redirects));
}
@@ -1060,7 +1060,7 @@ public class TestRestTemplate {
/**
* Enable redirects.
* @deprecated since 3.5.0 for removal in 4.0.0 in favor of
* {@link TestRestTemplate#withRedirects(Redirects)}
* {@link TestRestTemplate#withRedirects(HttpRedirects)}
*/
@Deprecated(since = "3.5.0", forRemoval = true)
ENABLE_REDIRECTS,
@@ -1112,7 +1112,7 @@ public class TestRestTemplate {
ClientHttpRequestFactorySettings settings) {
this.cookieSpec = (HttpClientOption.ENABLE_COOKIES.isPresent(httpClientOptions) ? StandardCookieSpec.STRICT
: StandardCookieSpec.IGNORE);
this.enableRedirects = settings.redirects() != Redirects.DONT_FOLLOW;
this.enableRedirects = settings.redirects() != HttpRedirects.DONT_FOLLOW;
boolean ssl = HttpClientOption.SSL.isPresent(httpClientOptions);
if (settings.readTimeout() != null || ssl) {
setHttpClient(createHttpClient(settings.readTimeout(), ssl));

View File

@@ -34,7 +34,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects;
import org.springframework.boot.http.client.HttpRedirects;
import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.core.ParameterizedTypeReference;
@@ -152,9 +152,9 @@ class TestRestTemplateTests {
void jdkBuilderCanBeSpecifiedWithSpecificRedirects() {
RestTemplateBuilder builder = new RestTemplateBuilder()
.requestFactoryBuilder(ClientHttpRequestFactoryBuilder.jdk());
TestRestTemplate templateWithRedirects = new TestRestTemplate(builder.redirects(Redirects.FOLLOW));
TestRestTemplate templateWithRedirects = new TestRestTemplate(builder.redirects(HttpRedirects.FOLLOW));
assertThat(getJdkHttpClient(templateWithRedirects).followRedirects()).isEqualTo(Redirect.NORMAL);
TestRestTemplate templateWithoutRedirects = new TestRestTemplate(builder.redirects(Redirects.DONT_FOLLOW));
TestRestTemplate templateWithoutRedirects = new TestRestTemplate(builder.redirects(HttpRedirects.DONT_FOLLOW));
assertThat(getJdkHttpClient(templateWithoutRedirects).followRedirects()).isEqualTo(Redirect.NEVER);
}
@@ -167,8 +167,9 @@ class TestRestTemplateTests {
assertThat(getRedirectStrategy(null, HttpClientOption.ENABLE_REDIRECTS)).matches(this::isFollowStrategy);
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))
assertThat(getRedirectStrategy(builder.redirects(HttpRedirects.DONT_FOLLOW)))
.matches(this::isDontFollowStrategy);
assertThat(getRedirectStrategy(builder.redirects(HttpRedirects.DONT_FOLLOW), HttpClientOption.ENABLE_REDIRECTS))
.matches(this::isFollowStrategy);
}
@@ -176,11 +177,11 @@ class TestRestTemplateTests {
void withRequestFactorySettingsRedirectsForHttpComponents() {
TestRestTemplate template = new TestRestTemplate();
assertThat(getRedirectStrategy(template)).matches(this::isFollowStrategy);
assertThat(getRedirectStrategy(template
.withRequestFactorySettings(ClientHttpRequestFactorySettings.defaults().withRedirects(Redirects.FOLLOW))))
assertThat(getRedirectStrategy(template.withRequestFactorySettings(
ClientHttpRequestFactorySettings.defaults().withRedirects(HttpRedirects.FOLLOW))))
.matches(this::isFollowStrategy);
assertThat(getRedirectStrategy(template.withRequestFactorySettings(
ClientHttpRequestFactorySettings.defaults().withRedirects(Redirects.DONT_FOLLOW))))
ClientHttpRequestFactorySettings.defaults().withRedirects(HttpRedirects.DONT_FOLLOW))))
.matches(this::isDontFollowStrategy);
}
@@ -188,8 +189,8 @@ class TestRestTemplateTests {
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)))
assertThat(getRedirectStrategy(template.withRedirects(HttpRedirects.FOLLOW))).matches(this::isFollowStrategy);
assertThat(getRedirectStrategy(template.withRedirects(HttpRedirects.DONT_FOLLOW)))
.matches(this::isDontFollowStrategy);
}
@@ -199,7 +200,7 @@ class TestRestTemplateTests {
new RestTemplateBuilder().requestFactoryBuilder(ClientHttpRequestFactoryBuilder.jdk()));
assertThat(getJdkHttpClient(template).followRedirects()).isEqualTo(Redirect.NORMAL);
assertThat(getJdkHttpClient(template.withRequestFactorySettings(
ClientHttpRequestFactorySettings.defaults().withRedirects(Redirects.DONT_FOLLOW)))
ClientHttpRequestFactorySettings.defaults().withRedirects(HttpRedirects.DONT_FOLLOW)))
.followRedirects()).isEqualTo(Redirect.NEVER);
}
@@ -209,7 +210,7 @@ class TestRestTemplateTests {
new RestTemplateBuilder().requestFactoryBuilder(ClientHttpRequestFactoryBuilder.jdk()));
assertThat(getJdkHttpClient(template).followRedirects()).isEqualTo(Redirect.NORMAL);
assertThat(getJdkHttpClient(
template.withRequestFactorySettings((settings) -> settings.withRedirects(Redirects.DONT_FOLLOW)))
template.withRequestFactorySettings((settings) -> settings.withRedirects(HttpRedirects.DONT_FOLLOW)))
.followRedirects()).isEqualTo(Redirect.NEVER);
}