|
| 1 | +/* |
| 2 | + * Copyright 2012-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.web.client; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.util.function.Consumer; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.mockito.Mock; |
| 25 | +import org.mockito.MockitoAnnotations; |
| 26 | + |
| 27 | +import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder; |
| 28 | +import org.springframework.boot.http.client.ClientHttpRequestFactorySettings; |
| 29 | +import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects; |
| 30 | +import org.springframework.boot.ssl.SslBundle; |
| 31 | +import org.springframework.boot.ssl.SslBundles; |
| 32 | +import org.springframework.http.client.ClientHttpRequestFactory; |
| 33 | +import org.springframework.web.client.RestClient; |
| 34 | +import org.springframework.web.client.RestClient.Builder; |
| 35 | + |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
| 37 | +import static org.mockito.BDDMockito.given; |
| 38 | +import static org.mockito.Mockito.mock; |
| 39 | + |
| 40 | +/** |
| 41 | + * Tests for {@link AutoConfiguredRestClientSsl}. |
| 42 | + * |
| 43 | + * @author Dmytro Nosan |
| 44 | + * @author Phillip Webb |
| 45 | + */ |
| 46 | +class AutoConfiguredRestClientSslTests { |
| 47 | + |
| 48 | + private final ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings |
| 49 | + .ofSslBundle(mock(SslBundle.class, "Default SslBundle")) |
| 50 | + .withRedirects(Redirects.DONT_FOLLOW) |
| 51 | + .withReadTimeout(Duration.ofSeconds(10)) |
| 52 | + .withConnectTimeout(Duration.ofSeconds(30)); |
| 53 | + |
| 54 | + @Mock |
| 55 | + private SslBundles sslBundles; |
| 56 | + |
| 57 | + @Mock |
| 58 | + private ClientHttpRequestFactoryBuilder<ClientHttpRequestFactory> factoryBuilder; |
| 59 | + |
| 60 | + @Mock |
| 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 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void shouldConfigureRestClientUsingBundleName() { |
| 73 | + String bundleName = "test"; |
| 74 | + SslBundle sslBundle = mock(SslBundle.class, "SslBundle named '%s'".formatted(bundleName)); |
| 75 | + given(this.sslBundles.getBundle(bundleName)).willReturn(sslBundle); |
| 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); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void shouldConfigureRestClientUsingBundle() { |
| 83 | + SslBundle sslBundle = mock(SslBundle.class, "Custom SslBundle"); |
| 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); |
| 87 | + } |
| 88 | + |
| 89 | + private RestClient build(Consumer<RestClient.Builder> customizer) { |
| 90 | + Builder builder = RestClient.builder(); |
| 91 | + customizer.accept(builder); |
| 92 | + return builder.build(); |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments