|
16 | 16 |
|
17 | 17 | package org.springframework.boot.web.client;
|
18 | 18 |
|
| 19 | +import java.net.URI; |
| 20 | +import java.nio.charset.StandardCharsets; |
19 | 21 | import java.time.Duration;
|
20 | 22 |
|
| 23 | +import javax.net.ssl.SSLHandshakeException; |
| 24 | + |
21 | 25 | import org.junit.jupiter.api.Test;
|
22 | 26 |
|
| 27 | +import org.springframework.boot.ssl.SslBundle; |
| 28 | +import org.springframework.boot.ssl.SslBundleKey; |
| 29 | +import org.springframework.boot.ssl.jks.JksSslStoreBundle; |
| 30 | +import org.springframework.boot.ssl.jks.JksSslStoreDetails; |
| 31 | +import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories; |
| 32 | +import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; |
| 33 | +import org.springframework.boot.web.server.Ssl; |
| 34 | +import org.springframework.boot.web.server.Ssl.ClientAuth; |
| 35 | +import org.springframework.boot.web.server.WebServer; |
| 36 | +import org.springframework.http.HttpMethod; |
| 37 | +import org.springframework.http.client.ClientHttpRequest; |
23 | 38 | import org.springframework.http.client.ClientHttpRequestFactory;
|
| 39 | +import org.springframework.util.StreamUtils; |
24 | 40 |
|
25 | 41 | import static org.assertj.core.api.Assertions.assertThat;
|
| 42 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
26 | 43 |
|
27 | 44 | /**
|
28 | 45 | * Base classes for testing of {@link ClientHttpRequestFactories} with different HTTP
|
|
31 | 48 | * @param <T> the {@link ClientHttpRequestFactory} to be produced
|
32 | 49 | * @author Andy Wilkinson
|
33 | 50 | */
|
| 51 | +@DirtiesUrlFactories |
34 | 52 | abstract class AbstractClientHttpRequestFactoriesTests<T extends ClientHttpRequestFactory> {
|
35 | 53 |
|
36 | 54 | private final Class<T> requestFactoryType;
|
@@ -76,6 +94,39 @@ void getReturnsRequestFactoryWithConfiguredReadTimeout() {
|
76 | 94 | assertThat(readTimeout((T) requestFactory)).isEqualTo(Duration.ofSeconds(120).toMillis());
|
77 | 95 | }
|
78 | 96 |
|
| 97 | + @Test |
| 98 | + void connectWithSslBundle() throws Exception { |
| 99 | + TomcatServletWebServerFactory webServerFactory = new TomcatServletWebServerFactory(0); |
| 100 | + Ssl ssl = new Ssl(); |
| 101 | + ssl.setClientAuth(ClientAuth.NEED); |
| 102 | + ssl.setKeyPassword("password"); |
| 103 | + ssl.setKeyStore("classpath:test.jks"); |
| 104 | + ssl.setTrustStore("classpath:test.jks"); |
| 105 | + webServerFactory.setSsl(ssl); |
| 106 | + WebServer webServer = webServerFactory.getWebServer(); |
| 107 | + try { |
| 108 | + webServer.start(); |
| 109 | + int port = webServer.getPort(); |
| 110 | + URI uri = new URI("https://localhost:%s".formatted(port)); |
| 111 | + ClientHttpRequestFactory insecureRequestFactory = ClientHttpRequestFactories |
| 112 | + .get(ClientHttpRequestFactorySettings.DEFAULTS); |
| 113 | + ClientHttpRequest insecureRequest = insecureRequestFactory.createRequest(uri, HttpMethod.GET); |
| 114 | + assertThatExceptionOfType(SSLHandshakeException.class) |
| 115 | + .isThrownBy(() -> insecureRequest.execute().getBody()); |
| 116 | + JksSslStoreDetails storeDetails = JksSslStoreDetails.forLocation("classpath:test.jks"); |
| 117 | + JksSslStoreBundle stores = new JksSslStoreBundle(storeDetails, storeDetails); |
| 118 | + SslBundle sslBundle = SslBundle.of(stores, SslBundleKey.of("password")); |
| 119 | + ClientHttpRequestFactory secureRequestFactory = ClientHttpRequestFactories |
| 120 | + .get(ClientHttpRequestFactorySettings.DEFAULTS.withSslBundle(sslBundle)); |
| 121 | + ClientHttpRequest secureRequest = secureRequestFactory.createRequest(uri, HttpMethod.GET); |
| 122 | + String secureResponse = StreamUtils.copyToString(secureRequest.execute().getBody(), StandardCharsets.UTF_8); |
| 123 | + assertThat(secureResponse).contains("HTTP Status 404 – Not Found"); |
| 124 | + } |
| 125 | + finally { |
| 126 | + webServer.stop(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
79 | 130 | protected abstract long connectTimeout(T requestFactory);
|
80 | 131 |
|
81 | 132 | protected abstract long readTimeout(T requestFactory);
|
|
0 commit comments