Add redirects(...) method to RestTemplateBuilder

Add `redirects(...)` method to `RestTemplateBuilder` to allow redirect
customization. This new method is required in 3.4 since the default
redirect strategy for some clients has changed and users need a way
to restore the old behavior.

See gh-43258
This commit is contained in:
Yanming Zhou
2024-11-22 17:12:46 +08:00
committed by Phillip Webb
parent 916efb632c
commit f450b28a7c
4 changed files with 65 additions and 1 deletions

View File

@@ -26,6 +26,7 @@ import java.util.stream.Stream;
import org.apache.hc.client5.http.config.RequestConfig;
import org.junit.jupiter.api.Test;
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects;
import org.springframework.boot.test.web.client.TestRestTemplate.CustomHttpComponentsClientHttpRequestFactory;
import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption;
import org.springframework.boot.web.client.RestTemplateBuilder;
@@ -66,6 +67,7 @@ import static org.mockito.Mockito.mock;
* @author Stephane Nicoll
* @author Andy Wilkinson
* @author Kristine Jetzke
* @author Yanming Zhou
*/
class TestRestTemplateTests {
@@ -129,6 +131,7 @@ class TestRestTemplateTests {
assertBasicAuthorizationCredentials(restTemplate, "user", "password");
}
@SuppressWarnings("deprecation")
@Test
void options() {
TestRestTemplate template = new TestRestTemplate(HttpClientOption.ENABLE_REDIRECTS);
@@ -139,6 +142,21 @@ class TestRestTemplateTests {
assertThat(config.isRedirectsEnabled()).isTrue();
}
@Test
void redirects() {
TestRestTemplate template = new TestRestTemplate().withRedirects(Redirects.DONT_FOLLOW);
CustomHttpComponentsClientHttpRequestFactory factory = (CustomHttpComponentsClientHttpRequestFactory) template
.getRestTemplate()
.getRequestFactory();
RequestConfig config = factory.createRequestConfig();
assertThat(config.isRedirectsEnabled()).isFalse();
template = new TestRestTemplate().withRedirects(Redirects.FOLLOW);
factory = (CustomHttpComponentsClientHttpRequestFactory) template.getRestTemplate().getRequestFactory();
config = factory.createRequestConfig();
assertThat(config.isRedirectsEnabled()).isTrue();
}
@Test
void restOperationsAreAvailable() {
RestTemplate delegate = mock(RestTemplate.class);