|
1 | 1 | package org.baeldung.httpclient; |
2 | 2 |
|
3 | | -import static org.hamcrest.CoreMatchers.equalTo; |
4 | | -import static org.junit.Assert.assertThat; |
5 | | - |
6 | | -import java.io.IOException; |
7 | | -import java.security.GeneralSecurityException; |
8 | | -import java.security.KeyManagementException; |
9 | | -import java.security.KeyStoreException; |
10 | | -import java.security.NoSuchAlgorithmException; |
11 | | - |
12 | | -import javax.net.ssl.SSLContext; |
13 | | -import javax.net.ssl.SSLException; |
14 | | - |
15 | 3 | import org.apache.http.HttpResponse; |
16 | 4 | import org.apache.http.client.methods.HttpGet; |
17 | 5 | import org.apache.http.conn.ClientConnectionManager; |
18 | 6 | import org.apache.http.conn.scheme.Scheme; |
19 | 7 | import org.apache.http.conn.scheme.SchemeRegistry; |
20 | | -import org.apache.http.conn.ssl.NoopHostnameVerifier; |
21 | | -import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
22 | | -import org.apache.http.conn.ssl.SSLSocketFactory; |
23 | | -import org.apache.http.conn.ssl.TrustSelfSignedStrategy; |
24 | | -import org.apache.http.conn.ssl.TrustStrategy; |
| 8 | +import org.apache.http.conn.ssl.*; |
25 | 9 | import org.apache.http.impl.client.CloseableHttpClient; |
26 | 10 | import org.apache.http.impl.client.DefaultHttpClient; |
27 | 11 | import org.apache.http.impl.client.HttpClientBuilder; |
|
31 | 15 | import org.apache.http.ssl.SSLContexts; |
32 | 16 | import org.junit.Test; |
33 | 17 |
|
| 18 | +import javax.net.ssl.SSLContext; |
| 19 | +import javax.net.ssl.SSLException; |
| 20 | +import java.io.IOException; |
| 21 | +import java.security.GeneralSecurityException; |
| 22 | + |
| 23 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 24 | +import static org.junit.Assert.assertThat; |
| 25 | + |
34 | 26 | /** |
35 | 27 | * This test requires a localhost server over HTTPS <br> |
36 | 28 | * It should only be manually run, not part of the automated build |
@@ -101,17 +93,9 @@ public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanCon |
101 | 93 | } |
102 | 94 |
|
103 | 95 | @Test |
104 | | - public final void givenIgnoringCertificates_whenHttpsUrlIsConsumed_thenCorrect() throws IOException { |
105 | | - |
106 | | - final TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true; |
107 | | - |
108 | | - SSLContext sslContext = null; |
109 | | - try { |
110 | | - sslContext = new SSLContextBuilder().loadTrustMaterial(null, acceptingTrustStrategy).build(); |
111 | | - |
112 | | - } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) { |
113 | | - e.printStackTrace(); |
114 | | - } |
| 96 | + public final void givenIgnoringCertificates_whenHttpsUrlIsConsumed_thenCorrect() throws Exception { |
| 97 | + SSLContext sslContext = new SSLContextBuilder() |
| 98 | + .loadTrustMaterial(null, (certificate, authType) -> true).build(); |
115 | 99 |
|
116 | 100 | final CloseableHttpClient client = HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()).build(); |
117 | 101 | final HttpGet httpGet = new HttpGet(HOST_WITH_SSL); |
|
0 commit comments