Upgrade class HttpsClientSslLiveTest
This commit is contained in:
parent
5b416e68f7
commit
8a113ffec4
|
@ -7,6 +7,7 @@ import java.io.IOException;
|
||||||
import java.security.GeneralSecurityException;
|
import java.security.GeneralSecurityException;
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
|
|
||||||
|
import javax.net.ssl.SSLContext;
|
||||||
import javax.net.ssl.SSLException;
|
import javax.net.ssl.SSLException;
|
||||||
|
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
|
@ -17,6 +18,7 @@ import org.apache.http.conn.scheme.Scheme;
|
||||||
import org.apache.http.conn.scheme.SchemeRegistry;
|
import org.apache.http.conn.scheme.SchemeRegistry;
|
||||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||||
import org.apache.http.conn.ssl.SSLContextBuilder;
|
import org.apache.http.conn.ssl.SSLContextBuilder;
|
||||||
|
import org.apache.http.conn.ssl.SSLContexts;
|
||||||
import org.apache.http.conn.ssl.SSLSocketFactory;
|
import org.apache.http.conn.ssl.SSLSocketFactory;
|
||||||
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
|
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
|
||||||
import org.apache.http.conn.ssl.TrustStrategy;
|
import org.apache.http.conn.ssl.TrustStrategy;
|
||||||
|
@ -33,78 +35,85 @@ import org.junit.Test;
|
||||||
* */
|
* */
|
||||||
public class HttpsClientSslLiveTest {
|
public class HttpsClientSslLiveTest {
|
||||||
|
|
||||||
// "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1" // local
|
// "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1" // local
|
||||||
// "https://mms.nw.ru/" // hosted
|
// "https://mms.nw.ru/" // hosted
|
||||||
private static final String HOST_WITH_SSL = "https://mms.nw.ru/";
|
private static final String HOST_WITH_SSL = "https://mms.nw.ru/";
|
||||||
|
|
||||||
// tests
|
// tests
|
||||||
|
|
||||||
@Test(expected = SSLException.class)
|
@Test(expected = SSLException.class)
|
||||||
public final void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException {
|
public final void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException {
|
||||||
final CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
final CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
||||||
|
|
||||||
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
|
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
|
||||||
final HttpResponse response = httpClient.execute(getMethod);
|
final HttpResponse response = httpClient.execute(getMethod);
|
||||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
@Test
|
@Test
|
||||||
public final void givenHttpClientPre4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
|
public final void givenHttpClientPre4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
|
||||||
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
|
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
|
||||||
@Override
|
@Override
|
||||||
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
|
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
||||||
final SchemeRegistry registry = new SchemeRegistry();
|
final SchemeRegistry registry = new SchemeRegistry();
|
||||||
registry.register(new Scheme("https", 443, sf));
|
registry.register(new Scheme("https", 443, sf));
|
||||||
final ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);
|
final ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);
|
||||||
|
|
||||||
final CloseableHttpClient httpClient = new DefaultHttpClient(ccm);
|
final CloseableHttpClient httpClient = new DefaultHttpClient(ccm);
|
||||||
|
|
||||||
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
|
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
|
||||||
final HttpResponse response = httpClient.execute(getMethod);
|
final HttpResponse response = httpClient.execute(getMethod);
|
||||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||||
|
|
||||||
httpClient.close();
|
httpClient.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenHttpClientAfter4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
|
public final void givenHttpClientAfter4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
|
||||||
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
|
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
|
||||||
@Override
|
@Override
|
||||||
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
|
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
final SSLContext sslContext = SSLContexts.custom()
|
||||||
final SchemeRegistry registry = new SchemeRegistry();
|
.loadTrustMaterial(null, acceptingTrustStrategy).build();
|
||||||
registry.register(new Scheme("https", 443, sf));
|
|
||||||
final ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);
|
|
||||||
|
|
||||||
final CloseableHttpClient httpClient = new DefaultHttpClient(ccm);
|
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
|
||||||
|
sslContext,
|
||||||
|
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
||||||
|
|
||||||
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
|
final CloseableHttpClient httpClient = HttpClients
|
||||||
final HttpResponse response = httpClient.execute(getMethod);
|
.custom()
|
||||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
.setHostnameVerifier(
|
||||||
|
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
|
||||||
|
.setSSLSocketFactory(sslsf).build();
|
||||||
|
|
||||||
httpClient.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
|
||||||
public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
|
final HttpResponse response = httpClient.execute(getMethod);
|
||||||
final SSLContextBuilder builder = new SSLContextBuilder();
|
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||||
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
|
|
||||||
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
|
|
||||||
final CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
|
|
||||||
|
|
||||||
// new
|
httpClient.close();
|
||||||
|
}
|
||||||
|
|
||||||
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
|
@Test
|
||||||
final HttpResponse response = httpClient.execute(getMethod);
|
public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
|
||||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
final SSLContextBuilder builder = new SSLContextBuilder();
|
||||||
}
|
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
|
||||||
|
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
|
||||||
|
final CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
|
||||||
|
|
||||||
|
// new
|
||||||
|
|
||||||
|
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
|
||||||
|
final HttpResponse response = httpClient.execute(getMethod);
|
||||||
|
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue