Skip to content
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

Polish RestClientAutoConfiguration and RestClientBuilderConfigurer #44982

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
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -79,11 +79,10 @@ RestClientBuilderConfigurer restClientBuilderConfigurer(
ObjectProvider<ClientHttpRequestFactoryBuilder<?>> clientHttpRequestFactoryBuilder,
ObjectProvider<ClientHttpRequestFactorySettings> clientHttpRequestFactorySettings,
ObjectProvider<RestClientCustomizer> customizerProvider) {
RestClientBuilderConfigurer configurer = new RestClientBuilderConfigurer();
configurer.setRequestFactoryBuilder(clientHttpRequestFactoryBuilder.getIfAvailable());
configurer.setRequestFactorySettings(clientHttpRequestFactorySettings.getIfAvailable());
configurer.setRestClientCustomizers(customizerProvider.orderedStream().toList());
return configurer;
return new RestClientBuilderConfigurer(
clientHttpRequestFactoryBuilder.getIfAvailable(ClientHttpRequestFactoryBuilder::detect),
clientHttpRequestFactorySettings.getIfAvailable(ClientHttpRequestFactorySettings::defaults),
customizerProvider.orderedStream().toList());
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@

package org.springframework.boot.autoconfigure.web.client;

import java.util.Collections;
import java.util.List;

import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
Expand All @@ -32,21 +33,21 @@
*/
public class RestClientBuilderConfigurer {

private ClientHttpRequestFactoryBuilder<?> requestFactoryBuilder;
private final ClientHttpRequestFactoryBuilder<?> requestFactoryBuilder;

private ClientHttpRequestFactorySettings requestFactorySettings;
private final ClientHttpRequestFactorySettings requestFactorySettings;

private List<RestClientCustomizer> customizers;
private final List<RestClientCustomizer> customizers;

void setRequestFactoryBuilder(ClientHttpRequestFactoryBuilder<?> requestFactoryBuilder) {
this.requestFactoryBuilder = requestFactoryBuilder;
public RestClientBuilderConfigurer() {
this(ClientHttpRequestFactoryBuilder.detect(), ClientHttpRequestFactorySettings.defaults(),
Collections.emptyList());
}

void setRequestFactorySettings(ClientHttpRequestFactorySettings requestFactorySettings) {
RestClientBuilderConfigurer(ClientHttpRequestFactoryBuilder<?> requestFactoryBuilder,
ClientHttpRequestFactorySettings requestFactorySettings, List<RestClientCustomizer> customizers) {
this.requestFactoryBuilder = requestFactoryBuilder;
this.requestFactorySettings = requestFactorySettings;
}

void setRestClientCustomizers(List<RestClientCustomizer> customizers) {
this.customizers = customizers;
}

Expand All @@ -57,18 +58,14 @@ void setRestClientCustomizers(List<RestClientCustomizer> customizers) {
* @return the configured builder
*/
public RestClient.Builder configure(RestClient.Builder builder) {
ClientHttpRequestFactoryBuilder<?> requestFactoryBuilder = (this.requestFactoryBuilder != null)
? this.requestFactoryBuilder : ClientHttpRequestFactoryBuilder.detect();
builder.requestFactory(requestFactoryBuilder.build(this.requestFactorySettings));
builder.requestFactory(this.requestFactoryBuilder.build(this.requestFactorySettings));
applyCustomizers(builder);
return builder;
}

private void applyCustomizers(Builder builder) {
if (this.customizers != null) {
for (RestClientCustomizer customizer : this.customizers) {
customizer.customize(builder);
}
for (RestClientCustomizer customizer : this.customizers) {
customizer.customize(builder);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,9 @@
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.http.client.HttpClientAutoConfiguration;
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.SslBundles;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.web.client.RestClientCustomizer;
Expand Down Expand Up @@ -171,6 +174,57 @@ void whenHasFactoryProperty() {
});
}

@Test
void shouldSupplyRestClientBuilderConfigurerWithCustomSettings() {
ClientHttpRequestFactorySettings clientHttpRequestFactorySettings = ClientHttpRequestFactorySettings.defaults()
.withRedirects(Redirects.DONT_FOLLOW);
ClientHttpRequestFactoryBuilder<?> clientHttpRequestFactoryBuilder = mock(
ClientHttpRequestFactoryBuilder.class);
RestClientCustomizer customizer1 = mock(RestClientCustomizer.class);
RestClientCustomizer customizer2 = mock(RestClientCustomizer.class);
HttpMessageConvertersRestClientCustomizer httpMessageConverterCustomizer = mock(
HttpMessageConvertersRestClientCustomizer.class);
this.contextRunner.withBean(ClientHttpRequestFactorySettings.class, () -> clientHttpRequestFactorySettings)
.withBean(ClientHttpRequestFactoryBuilder.class, () -> clientHttpRequestFactoryBuilder)
.withBean("customizer1", RestClientCustomizer.class, () -> customizer1)
.withBean("customizer2", RestClientCustomizer.class, () -> customizer2)
.withBean("httpMessageConverterCustomizer", HttpMessageConvertersRestClientCustomizer.class,
() -> httpMessageConverterCustomizer)
.run((context) -> {
assertThat(context).hasSingleBean(RestClientBuilderConfigurer.class)
.hasSingleBean(ClientHttpRequestFactorySettings.class)
.hasSingleBean(ClientHttpRequestFactoryBuilder.class);
RestClientBuilderConfigurer configurer = context.getBean(RestClientBuilderConfigurer.class);
assertThat(configurer).hasFieldOrPropertyWithValue("requestFactoryBuilder",
clientHttpRequestFactoryBuilder);
assertThat(configurer).hasFieldOrPropertyWithValue("requestFactorySettings",
clientHttpRequestFactorySettings);
assertThat(configurer).hasFieldOrPropertyWithValue("customizers",
List.of(customizer1, customizer2, httpMessageConverterCustomizer));
});
}

@Test
void shouldSupplyRestClientBuilderConfigurerWithAutoConfiguredHttpSettings() {
RestClientCustomizer customizer1 = mock(RestClientCustomizer.class);
RestClientCustomizer customizer2 = mock(RestClientCustomizer.class);
this.contextRunner.withBean("customizer1", RestClientCustomizer.class, () -> customizer1)
.withBean("customizer2", RestClientCustomizer.class, () -> customizer2)
.run((context) -> {
assertThat(context).hasSingleBean(RestClientBuilderConfigurer.class)
.hasSingleBean(ClientHttpRequestFactorySettings.class)
.hasSingleBean(ClientHttpRequestFactoryBuilder.class)
.hasSingleBean(HttpMessageConvertersRestClientCustomizer.class);
RestClientBuilderConfigurer configurer = context.getBean(RestClientBuilderConfigurer.class);
assertThat(configurer).hasFieldOrPropertyWithValue("requestFactoryBuilder",
context.getBean(ClientHttpRequestFactoryBuilder.class));
assertThat(configurer).hasFieldOrPropertyWithValue("requestFactorySettings",
context.getBean(ClientHttpRequestFactorySettings.class));
assertThat(configurer).hasFieldOrPropertyWithValue("customizers", List.of(customizer1, customizer2,
context.getBean(HttpMessageConvertersRestClientCustomizer.class)));
});
}

@Configuration(proxyBeanMethods = false)
static class CodecConfiguration {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,11 +19,19 @@
import java.util.List;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
import org.springframework.boot.ssl.SslBundle;
import org.springframework.boot.web.client.RestClientCustomizer;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.web.client.RestClient;

import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;

Expand All @@ -32,23 +40,29 @@
*
* @author Moritz Halbritter
*/
@ExtendWith(MockitoExtension.class)
class RestClientBuilderConfigurerTests {

@Mock
private ClientHttpRequestFactoryBuilder<ClientHttpRequestFactory> clientHttpRequestFactoryBuilder;

@Mock
private ClientHttpRequestFactory clientHttpRequestFactory;

@Test
void shouldApplyCustomizers() {
RestClientBuilderConfigurer configurer = new RestClientBuilderConfigurer();
void shouldConfigureRestClientBuilder() {
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.ofSslBundle(mock(SslBundle.class));
RestClientCustomizer customizer = mock(RestClientCustomizer.class);
configurer.setRestClientCustomizers(List.of(customizer));
RestClientCustomizer customizer1 = mock(RestClientCustomizer.class);
RestClientBuilderConfigurer configurer = new RestClientBuilderConfigurer(this.clientHttpRequestFactoryBuilder,
settings, List.of(customizer, customizer1));
given(this.clientHttpRequestFactoryBuilder.build(settings)).willReturn(this.clientHttpRequestFactory);

RestClient.Builder builder = RestClient.builder();
configurer.configure(builder);
assertThat(builder.build()).hasFieldOrPropertyWithValue("clientRequestFactory", this.clientHttpRequestFactory);
then(customizer).should().customize(builder);
}

@Test
void shouldSupportNullAsCustomizers() {
RestClientBuilderConfigurer configurer = new RestClientBuilderConfigurer();
configurer.setRestClientCustomizers(null);
assertThatCode(() -> configurer.configure(RestClient.builder())).doesNotThrowAnyException();
then(customizer1).should().customize(builder);
}

}