Skip to content

Commit 584ae41

Browse files
author
Grzegorz Piwowarek
committed
Refactor HttpsClientSSLLiveTest
1 parent c1411e9 commit 584ae41

1 file changed

Lines changed: 12 additions & 28 deletions

File tree

httpclient/src/test/java/org/baeldung/httpclient/HttpsClientSslLiveTest.java

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
11
package org.baeldung.httpclient;
22

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-
153
import org.apache.http.HttpResponse;
164
import org.apache.http.client.methods.HttpGet;
175
import org.apache.http.conn.ClientConnectionManager;
186
import org.apache.http.conn.scheme.Scheme;
197
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.*;
259
import org.apache.http.impl.client.CloseableHttpClient;
2610
import org.apache.http.impl.client.DefaultHttpClient;
2711
import org.apache.http.impl.client.HttpClientBuilder;
@@ -31,6 +15,14 @@
3115
import org.apache.http.ssl.SSLContexts;
3216
import org.junit.Test;
3317

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+
3426
/**
3527
* This test requires a localhost server over HTTPS <br>
3628
* It should only be manually run, not part of the automated build
@@ -101,17 +93,9 @@ public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanCon
10193
}
10294

10395
@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();
11599

116100
final CloseableHttpClient client = HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
117101
final HttpGet httpGet = new HttpGet(HOST_WITH_SSL);

0 commit comments

Comments
 (0)