Skip to content

TestRestTemplate does not allow redirects to be customized #43258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.apache.hc.core5.ssl.TrustStrategy;

import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.client.RootUriTemplateHandler;
import org.springframework.core.ParameterizedTypeReference;
Expand Down Expand Up @@ -92,6 +93,7 @@
* @author Andy Wilkinson
* @author Kristine Jetzke
* @author Dmytro Nosan
* @author Yanming Zhou
* @since 1.4.0
*/
public class TestRestTemplate {
Expand Down Expand Up @@ -946,6 +948,22 @@ public TestRestTemplate withBasicAuth(String username, String password) {
return template;
}

/**
* Creates a new {@code TestRestTemplate} with the same configuration as this one,
* except that it will use the given {@code redirects} strategies. The request factory
* used is a new instance of the underlying {@link RestTemplate}'s request factory
* type (when possible).
* @param redirects the redirects
* @return the new template
* @since 3.4.1
*/
public TestRestTemplate withRedirects(Redirects redirects) {
TestRestTemplate template = new TestRestTemplate(this.builder.redirects(redirects), null, null,
this.httpClientOptions);
template.setUriTemplateHandler(getRestTemplate().getUriTemplateHandler());
return template;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private RequestEntity<?> createRequestEntityWithRootAppliedUri(RequestEntity<?> requestEntity) {
return new RequestEntity(requestEntity.getBody(), requestEntity.getHeaders(), requestEntity.getMethod(),
Expand Down Expand Up @@ -988,7 +1006,10 @@ public enum HttpClientOption {

/**
* Enable redirects.
* @deprecated since 3.4.1 for removal in 3.6.0 in favor of
* {@link #withRedirects(Redirects)}
*/
@Deprecated(since = "3.4.1", forRemoval = true)
ENABLE_REDIRECTS,

/**
Expand Down Expand Up @@ -1032,7 +1053,8 @@ public CustomHttpComponentsClientHttpRequestFactory(HttpClientOption[] httpClien
Set<HttpClientOption> options = new HashSet<>(Arrays.asList(httpClientOptions));
this.cookieSpec = (options.contains(HttpClientOption.ENABLE_COOKIES) ? StandardCookieSpec.STRICT
: StandardCookieSpec.IGNORE);
this.enableRedirects = options.contains(HttpClientOption.ENABLE_REDIRECTS);
this.enableRedirects = options.contains(HttpClientOption.ENABLE_REDIRECTS)
|| settings.redirects() != Redirects.DONT_FOLLOW;
boolean ssl = options.contains(HttpClientOption.SSL);
if (settings.readTimeout() != null || ssl) {
setHttpClient(createHttpClient(settings.readTimeout(), ssl));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -66,6 +67,7 @@
* @author Stephane Nicoll
* @author Andy Wilkinson
* @author Kristine Jetzke
* @author Yanming Zhou
*/
class TestRestTemplateTests {

Expand Down Expand Up @@ -129,6 +131,7 @@ void authenticated() {
assertBasicAuthorizationCredentials(restTemplate, "user", "password");
}

@SuppressWarnings("deprecation")
@Test
void options() {
TestRestTemplate template = new TestRestTemplate(HttpClientOption.ENABLE_REDIRECTS);
Expand All @@ -139,6 +142,21 @@ void options() {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.beans.BeanUtils;
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.ssl.SslBundle;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
Expand Down Expand Up @@ -64,6 +65,7 @@
* @author Kevin Strijbos
* @author Ilya Lukyanovich
* @author Scott Frederick
* @author Yanming Zhou
* @since 1.4.0
*/
public class RestTemplateBuilder {
Expand Down Expand Up @@ -501,6 +503,19 @@ public RestTemplateBuilder readTimeout(Duration readTimeout) {
this.defaultHeaders, this.customizers, this.requestCustomizers);
}

/**
* Sets the redirect strategy on the underlying {@link ClientHttpRequestFactory}.
* @param redirects the redirect strategy
* @return a new builder instance.
* @since 3.4.1
*/
public RestTemplateBuilder redirects(Redirects redirects) {
return new RestTemplateBuilder(this.requestFactorySettings.withRedirects(redirects), this.detectRequestFactory,
this.rootUri, this.messageConverters, this.interceptors, this.requestFactoryBuilder,
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication, this.defaultHeaders,
this.customizers, this.requestCustomizers);
}

/**
* Sets the SSL bundle on the underlying {@link ClientHttpRequestFactory}.
* @param sslBundle the SSL bundle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.mockito.junit.jupiter.MockitoExtension;

import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -73,6 +74,7 @@
* @author Kevin Strijbos
* @author Ilya Lukyanovich
* @author Brian Clozel
* @author Yanming Zhou
*/
@ExtendWith(MockitoExtension.class)
class RestTemplateBuilderTests {
Expand Down Expand Up @@ -486,6 +488,13 @@ void unwrappingDoesNotAffectRequestFactoryThatIsSetOnTheBuiltTemplate() {
assertThat(template.getRequestFactory()).isInstanceOf(BufferingClientHttpRequestFactory.class);
}

@Test
void configureRedirects() {
assertThat(this.builder.redirects(Redirects.DONT_FOLLOW)).extracting("requestFactorySettings")
.extracting("redirects")
.isSameAs(Redirects.DONT_FOLLOW);
}

private ClientHttpRequest createRequest(RestTemplate template) {
return ReflectionTestUtils.invokeMethod(template, "createRequest", URI.create("http://localhost"),
HttpMethod.GET);
Expand Down
Loading