11package org .baeldung .httpclient ;
22
3- import static org .junit .Assert .assertThat ;
4-
5- import java .io .IOException ;
6- import java .security .GeneralSecurityException ;
7- import java .security .KeyManagementException ;
8- import java .security .KeyStoreException ;
9- import java .security .NoSuchAlgorithmException ;
10- import java .security .cert .X509Certificate ;
11-
12- import javax .net .ssl .SSLContext ;
13- import javax .net .ssl .SSLException ;
14-
153import org .apache .http .HttpResponse ;
16- import org .apache .http .client .ClientProtocolException ;
174import org .apache .http .client .methods .HttpGet ;
185import org .apache .http .conn .ClientConnectionManager ;
196import org .apache .http .conn .scheme .Scheme ;
207import org .apache .http .conn .scheme .SchemeRegistry ;
21- import org .apache .http .conn .ssl .NoopHostnameVerifier ;
22- import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
23- import org .apache .http .conn .ssl .SSLContextBuilder ;
24- import org .apache .http .conn .ssl .SSLContexts ;
25- import org .apache .http .conn .ssl .SSLSocketFactory ;
26- import org .apache .http .conn .ssl .TrustSelfSignedStrategy ;
27- import org .apache .http .conn .ssl .TrustStrategy ;
8+ import org .apache .http .conn .ssl .*;
289import org .apache .http .impl .client .CloseableHttpClient ;
2910import org .apache .http .impl .client .DefaultHttpClient ;
3011import org .apache .http .impl .client .HttpClientBuilder ;
3112import org .apache .http .impl .client .HttpClients ;
3213import org .apache .http .impl .conn .PoolingClientConnectionManager ;
3314import org .junit .Test ;
3415
16+ import javax .net .ssl .SSLContext ;
17+ import javax .net .ssl .SSLException ;
18+ import java .io .IOException ;
19+ import java .security .GeneralSecurityException ;
20+ import java .security .KeyManagementException ;
21+ import java .security .KeyStoreException ;
22+ import java .security .NoSuchAlgorithmException ;
23+
24+ import static org .hamcrest .CoreMatchers .equalTo ;
25+ import static org .junit .Assert .assertThat ;
26+
3527/**
3628 * This test requires a localhost server over HTTPS <br>
3729 * It should only be manually run, not part of the automated build
@@ -45,7 +37,7 @@ public class HttpsClientSslLiveTest {
4537 // tests
4638
4739 @ Test (expected = SSLException .class )
48- public final void whenHttpsUrlIsConsumed_thenException () throws ClientProtocolException , IOException {
40+ public final void whenHttpsUrlIsConsumed_thenException () throws IOException {
4941 final CloseableHttpClient httpClient = HttpClientBuilder .create ().build ();
5042
5143 final HttpGet getMethod = new HttpGet (HOST_WITH_SSL );
@@ -56,12 +48,7 @@ public final void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolEx
5648 @ SuppressWarnings ("deprecation" )
5749 @ Test
5850 public final void givenHttpClientPre4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate () throws IOException , GeneralSecurityException {
59- final TrustStrategy acceptingTrustStrategy = new TrustStrategy () {
60- @ Override
61- public final boolean isTrusted (final X509Certificate [] certificate , final String authType ) {
62- return true ;
63- }
64- };
51+ final TrustStrategy acceptingTrustStrategy = (certificate , authType ) -> true ;
6552 final SSLSocketFactory sf = new SSLSocketFactory (acceptingTrustStrategy , SSLSocketFactory .ALLOW_ALL_HOSTNAME_VERIFIER );
6653 final SchemeRegistry registry = new SchemeRegistry ();
6754 registry .register (new Scheme ("https" , 443 , sf ));
@@ -78,12 +65,7 @@ public final boolean isTrusted(final X509Certificate[] certificate, final String
7865
7966 @ Test
8067 public final void givenHttpClientAfter4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate () throws IOException , GeneralSecurityException {
81- final TrustStrategy acceptingTrustStrategy = new TrustStrategy () {
82- @ Override
83- public final boolean isTrusted (final X509Certificate [] certificate , final String authType ) {
84- return true ;
85- }
86- };
68+ final TrustStrategy acceptingTrustStrategy = (certificate , authType ) -> true ;
8769 final SSLContext sslContext = SSLContexts .custom ().loadTrustMaterial (null , acceptingTrustStrategy ).build ();
8870
8971 final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory (sslContext , SSLConnectionSocketFactory .ALLOW_ALL_HOSTNAME_VERIFIER );
@@ -112,14 +94,9 @@ public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanCon
11294 }
11395
11496 @ Test
115- public final void givenIgnoringCertificates_whenHttpsUrlIsConsumed_thenCorrect () throws ClientProtocolException , IOException {
116-
117- TrustStrategy acceptingTrustStrategy = new TrustStrategy () {
118- @ Override
119- public boolean isTrusted (X509Certificate [] certificate , String authType ) {
120- return true ;
121- }
122- };
97+ public final void givenIgnoringCertificates_whenHttpsUrlIsConsumed_thenCorrect () throws IOException {
98+
99+ TrustStrategy acceptingTrustStrategy = (certificate , authType ) -> true ;
123100
124101 SSLContext sslContext = null ;
125102 try {
0 commit comments