From d8e4f042256e17fd4d508edc9bc216de10070d0f Mon Sep 17 00:00:00 2001 From: eugenp Date: Tue, 25 Nov 2014 13:51:42 +0200 Subject: [PATCH 1/2] cleanup work --- ...lTest.java => HttpsClientSslLiveTest.java} | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) rename httpclient/src/test/java/org/baeldung/httpclient/{HttpsClientLiveManualTest.java => HttpsClientSslLiveTest.java} (83%) diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpsClientLiveManualTest.java b/httpclient/src/test/java/org/baeldung/httpclient/HttpsClientSslLiveTest.java similarity index 83% rename from httpclient/src/test/java/org/baeldung/httpclient/HttpsClientLiveManualTest.java rename to httpclient/src/test/java/org/baeldung/httpclient/HttpsClientSslLiveTest.java index df3ac3f25b..652a01aedf 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/HttpsClientLiveManualTest.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/HttpsClientSslLiveTest.java @@ -25,24 +25,25 @@ import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingClientConnectionManager; -import org.junit.Ignore; import org.junit.Test; /** * This test requires a localhost server over HTTPS
* It should only be manually run, not part of the automated build * */ -public class HttpsClientLiveManualTest { +public class HttpsClientSslLiveTest { + + // "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1" // local + // "https://mms.nw.ru/" // hosted + private static final String HOST_WITH_SSL = "https://mms.nw.ru/"; // tests @Test(expected = SSLPeerUnverifiedException.class) - @Ignore("Only for a server that has HTTPS enabled (on 8443)") public final void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException { final CloseableHttpClient httpClient = HttpClientBuilder.create().build(); - final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1"; - final HttpGet getMethod = new HttpGet(urlOverHttps); + final HttpGet getMethod = new HttpGet(HOST_WITH_SSL); final HttpResponse response = httpClient.execute(getMethod); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); } @@ -58,13 +59,12 @@ public class HttpsClientLiveManualTest { }; final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); final SchemeRegistry registry = new SchemeRegistry(); - registry.register(new Scheme("https", 8443, sf)); + registry.register(new Scheme("https", 443, sf)); final ClientConnectionManager ccm = new PoolingClientConnectionManager(registry); final CloseableHttpClient httpClient = new DefaultHttpClient(ccm); - final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1"; - final HttpGet getMethod = new HttpGet(urlOverHttps); + final HttpGet getMethod = new HttpGet(HOST_WITH_SSL); final HttpResponse response = httpClient.execute(getMethod); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); @@ -80,8 +80,7 @@ public class HttpsClientLiveManualTest { // new - final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1"; - final HttpGet getMethod = new HttpGet(urlOverHttps); + final HttpGet getMethod = new HttpGet(HOST_WITH_SSL); final HttpResponse response = httpClient.execute(getMethod); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); } From 72279b8d80d0adcfd70a55056f27c157139f79d3 Mon Sep 17 00:00:00 2001 From: eugenp Date: Tue, 25 Nov 2014 14:02:43 +0200 Subject: [PATCH 2/2] cleanup of httpclient tests --- httpclient/pom.xml | 2 +- .../httpclient/HttpsClientSslLiveTest.java | 26 +++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/httpclient/pom.xml b/httpclient/pom.xml index 106ed3de1e..020106c497 100644 --- a/httpclient/pom.xml +++ b/httpclient/pom.xml @@ -173,7 +173,7 @@ 1.10.8 4.3.3 - 4.3.4 + 4.3.6 2.4.0 diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpsClientSslLiveTest.java b/httpclient/src/test/java/org/baeldung/httpclient/HttpsClientSslLiveTest.java index 652a01aedf..aa64bafb23 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/HttpsClientSslLiveTest.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/HttpsClientSslLiveTest.java @@ -7,7 +7,7 @@ import java.io.IOException; import java.security.GeneralSecurityException; import java.security.cert.X509Certificate; -import javax.net.ssl.SSLPeerUnverifiedException; +import javax.net.ssl.SSLException; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; @@ -39,7 +39,7 @@ public class HttpsClientSslLiveTest { // tests - @Test(expected = SSLPeerUnverifiedException.class) + @Test(expected = SSLException.class) public final void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException { final CloseableHttpClient httpClient = HttpClientBuilder.create().build(); @@ -71,6 +71,28 @@ public class HttpsClientSslLiveTest { httpClient.close(); } + @Test + public final void givenHttpClientAfter4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException { + final TrustStrategy acceptingTrustStrategy = new TrustStrategy() { + @Override + public final boolean isTrusted(final X509Certificate[] certificate, final String authType) { + return true; + } + }; + final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); + final SchemeRegistry registry = new SchemeRegistry(); + registry.register(new Scheme("https", 443, sf)); + final ClientConnectionManager ccm = new PoolingClientConnectionManager(registry); + + final CloseableHttpClient httpClient = new DefaultHttpClient(ccm); + + final HttpGet getMethod = new HttpGet(HOST_WITH_SSL); + final HttpResponse response = httpClient.execute(getMethod); + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); + + httpClient.close(); + } + @Test public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException { final SSLContextBuilder builder = new SSLContextBuilder();