Skip to content

Commit

Permalink
final cleanup for tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenp committed Aug 9, 2013
1 parent 2119981 commit 1f5cf71
Showing 1 changed file with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.baeldung.client;

import static org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.cert.X509Certificate;

Expand Down Expand Up @@ -54,9 +54,8 @@ public final void whenHttpsUrlIsConsumed_thenException() {
}

@Test
public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException() throws IOException, GeneralSecurityException {
final RestTemplate newRestTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
final HttpComponentsClientHttpRequestFactory requestFactory = (HttpComponentsClientHttpRequestFactory) newRestTemplate.getRequestFactory();
public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException() throws GeneralSecurityException {
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
final DefaultHttpClient httpClient = (DefaultHttpClient) requestFactory.getHttpClient();

final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
Expand All @@ -65,11 +64,11 @@ public final boolean isTrusted(final X509Certificate[] certificate, final String
return true;
}
};
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, ALLOW_ALL_HOSTNAME_VERIFIER);
httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 8443, sf));

final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1";
final ResponseEntity<String> response = newRestTemplate.exchange(urlOverHttps, HttpMethod.GET, null, String.class);
final ResponseEntity<String> response = new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class);
assertThat(response.getStatusCode().value(), equalTo(200));
}

Expand Down

0 comments on commit 1f5cf71

Please sign in to comment.