77import java .security .GeneralSecurityException ;
88import java .security .cert .X509Certificate ;
99
10- import javax .net .ssl .SSLPeerUnverifiedException ;
10+ import javax .net .ssl .SSLException ;
1111
1212import org .apache .http .HttpResponse ;
1313import org .apache .http .client .ClientProtocolException ;
2525import org .apache .http .impl .client .HttpClientBuilder ;
2626import org .apache .http .impl .client .HttpClients ;
2727import org .apache .http .impl .conn .PoolingClientConnectionManager ;
28- import org .junit .Ignore ;
2928import org .junit .Test ;
3029
3130/**
3231 * This test requires a localhost server over HTTPS <br>
3332 * It should only be manually run, not part of the automated build
3433 * */
35- public class HttpsClientLiveManualTest {
34+ public class HttpsClientSslLiveTest {
35+
36+ // "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1" // local
37+ // "https://mms.nw.ru/" // hosted
38+ private static final String HOST_WITH_SSL = "https://mms.nw.ru/" ;
3639
3740 // tests
3841
39- @ Test (expected = SSLPeerUnverifiedException .class )
40- @ Ignore ("Only for a server that has HTTPS enabled (on 8443)" )
42+ @ Test (expected = SSLException .class )
4143 public final void whenHttpsUrlIsConsumed_thenException () throws ClientProtocolException , IOException {
4244 final CloseableHttpClient httpClient = HttpClientBuilder .create ().build ();
4345
44- final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1" ;
45- final HttpGet getMethod = new HttpGet (urlOverHttps );
46+ final HttpGet getMethod = new HttpGet (HOST_WITH_SSL );
4647 final HttpResponse response = httpClient .execute (getMethod );
4748 assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
4849 }
@@ -58,13 +59,34 @@ public final boolean isTrusted(final X509Certificate[] certificate, final String
5859 };
5960 final SSLSocketFactory sf = new SSLSocketFactory (acceptingTrustStrategy , SSLSocketFactory .ALLOW_ALL_HOSTNAME_VERIFIER );
6061 final SchemeRegistry registry = new SchemeRegistry ();
61- registry .register (new Scheme ("https" , 8443 , sf ));
62+ registry .register (new Scheme ("https" , 443 , sf ));
63+ final ClientConnectionManager ccm = new PoolingClientConnectionManager (registry );
64+
65+ final CloseableHttpClient httpClient = new DefaultHttpClient (ccm );
66+
67+ final HttpGet getMethod = new HttpGet (HOST_WITH_SSL );
68+ final HttpResponse response = httpClient .execute (getMethod );
69+ assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
70+
71+ httpClient .close ();
72+ }
73+
74+ @ Test
75+ public final void givenHttpClientAfter4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate () throws IOException , GeneralSecurityException {
76+ final TrustStrategy acceptingTrustStrategy = new TrustStrategy () {
77+ @ Override
78+ public final boolean isTrusted (final X509Certificate [] certificate , final String authType ) {
79+ return true ;
80+ }
81+ };
82+ final SSLSocketFactory sf = new SSLSocketFactory (acceptingTrustStrategy , SSLSocketFactory .ALLOW_ALL_HOSTNAME_VERIFIER );
83+ final SchemeRegistry registry = new SchemeRegistry ();
84+ registry .register (new Scheme ("https" , 443 , sf ));
6285 final ClientConnectionManager ccm = new PoolingClientConnectionManager (registry );
6386
6487 final CloseableHttpClient httpClient = new DefaultHttpClient (ccm );
6588
66- final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1" ;
67- final HttpGet getMethod = new HttpGet (urlOverHttps );
89+ final HttpGet getMethod = new HttpGet (HOST_WITH_SSL );
6890 final HttpResponse response = httpClient .execute (getMethod );
6991 assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
7092
@@ -80,8 +102,7 @@ public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanCon
80102
81103 // new
82104
83- final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1" ;
84- final HttpGet getMethod = new HttpGet (urlOverHttps );
105+ final HttpGet getMethod = new HttpGet (HOST_WITH_SSL );
85106 final HttpResponse response = httpClient .execute (getMethod );
86107 assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
87108 }
0 commit comments