|
16 | 16 |
|
17 | 17 | package org.springframework.boot.http.client.autoconfigure; |
18 | 18 |
|
19 | | -import org.junit.jupiter.api.Disabled; |
| 19 | +import java.time.Duration; |
| 20 | + |
20 | 21 | import org.junit.jupiter.api.Test; |
21 | 22 |
|
| 23 | +import org.springframework.boot.http.client.HttpClientSettings; |
| 24 | +import org.springframework.boot.http.client.HttpRedirects; |
| 25 | +import org.springframework.boot.ssl.SslBundle; |
| 26 | +import org.springframework.boot.ssl.SslBundles; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
| 30 | +import static org.mockito.BDDMockito.given; |
| 31 | +import static org.mockito.Mockito.mock; |
| 32 | + |
22 | 33 | /** |
23 | 34 | * Tests for {@link HttpClientSettingsPropertyMapper}. |
24 | 35 | * |
25 | | - * @author Phillip Webb |
| 36 | + * @author Steve Armstrong |
26 | 37 | */ |
27 | | -@Disabled("TODO") |
28 | 38 | class HttpClientSettingsPropertyMapperTests { |
29 | 39 |
|
30 | 40 | @Test |
31 | | - void mapMapsProperties() { |
| 41 | + void mapWhenPropertiesIsNullAndBaseSettingsIsNullReturnsDefaults() { |
| 42 | + HttpClientSettingsPropertyMapper mapper = new HttpClientSettingsPropertyMapper(null, null); |
| 43 | + HttpClientSettings result = mapper.map(null); |
| 44 | + assertThat(result).isEqualTo(HttpClientSettings.defaults()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void mapWhenPropertiesIsNullReturnsBaseSettings() { |
| 49 | + HttpClientSettings baseSettings = HttpClientSettings.defaults().withConnectTimeout(Duration.ofSeconds(10)); |
| 50 | + HttpClientSettingsPropertyMapper mapper = new HttpClientSettingsPropertyMapper(null, baseSettings); |
| 51 | + HttpClientSettings result = mapper.map(null); |
| 52 | + assertThat(result).isEqualTo(baseSettings); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void mapMapsRedirects() { |
| 57 | + HttpClientSettingsPropertyMapper mapper = new HttpClientSettingsPropertyMapper(null, null); |
| 58 | + TestHttpClientSettingsProperties properties = new TestHttpClientSettingsProperties(); |
| 59 | + properties.setRedirects(HttpRedirects.DONT_FOLLOW); |
| 60 | + HttpClientSettings result = mapper.map(properties); |
| 61 | + assertThat(result.redirects()).isEqualTo(HttpRedirects.DONT_FOLLOW); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void mapMapsConnectTimeout() { |
| 66 | + HttpClientSettingsPropertyMapper mapper = new HttpClientSettingsPropertyMapper(null, null); |
| 67 | + TestHttpClientSettingsProperties properties = new TestHttpClientSettingsProperties(); |
| 68 | + properties.setConnectTimeout(Duration.ofSeconds(5)); |
| 69 | + HttpClientSettings result = mapper.map(properties); |
| 70 | + assertThat(result.connectTimeout()).isEqualTo(Duration.ofSeconds(5)); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void mapMapsReadTimeout() { |
| 75 | + HttpClientSettingsPropertyMapper mapper = new HttpClientSettingsPropertyMapper(null, null); |
| 76 | + TestHttpClientSettingsProperties properties = new TestHttpClientSettingsProperties(); |
| 77 | + properties.setReadTimeout(Duration.ofSeconds(30)); |
| 78 | + HttpClientSettings result = mapper.map(properties); |
| 79 | + assertThat(result.readTimeout()).isEqualTo(Duration.ofSeconds(30)); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void mapMapsSslBundle() { |
| 84 | + SslBundle sslBundle = mock(SslBundle.class); |
| 85 | + SslBundles sslBundles = mock(SslBundles.class); |
| 86 | + given(sslBundles.getBundle("test-bundle")).willReturn(sslBundle); |
| 87 | + HttpClientSettingsPropertyMapper mapper = new HttpClientSettingsPropertyMapper(sslBundles, null); |
| 88 | + TestHttpClientSettingsProperties properties = new TestHttpClientSettingsProperties(); |
| 89 | + properties.getSsl().setBundle("test-bundle"); |
| 90 | + HttpClientSettings result = mapper.map(properties); |
| 91 | + assertThat(result.sslBundle()).isSameAs(sslBundle); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void mapUsesBaseSettingsForMissingProperties() { |
| 96 | + HttpClientSettings baseSettings = new HttpClientSettings(HttpRedirects.FOLLOW_WHEN_POSSIBLE, |
| 97 | + Duration.ofSeconds(15), Duration.ofSeconds(25), null); |
| 98 | + HttpClientSettingsPropertyMapper mapper = new HttpClientSettingsPropertyMapper(null, baseSettings); |
| 99 | + TestHttpClientSettingsProperties properties = new TestHttpClientSettingsProperties(); |
| 100 | + properties.setConnectTimeout(Duration.ofSeconds(5)); |
| 101 | + HttpClientSettings result = mapper.map(properties); |
| 102 | + assertThat(result.redirects()).isEqualTo(HttpRedirects.FOLLOW_WHEN_POSSIBLE); |
| 103 | + assertThat(result.connectTimeout()).isEqualTo(Duration.ofSeconds(5)); |
| 104 | + assertThat(result.readTimeout()).isEqualTo(Duration.ofSeconds(25)); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void mapWhenSslBundleRequestedButSslBundlesIsNullThrowsException() { |
| 109 | + HttpClientSettingsPropertyMapper mapper = new HttpClientSettingsPropertyMapper(null, null); |
| 110 | + TestHttpClientSettingsProperties properties = new TestHttpClientSettingsProperties(); |
| 111 | + properties.getSsl().setBundle("test-bundle"); |
| 112 | + assertThatIllegalStateException().isThrownBy(() -> mapper.map(properties)) |
| 113 | + .withMessage("No 'sslBundles' available"); |
| 114 | + } |
| 115 | + |
| 116 | + static class TestHttpClientSettingsProperties extends HttpClientSettingsProperties { |
32 | 117 |
|
33 | 118 | } |
34 | 119 |
|
|
0 commit comments