11package org .baeldung .client ;
22
3+ import static org .apache .http .conn .ssl .SSLSocketFactory .ALLOW_ALL_HOSTNAME_VERIFIER ;
34import static org .hamcrest .Matchers .equalTo ;
45import static org .junit .Assert .assertThat ;
56
6- import java .io .IOException ;
77import java .security .GeneralSecurityException ;
88import java .security .cert .X509Certificate ;
99
@@ -54,9 +54,8 @@ public final void whenHttpsUrlIsConsumed_thenException() {
5454 }
5555
5656 @ Test
57- public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException () throws IOException , GeneralSecurityException {
58- final RestTemplate newRestTemplate = new RestTemplate (new HttpComponentsClientHttpRequestFactory ());
59- final HttpComponentsClientHttpRequestFactory requestFactory = (HttpComponentsClientHttpRequestFactory ) newRestTemplate .getRequestFactory ();
57+ public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException () throws GeneralSecurityException {
58+ final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory ();
6059 final DefaultHttpClient httpClient = (DefaultHttpClient ) requestFactory .getHttpClient ();
6160
6261 final TrustStrategy acceptingTrustStrategy = new TrustStrategy () {
@@ -65,11 +64,11 @@ public final boolean isTrusted(final X509Certificate[] certificate, final String
6564 return true ;
6665 }
6766 };
68- final SSLSocketFactory sf = new SSLSocketFactory (acceptingTrustStrategy , SSLSocketFactory . ALLOW_ALL_HOSTNAME_VERIFIER );
67+ final SSLSocketFactory sf = new SSLSocketFactory (acceptingTrustStrategy , ALLOW_ALL_HOSTNAME_VERIFIER );
6968 httpClient .getConnectionManager ().getSchemeRegistry ().register (new Scheme ("https" , 8443 , sf ));
7069
7170 final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1" ;
72- final ResponseEntity <String > response = newRestTemplate .exchange (urlOverHttps , HttpMethod .GET , null , String .class );
71+ final ResponseEntity <String > response = new RestTemplate ( requestFactory ) .exchange (urlOverHttps , HttpMethod .GET , null , String .class );
7372 assertThat (response .getStatusCode ().value (), equalTo (200 ));
7473 }
7574
0 commit comments