Skip to content

Commit b7f3627

Browse files
committed
Merge pull request #43258 from quaff
* pr/43258: Polish 'Add `redirects(...)` method to `RestTemplateBuilder`' Add `redirects(...)` method to `RestTemplateBuilder` Closes gh-43258
2 parents 916efb6 + 8b83afd commit b7f3627

File tree

4 files changed

+78
-4
lines changed

4 files changed

+78
-4
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.apache.hc.core5.ssl.TrustStrategy;
4848

4949
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
50+
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects;
5051
import org.springframework.boot.web.client.RestTemplateBuilder;
5152
import org.springframework.boot.web.client.RootUriTemplateHandler;
5253
import org.springframework.core.ParameterizedTypeReference;
@@ -92,6 +93,7 @@
9293
* @author Andy Wilkinson
9394
* @author Kristine Jetzke
9495
* @author Dmytro Nosan
96+
* @author Yanming Zhou
9597
* @since 1.4.0
9698
*/
9799
public class TestRestTemplate {
@@ -1032,7 +1034,8 @@ public CustomHttpComponentsClientHttpRequestFactory(HttpClientOption[] httpClien
10321034
Set<HttpClientOption> options = new HashSet<>(Arrays.asList(httpClientOptions));
10331035
this.cookieSpec = (options.contains(HttpClientOption.ENABLE_COOKIES) ? StandardCookieSpec.STRICT
10341036
: StandardCookieSpec.IGNORE);
1035-
this.enableRedirects = options.contains(HttpClientOption.ENABLE_REDIRECTS);
1037+
this.enableRedirects = options.contains(HttpClientOption.ENABLE_REDIRECTS)
1038+
|| settings.redirects() != Redirects.DONT_FOLLOW;
10361039
boolean ssl = options.contains(HttpClientOption.SSL);
10371040
if (settings.readTimeout() != null || ssl) {
10381041
setHttpClient(createHttpClient(settings.readTimeout(), ssl));

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

+50-3
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@
2020
import java.lang.reflect.Method;
2121
import java.lang.reflect.Modifier;
2222
import java.net.URI;
23+
import java.net.http.HttpClient;
24+
import java.net.http.HttpClient.Redirect;
2325
import java.util.Base64;
2426
import java.util.stream.Stream;
2527

2628
import org.apache.hc.client5.http.config.RequestConfig;
2729
import org.junit.jupiter.api.Test;
2830

31+
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
32+
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects;
2933
import org.springframework.boot.test.web.client.TestRestTemplate.CustomHttpComponentsClientHttpRequestFactory;
3034
import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption;
3135
import org.springframework.boot.web.client.RestTemplateBuilder;
@@ -38,6 +42,7 @@
3842
import org.springframework.http.client.ClientHttpRequest;
3943
import org.springframework.http.client.ClientHttpRequestFactory;
4044
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
45+
import org.springframework.http.client.JdkClientHttpRequestFactory;
4146
import org.springframework.http.client.SimpleClientHttpRequestFactory;
4247
import org.springframework.mock.env.MockEnvironment;
4348
import org.springframework.mock.http.client.MockClientHttpRequest;
@@ -66,6 +71,7 @@
6671
* @author Stephane Nicoll
6772
* @author Andy Wilkinson
6873
* @author Kristine Jetzke
74+
* @author Yanming Zhou
6975
*/
7076
class TestRestTemplateTests {
7177

@@ -131,12 +137,53 @@ void authenticated() {
131137

132138
@Test
133139
void options() {
134-
TestRestTemplate template = new TestRestTemplate(HttpClientOption.ENABLE_REDIRECTS);
140+
RequestConfig config = getRequestConfig(
141+
new TestRestTemplate(HttpClientOption.ENABLE_REDIRECTS, HttpClientOption.ENABLE_COOKIES));
142+
assertThat(config.isRedirectsEnabled()).isTrue();
143+
assertThat(config.getCookieSpec()).isEqualTo("strict");
144+
}
145+
146+
@Test
147+
void jdkBuilderCanBeSpecifiedWithSpecificRedirects() {
148+
RestTemplateBuilder builder = new RestTemplateBuilder()
149+
.requestFactoryBuilder(ClientHttpRequestFactoryBuilder.jdk());
150+
TestRestTemplate templateWithRedirects = new TestRestTemplate(builder.redirects(Redirects.FOLLOW));
151+
assertThat(getJdkHttpClient(templateWithRedirects).followRedirects()).isEqualTo(Redirect.NORMAL);
152+
TestRestTemplate templateWithoutRedirects = new TestRestTemplate(builder.redirects(Redirects.DONT_FOLLOW));
153+
assertThat(getJdkHttpClient(templateWithoutRedirects).followRedirects()).isEqualTo(Redirect.NEVER);
154+
}
155+
156+
@Test
157+
void httpComponentsAreBuildConsideringSettingsInRestTemplateBuilder() {
158+
RestTemplateBuilder builder = new RestTemplateBuilder()
159+
.requestFactoryBuilder(ClientHttpRequestFactoryBuilder.httpComponents());
160+
assertThat(getRequestConfig((RestTemplateBuilder) null).isRedirectsEnabled()).isTrue();
161+
assertThat(getRequestConfig(null, HttpClientOption.ENABLE_REDIRECTS).isRedirectsEnabled()).isTrue();
162+
assertThat(getRequestConfig(builder).isRedirectsEnabled()).isTrue();
163+
assertThat(getRequestConfig(builder, HttpClientOption.ENABLE_REDIRECTS).isRedirectsEnabled()).isTrue();
164+
assertThat(getRequestConfig(builder.redirects(Redirects.DONT_FOLLOW)).isRedirectsEnabled()).isFalse();
165+
assertThat(getRequestConfig(builder.redirects(Redirects.DONT_FOLLOW), HttpClientOption.ENABLE_REDIRECTS)
166+
.isRedirectsEnabled()).isTrue();
167+
}
168+
169+
private RequestConfig getRequestConfig(RestTemplateBuilder builder, HttpClientOption... httpClientOptions) {
170+
builder = (builder != null) ? builder : new RestTemplateBuilder();
171+
TestRestTemplate template = new TestRestTemplate(builder, null, null, httpClientOptions);
172+
return getRequestConfig(template);
173+
}
174+
175+
private RequestConfig getRequestConfig(TestRestTemplate template) {
135176
CustomHttpComponentsClientHttpRequestFactory factory = (CustomHttpComponentsClientHttpRequestFactory) template
136177
.getRestTemplate()
137178
.getRequestFactory();
138-
RequestConfig config = factory.createRequestConfig();
139-
assertThat(config.isRedirectsEnabled()).isTrue();
179+
return factory.createRequestConfig();
180+
}
181+
182+
private HttpClient getJdkHttpClient(TestRestTemplate templateWithRedirects) {
183+
JdkClientHttpRequestFactory requestFactory = (JdkClientHttpRequestFactory) templateWithRedirects
184+
.getRestTemplate()
185+
.getRequestFactory();
186+
return (HttpClient) ReflectionTestUtils.getField(requestFactory, "httpClient");
140187
}
141188

142189
@Test

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

+15
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.beans.BeanUtils;
3434
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
3535
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
36+
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects;
3637
import org.springframework.boot.ssl.SslBundle;
3738
import org.springframework.http.client.ClientHttpRequest;
3839
import org.springframework.http.client.ClientHttpRequestFactory;
@@ -64,6 +65,7 @@
6465
* @author Kevin Strijbos
6566
* @author Ilya Lukyanovich
6667
* @author Scott Frederick
68+
* @author Yanming Zhou
6769
* @since 1.4.0
6870
*/
6971
public class RestTemplateBuilder {
@@ -501,6 +503,19 @@ public RestTemplateBuilder readTimeout(Duration readTimeout) {
501503
this.defaultHeaders, this.customizers, this.requestCustomizers);
502504
}
503505

506+
/**
507+
* Sets the redirect strategy on the underlying {@link ClientHttpRequestFactory}.
508+
* @param redirects the redirect strategy
509+
* @return a new builder instance.
510+
* @since 3.4.1
511+
*/
512+
public RestTemplateBuilder redirects(Redirects redirects) {
513+
return new RestTemplateBuilder(this.requestFactorySettings.withRedirects(redirects), this.detectRequestFactory,
514+
this.rootUri, this.messageConverters, this.interceptors, this.requestFactoryBuilder,
515+
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication, this.defaultHeaders,
516+
this.customizers, this.requestCustomizers);
517+
}
518+
504519
/**
505520
* Sets the SSL bundle on the underlying {@link ClientHttpRequestFactory}.
506521
* @param sslBundle the SSL bundle

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

+9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.mockito.junit.jupiter.MockitoExtension;
3333

3434
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
35+
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects;
3536
import org.springframework.http.HttpHeaders;
3637
import org.springframework.http.HttpMethod;
3738
import org.springframework.http.MediaType;
@@ -73,6 +74,7 @@
7374
* @author Kevin Strijbos
7475
* @author Ilya Lukyanovich
7576
* @author Brian Clozel
77+
* @author Yanming Zhou
7678
*/
7779
@ExtendWith(MockitoExtension.class)
7880
class RestTemplateBuilderTests {
@@ -486,6 +488,13 @@ void unwrappingDoesNotAffectRequestFactoryThatIsSetOnTheBuiltTemplate() {
486488
assertThat(template.getRequestFactory()).isInstanceOf(BufferingClientHttpRequestFactory.class);
487489
}
488490

491+
@Test
492+
void configureRedirects() {
493+
assertThat(this.builder.redirects(Redirects.DONT_FOLLOW)).extracting("requestFactorySettings")
494+
.extracting("redirects")
495+
.isSameAs(Redirects.DONT_FOLLOW);
496+
}
497+
489498
private ClientHttpRequest createRequest(RestTemplate template) {
490499
return ReflectionTestUtils.invokeMethod(template, "createRequest", URI.create("http://localhost"),
491500
HttpMethod.GET);

0 commit comments

Comments
 (0)