Skip to content

Commit 567353a

Browse files
committed
Polish 'Update RestClientSsl to support ClientHttpRequestFactorySettings'
See gh-44979
1 parent af5d72b commit 567353a

File tree

4 files changed

+42
-45
lines changed

4 files changed

+42
-45
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@
2929
* An auto-configured {@link RestClientSsl} implementation.
3030
*
3131
* @author Phillip Webb
32+
* @author Dmytro Nosan
3233
*/
3334
class AutoConfiguredRestClientSsl implements RestClientSsl {
3435

35-
private final ClientHttpRequestFactoryBuilder<?> clientHttpRequestFactoryBuilder;
36+
private final ClientHttpRequestFactoryBuilder<?> builder;
3637

37-
private final ClientHttpRequestFactorySettings clientHttpRequestFactorySettings;
38+
private final ClientHttpRequestFactorySettings settings;
3839

3940
private final SslBundles sslBundles;
4041

4142
AutoConfiguredRestClientSsl(ClientHttpRequestFactoryBuilder<?> clientHttpRequestFactoryBuilder,
4243
ClientHttpRequestFactorySettings clientHttpRequestFactorySettings, SslBundles sslBundles) {
43-
this.clientHttpRequestFactoryBuilder = clientHttpRequestFactoryBuilder;
44-
this.clientHttpRequestFactorySettings = clientHttpRequestFactorySettings;
44+
this.builder = clientHttpRequestFactoryBuilder;
45+
this.settings = clientHttpRequestFactorySettings;
4546
this.sslBundles = sslBundles;
4647
}
4748

@@ -52,11 +53,11 @@ public Consumer<RestClient.Builder> fromBundle(String bundleName) {
5253

5354
@Override
5455
public Consumer<RestClient.Builder> fromBundle(SslBundle bundle) {
55-
return (builder) -> {
56-
ClientHttpRequestFactorySettings settings = this.clientHttpRequestFactorySettings.withSslBundle(bundle);
57-
ClientHttpRequestFactory requestFactory = this.clientHttpRequestFactoryBuilder.build(settings);
58-
builder.requestFactory(requestFactory);
59-
};
56+
return (builder) -> builder.requestFactory(requestFactory(bundle));
57+
}
58+
59+
private ClientHttpRequestFactory requestFactory(SslBundle bundle) {
60+
return this.builder.build(this.settings.withSslBundle(bundle));
6061
}
6162

6263
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.function.Consumer;
2020

2121
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
22+
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
2223
import org.springframework.boot.ssl.NoSuchSslBundleException;
2324
import org.springframework.boot.ssl.SslBundle;
2425
import org.springframework.http.client.ClientHttpRequestFactory;
@@ -34,8 +35,11 @@
3435
* RestClient restClient = restClientBuilder.apply(ssl.fromBundle("mybundle")).build();
3536
* return new MyBean(restClient);
3637
* }
37-
* </pre> NOTE: Apply SSL configuration will replace any previously
38+
* </pre> NOTE: Applying SSL configuration will replace any previously
3839
* {@link RestClient.Builder#requestFactory configured} {@link ClientHttpRequestFactory}.
40+
* The replacement {@link ClientHttpRequestFactory} will apply only configured
41+
* {@link ClientHttpRequestFactorySettings} and the appropriate {@link SslBundle}.
42+
* <p>
3943
* If you need to configure {@link ClientHttpRequestFactory} with more than just SSL
4044
* consider using a {@link ClientHttpRequestFactoryBuilder}.
4145
*

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

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818

1919
import java.time.Duration;
2020
import java.util.function.Consumer;
21-
import java.util.function.Function;
2221

22+
import org.junit.jupiter.api.BeforeEach;
2323
import org.junit.jupiter.api.Test;
24-
import org.junit.jupiter.api.extension.ExtendWith;
2524
import org.mockito.Mock;
26-
import org.mockito.junit.jupiter.MockitoExtension;
25+
import org.mockito.MockitoAnnotations;
2726

2827
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
2928
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
@@ -42,11 +41,11 @@
4241
* Tests for {@link AutoConfiguredRestClientSsl}.
4342
*
4443
* @author Dmytro Nosan
44+
* @author Phillip Webb
4545
*/
46-
@ExtendWith(MockitoExtension.class)
4746
class AutoConfiguredRestClientSslTests {
4847

49-
private final ClientHttpRequestFactorySettings clientHttpRequestFactorySettings = ClientHttpRequestFactorySettings
48+
private final ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings
5049
.ofSslBundle(mock(SslBundle.class, "Default SslBundle"))
5150
.withRedirects(Redirects.DONT_FOLLOW)
5251
.withReadTimeout(Duration.ofSeconds(10))
@@ -56,47 +55,41 @@ class AutoConfiguredRestClientSslTests {
5655
private SslBundles sslBundles;
5756

5857
@Mock
59-
private ClientHttpRequestFactoryBuilder<ClientHttpRequestFactory> clientHttpRequestFactoryBuilder;
58+
private ClientHttpRequestFactoryBuilder<ClientHttpRequestFactory> factoryBuilder;
6059

6160
@Mock
62-
private ClientHttpRequestFactory clientHttpRequestFactory;
61+
private ClientHttpRequestFactory factory;
62+
63+
private AutoConfiguredRestClientSsl restClientSsl;
64+
65+
@BeforeEach
66+
void setup() {
67+
MockitoAnnotations.openMocks(this);
68+
this.restClientSsl = new AutoConfiguredRestClientSsl(this.factoryBuilder, this.settings, this.sslBundles);
69+
}
6370

6471
@Test
6572
void shouldConfigureRestClientUsingBundleName() {
6673
String bundleName = "test";
6774
SslBundle sslBundle = mock(SslBundle.class, "SslBundle named '%s'".formatted(bundleName));
68-
6975
given(this.sslBundles.getBundle(bundleName)).willReturn(sslBundle);
70-
given(this.clientHttpRequestFactoryBuilder
71-
.build(this.clientHttpRequestFactorySettings.withSslBundle(sslBundle)))
72-
.willReturn(this.clientHttpRequestFactory);
73-
74-
assertThat(applySslBundle((restClientSsl) -> restClientSsl.fromBundle(bundleName)))
75-
.hasFieldOrPropertyWithValue("clientRequestFactory", this.clientHttpRequestFactory);
76-
76+
given(this.factoryBuilder.build(this.settings.withSslBundle(sslBundle))).willReturn(this.factory);
77+
RestClient restClient = build(this.restClientSsl.fromBundle(bundleName));
78+
assertThat(restClient).hasFieldOrPropertyWithValue("clientRequestFactory", this.factory);
7779
}
7880

7981
@Test
8082
void shouldConfigureRestClientUsingBundle() {
8183
SslBundle sslBundle = mock(SslBundle.class, "Custom SslBundle");
82-
83-
given(this.clientHttpRequestFactoryBuilder
84-
.build(this.clientHttpRequestFactorySettings.withSslBundle(sslBundle)))
85-
.willReturn(this.clientHttpRequestFactory);
86-
87-
assertThat(applySslBundle((restClientSsl) -> restClientSsl.fromBundle(sslBundle)))
88-
.hasFieldOrPropertyWithValue("clientRequestFactory", this.clientHttpRequestFactory);
84+
given(this.factoryBuilder.build(this.settings.withSslBundle(sslBundle))).willReturn(this.factory);
85+
RestClient restClient = build(this.restClientSsl.fromBundle(sslBundle));
86+
assertThat(restClient).hasFieldOrPropertyWithValue("clientRequestFactory", this.factory);
8987
}
9088

91-
private RestClient applySslBundle(Function<RestClientSsl, Consumer<Builder>> applySslBundle) {
89+
private RestClient build(Consumer<RestClient.Builder> customizer) {
9290
Builder builder = RestClient.builder();
93-
applySslBundle.apply(getRestClientSsl()).accept(builder);
91+
customizer.accept(builder);
9492
return builder.build();
9593
}
9694

97-
private RestClientSsl getRestClientSsl() {
98-
return new AutoConfiguredRestClientSsl(this.clientHttpRequestFactoryBuilder,
99-
this.clientHttpRequestFactorySettings, this.sslBundles);
100-
}
101-
10295
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
*
5353
* @author Arjen Poutsma
5454
* @author Moritz Halbritter
55+
* @author Dmytro Nosan
5556
*/
5657
class RestClientAutoConfigurationTests {
5758

@@ -84,10 +85,8 @@ void shouldSupplyRestClientSslIfSslBundlesIsThereWithCustomHttpSettingsAndBuilde
8485
assertThat(context).hasSingleBean(RestClientSsl.class);
8586
RestClientSsl restClientSsl = context.getBean(RestClientSsl.class);
8687
assertThat(restClientSsl).hasFieldOrPropertyWithValue("sslBundles", sslBundles);
87-
assertThat(restClientSsl).hasFieldOrPropertyWithValue("clientHttpRequestFactoryBuilder",
88-
clientHttpRequestFactoryBuilder);
89-
assertThat(restClientSsl).hasFieldOrPropertyWithValue("clientHttpRequestFactorySettings",
90-
clientHttpRequestFactorySettings);
88+
assertThat(restClientSsl).hasFieldOrPropertyWithValue("builder", clientHttpRequestFactoryBuilder);
89+
assertThat(restClientSsl).hasFieldOrPropertyWithValue("settings", clientHttpRequestFactorySettings);
9190
});
9291
}
9392

@@ -100,9 +99,9 @@ void shouldSupplyRestClientSslIfSslBundlesIsThereWithAutoConfiguredHttpSettingsA
10099
.hasSingleBean(ClientHttpRequestFactoryBuilder.class);
101100
RestClientSsl restClientSsl = context.getBean(RestClientSsl.class);
102101
assertThat(restClientSsl).hasFieldOrPropertyWithValue("sslBundles", sslBundles);
103-
assertThat(restClientSsl).hasFieldOrPropertyWithValue("clientHttpRequestFactoryBuilder",
102+
assertThat(restClientSsl).hasFieldOrPropertyWithValue("builder",
104103
context.getBean(ClientHttpRequestFactoryBuilder.class));
105-
assertThat(restClientSsl).hasFieldOrPropertyWithValue("clientHttpRequestFactorySettings",
104+
assertThat(restClientSsl).hasFieldOrPropertyWithValue("settings",
106105
context.getBean(ClientHttpRequestFactorySettings.class));
107106
});
108107
}

0 commit comments

Comments
 (0)