44import static org .hamcrest .Matchers .equalTo ;
55import static org .junit .Assert .assertThat ;
66
7+ import java .io .IOException ;
78import java .security .GeneralSecurityException ;
89import java .security .cert .X509Certificate ;
910
11+ import org .apache .http .HttpResponse ;
12+ import org .apache .http .client .ClientProtocolException ;
13+ import org .apache .http .client .methods .HttpGet ;
1014import org .apache .http .conn .scheme .Scheme ;
15+ import org .apache .http .conn .ssl .NoopHostnameVerifier ;
1116import org .apache .http .conn .ssl .SSLSocketFactory ;
1217import org .apache .http .conn .ssl .TrustStrategy ;
1318import org .apache .http .impl .client .CloseableHttpClient ;
19+ import org .apache .http .impl .client .HttpClients ;
1420import org .junit .Test ;
1521import org .springframework .http .HttpMethod ;
1622import org .springframework .http .ResponseEntity ;
2329 * */
2430public class RestClientLiveManualTest {
2531
32+ final String urlOverHttps = "http://localhost:8080/spring-security-rest-basic-auth/api/bars/1" ;
33+
34+
2635 // tests
2736
2837 @ Test
@@ -39,9 +48,28 @@ public final boolean isTrusted(final X509Certificate[] certificate, final String
3948 final SSLSocketFactory sf = new SSLSocketFactory (acceptingTrustStrategy , ALLOW_ALL_HOSTNAME_VERIFIER );
4049 httpClient .getConnectionManager ().getSchemeRegistry ().register (new Scheme ("https" , 8443 , sf ));
4150
42- final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1" ;
51+ final ResponseEntity <String > response = new RestTemplate (requestFactory ).exchange (urlOverHttps , HttpMethod .GET , null , String .class );
52+ assertThat (response .getStatusCode ().value (), equalTo (200 ));
53+ }
54+
55+ @ Test
56+ public final void givenAcceptingAllCertificatesUsing4_4_whenHttpsUrlIsConsumed_thenCorrect () throws ClientProtocolException , IOException {
57+ final CloseableHttpClient httpClient = HttpClients .custom ().setSSLHostnameVerifier (new NoopHostnameVerifier ()).build ();
58+
59+ final HttpGet getMethod = new HttpGet (urlOverHttps );
60+ final HttpResponse response = httpClient .execute (getMethod );
61+ assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
62+ }
63+
64+ @ Test
65+ public final void givenAcceptingAllCertificatesUsing4_4_whenHttpsUrlIsConsumedUsingRestTemplate_thenCorrect () throws ClientProtocolException , IOException {
66+ final CloseableHttpClient httpClient = HttpClients .custom ().setSSLHostnameVerifier (new NoopHostnameVerifier ()).build ();
67+ final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory ();
68+ requestFactory .setHttpClient (httpClient );
69+
4370 final ResponseEntity <String > response = new RestTemplate (requestFactory ).exchange (urlOverHttps , HttpMethod .GET , null , String .class );
4471 assertThat (response .getStatusCode ().value (), equalTo (200 ));
4572 }
4673
4774}
75+
0 commit comments