Skip to content

Commit

Permalink
Add customizer variant of requestFactorySettings
Browse files Browse the repository at this point in the history
Add `requestFactorySettings` that accepts a `UnaryOperator` so that
the existing `requestFactorySettings` can be customized rather than
replaced.

Closes spring-projectsgh-43258
  • Loading branch information
philwebb committed Dec 10, 2024
1 parent 52992fb commit e72546d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.UnaryOperator;

import javax.net.ssl.SSLContext;

Expand Down Expand Up @@ -974,6 +975,22 @@ public TestRestTemplate withRequestFactorySettings(ClientHttpRequestFactorySetti
this.restTemplate.getUriTemplateHandler());
}

/**
* Creates a new {@code TestRestTemplate} with the same configuration as this one,
* except that it will customize the {@link ClientHttpRequestFactorySettings}. The
* request factory used is a new instance of the underlying {@link RestTemplate}'s
* request factory type (when possible).
* @param requestFactorySettingsCustomizer a {@link UnaryOperator} to update the
* settings
* @return the new template
* @since 3.4.1
*/
public TestRestTemplate withRequestFactorySettings(
UnaryOperator<ClientHttpRequestFactorySettings> requestFactorySettingsCustomizer) {
return new TestRestTemplate(this.builder.requestFactorySettings(requestFactorySettingsCustomizer),
this.restTemplate.getUriTemplateHandler());
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private RequestEntity<?> createRequestEntityWithRootAppliedUri(RequestEntity<?> requestEntity) {
return new RequestEntity(requestEntity.getBody(), requestEntity.getHeaders(), requestEntity.getMethod(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void httpComponentsAreBuildConsideringSettingsInRestTemplateBuilder() {
}

@Test
void withSettingsUpdatesRedirectsForHttpComponents() {
void withRequestFactorySettingsRedirectsForHttpComponents() {
TestRestTemplate template = new TestRestTemplate();
assertThat(getRequestConfig(template).isRedirectsEnabled()).isFalse();
assertThat(getRequestConfig(template
Expand All @@ -177,7 +177,7 @@ void withSettingsUpdatesRedirectsForHttpComponents() {
}

@Test
void withSettingsUpdatesRedirectsForJdk() {
void withRequestFactorySettingsRedirectsForJdk() {
TestRestTemplate template = new TestRestTemplate(
new RestTemplateBuilder().requestFactoryBuilder(ClientHttpRequestFactoryBuilder.jdk()));
assertThat(getJdkHttpClient(template).followRedirects()).isEqualTo(Redirect.NORMAL);
Expand All @@ -186,6 +186,16 @@ void withSettingsUpdatesRedirectsForJdk() {
.followRedirects()).isEqualTo(Redirect.NEVER);
}

@Test
void withRequestFactorySettingsUpdateRedirectsForJdk() {
TestRestTemplate template = new TestRestTemplate(
new RestTemplateBuilder().requestFactoryBuilder(ClientHttpRequestFactoryBuilder.jdk()));
assertThat(getJdkHttpClient(template).followRedirects()).isEqualTo(Redirect.NORMAL);
assertThat(getJdkHttpClient(
template.withRequestFactorySettings((settings) -> settings.withRedirects(Redirects.DONT_FOLLOW)))
.followRedirects()).isEqualTo(Redirect.NEVER);
}

private RequestConfig getRequestConfig(RestTemplateBuilder builder, HttpClientOption... httpClientOptions) {
builder = (builder != null) ? builder : new RestTemplateBuilder();
TestRestTemplate template = new TestRestTemplate(builder, null, null, httpClientOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

import org.springframework.beans.BeanUtils;
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
Expand Down Expand Up @@ -451,6 +452,22 @@ public RestTemplateBuilder requestFactorySettings(ClientHttpRequestFactorySettin
this.requestCustomizers);
}

/**
* Update the {@link ClientHttpRequestFactorySettings} using the given customizer.
* @param requestFactorySettingsCustomizer a {@link UnaryOperator} to update request
* factory settings
* @return a new builder instance
* @since 3.4.1
*/
public RestTemplateBuilder requestFactorySettings(
UnaryOperator<ClientHttpRequestFactorySettings> requestFactorySettingsCustomizer) {
Assert.notNull(requestFactorySettingsCustomizer, "ClientHttpRequestFactorySettingsCustomizer must not be null");
return new RestTemplateBuilder(requestFactorySettingsCustomizer.apply(this.requestFactorySettings),
this.detectRequestFactory, this.rootUri, this.messageConverters, this.interceptors,
this.requestFactoryBuilder, this.uriTemplateHandler, this.errorHandler, this.basicAuthentication,
this.defaultHeaders, this.customizers, this.requestCustomizers);
}

/**
* Sets the connection timeout on the underlying {@link ClientHttpRequestFactory}.
* @param connectTimeout the connection timeout
Expand Down

0 comments on commit e72546d

Please sign in to comment.