Skip to content

Commit 82fe577

Browse files
stevearmstrong-devsnicoll
authored andcommitted
Add missing HttpClientSettingsPropertyMapper tests
See gh-48145 Signed-off-by: Steve Armstrong <[email protected]>
1 parent 2e4566b commit 82fe577

File tree

1 file changed

+107
-3
lines changed

1 file changed

+107
-3
lines changed

module/spring-boot-http-client/src/test/java/org/springframework/boot/http/client/autoconfigure/HttpClientSettingsPropertyMapperTests.java

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,123 @@
1616

1717
package org.springframework.boot.http.client.autoconfigure;
1818

19-
import org.junit.jupiter.api.Disabled;
19+
import java.time.Duration;
20+
2021
import org.junit.jupiter.api.Test;
2122

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+
2233
/**
2334
* Tests for {@link HttpClientSettingsPropertyMapper}.
2435
*
2536
* @author Phillip Webb
2637
*/
27-
@Disabled("TODO")
2838
class HttpClientSettingsPropertyMapperTests {
2939

3040
@Test
31-
void mapMapsProperties() {
41+
void mapWhenPropertiesIsNullReturnsBaseSettings() {
42+
HttpClientSettings baseSettings = HttpClientSettings.defaults()
43+
.withConnectTimeout(Duration.ofSeconds(10));
44+
HttpClientSettingsPropertyMapper mapper = new HttpClientSettingsPropertyMapper(null, baseSettings);
45+
HttpClientSettings result = mapper.map(null);
46+
assertThat(result).isEqualTo(baseSettings);
47+
}
48+
49+
@Test
50+
void mapWhenPropertiesIsNullAndBaseSettingsIsNullReturnsDefaults() {
51+
HttpClientSettingsPropertyMapper mapper = new HttpClientSettingsPropertyMapper(null, null);
52+
HttpClientSettings result = mapper.map(null);
53+
assertThat(result).isEqualTo(HttpClientSettings.defaults());
54+
}
55+
56+
@Test
57+
void mapMapsRedirects() {
58+
HttpClientSettingsPropertyMapper mapper = new HttpClientSettingsPropertyMapper(null, null);
59+
TestHttpClientSettingsProperties properties = new TestHttpClientSettingsProperties();
60+
properties.setRedirects(HttpRedirects.DONT_FOLLOW);
61+
HttpClientSettings result = mapper.map(properties);
62+
assertThat(result.redirects()).isEqualTo(HttpRedirects.DONT_FOLLOW);
63+
}
64+
65+
@Test
66+
void mapMapsConnectTimeout() {
67+
HttpClientSettingsPropertyMapper mapper = new HttpClientSettingsPropertyMapper(null, null);
68+
TestHttpClientSettingsProperties properties = new TestHttpClientSettingsProperties();
69+
properties.setConnectTimeout(Duration.ofSeconds(5));
70+
HttpClientSettings result = mapper.map(properties);
71+
assertThat(result.connectTimeout()).isEqualTo(Duration.ofSeconds(5));
72+
}
73+
74+
@Test
75+
void mapMapsReadTimeout() {
76+
HttpClientSettingsPropertyMapper mapper = new HttpClientSettingsPropertyMapper(null, null);
77+
TestHttpClientSettingsProperties properties = new TestHttpClientSettingsProperties();
78+
properties.setReadTimeout(Duration.ofSeconds(30));
79+
HttpClientSettings result = mapper.map(properties);
80+
assertThat(result.readTimeout()).isEqualTo(Duration.ofSeconds(30));
81+
}
82+
83+
@Test
84+
void mapMapsSslBundle() {
85+
SslBundle sslBundle = mock(SslBundle.class);
86+
SslBundles sslBundles = mock(SslBundles.class);
87+
given(sslBundles.getBundle("test-bundle")).willReturn(sslBundle);
88+
HttpClientSettingsPropertyMapper mapper = new HttpClientSettingsPropertyMapper(sslBundles, null);
89+
TestHttpClientSettingsProperties properties = new TestHttpClientSettingsProperties();
90+
properties.getSsl().setBundle("test-bundle");
91+
HttpClientSettings result = mapper.map(properties);
92+
assertThat(result.sslBundle()).isSameAs(sslBundle);
93+
}
94+
95+
@Test
96+
void mapMapsAllProperties() {
97+
SslBundle sslBundle = mock(SslBundle.class);
98+
SslBundles sslBundles = mock(SslBundles.class);
99+
given(sslBundles.getBundle("my-bundle")).willReturn(sslBundle);
100+
HttpClientSettingsPropertyMapper mapper = new HttpClientSettingsPropertyMapper(sslBundles, null);
101+
TestHttpClientSettingsProperties properties = new TestHttpClientSettingsProperties();
102+
properties.setRedirects(HttpRedirects.FOLLOW);
103+
properties.setConnectTimeout(Duration.ofSeconds(10));
104+
properties.setReadTimeout(Duration.ofSeconds(20));
105+
properties.getSsl().setBundle("my-bundle");
106+
HttpClientSettings result = mapper.map(properties);
107+
assertThat(result.redirects()).isEqualTo(HttpRedirects.FOLLOW);
108+
assertThat(result.connectTimeout()).isEqualTo(Duration.ofSeconds(10));
109+
assertThat(result.readTimeout()).isEqualTo(Duration.ofSeconds(20));
110+
assertThat(result.sslBundle()).isSameAs(sslBundle);
111+
}
112+
113+
@Test
114+
void mapUsesBaseSettingsForMissingProperties() {
115+
HttpClientSettings baseSettings = new HttpClientSettings(HttpRedirects.FOLLOW_WHEN_POSSIBLE,
116+
Duration.ofSeconds(15), Duration.ofSeconds(25), null);
117+
HttpClientSettingsPropertyMapper mapper = new HttpClientSettingsPropertyMapper(null, baseSettings);
118+
TestHttpClientSettingsProperties properties = new TestHttpClientSettingsProperties();
119+
properties.setConnectTimeout(Duration.ofSeconds(5));
120+
HttpClientSettings result = mapper.map(properties);
121+
assertThat(result.redirects()).isEqualTo(HttpRedirects.FOLLOW_WHEN_POSSIBLE);
122+
assertThat(result.connectTimeout()).isEqualTo(Duration.ofSeconds(5));
123+
assertThat(result.readTimeout()).isEqualTo(Duration.ofSeconds(25));
124+
}
125+
126+
@Test
127+
void mapWhenSslBundleRequestedButSslBundlesIsNullThrowsException() {
128+
HttpClientSettingsPropertyMapper mapper = new HttpClientSettingsPropertyMapper(null, null);
129+
TestHttpClientSettingsProperties properties = new TestHttpClientSettingsProperties();
130+
properties.getSsl().setBundle("test-bundle");
131+
assertThatIllegalStateException().isThrownBy(() -> mapper.map(properties))
132+
.withMessage("No 'sslBundles' available");
133+
}
134+
135+
static class TestHttpClientSettingsProperties extends HttpClientSettingsProperties {
32136

33137
}
34138

0 commit comments

Comments
 (0)