Skip to content

Commit 8a113ff

Browse files
committed
Upgrade class HttpsClientSslLiveTest
1 parent 5b416e6 commit 8a113ff

1 file changed

Lines changed: 82 additions & 73 deletions

File tree

httpclient/src/test/java/org/baeldung/httpclient/HttpsClientSslLiveTest.java

Lines changed: 82 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.security.GeneralSecurityException;
88
import java.security.cert.X509Certificate;
99

10+
import javax.net.ssl.SSLContext;
1011
import javax.net.ssl.SSLException;
1112

1213
import org.apache.http.HttpResponse;
@@ -17,6 +18,7 @@
1718
import org.apache.http.conn.scheme.SchemeRegistry;
1819
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
1920
import org.apache.http.conn.ssl.SSLContextBuilder;
21+
import org.apache.http.conn.ssl.SSLContexts;
2022
import org.apache.http.conn.ssl.SSLSocketFactory;
2123
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
2224
import org.apache.http.conn.ssl.TrustStrategy;
@@ -33,78 +35,85 @@
3335
* */
3436
public class HttpsClientSslLiveTest {
3537

36-
// "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1" // local
37-
// "https://mms.nw.ru/" // hosted
38-
private static final String HOST_WITH_SSL = "https://mms.nw.ru/";
39-
40-
// tests
41-
42-
@Test(expected = SSLException.class)
43-
public final void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException {
44-
final CloseableHttpClient httpClient = HttpClientBuilder.create().build();
45-
46-
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
47-
final HttpResponse response = httpClient.execute(getMethod);
48-
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
49-
}
50-
51-
@SuppressWarnings("deprecation")
52-
@Test
53-
public final void givenHttpClientPre4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
54-
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
55-
@Override
56-
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
57-
return true;
58-
}
59-
};
60-
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
61-
final SchemeRegistry registry = new SchemeRegistry();
62-
registry.register(new Scheme("https", 443, sf));
63-
final ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);
64-
65-
final CloseableHttpClient httpClient = new DefaultHttpClient(ccm);
66-
67-
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
68-
final HttpResponse response = httpClient.execute(getMethod);
69-
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
70-
71-
httpClient.close();
72-
}
73-
74-
@Test
75-
public final void givenHttpClientAfter4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
76-
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
77-
@Override
78-
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
79-
return true;
80-
}
81-
};
82-
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
83-
final SchemeRegistry registry = new SchemeRegistry();
84-
registry.register(new Scheme("https", 443, sf));
85-
final ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);
86-
87-
final CloseableHttpClient httpClient = new DefaultHttpClient(ccm);
88-
89-
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
90-
final HttpResponse response = httpClient.execute(getMethod);
91-
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
92-
93-
httpClient.close();
94-
}
95-
96-
@Test
97-
public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
98-
final SSLContextBuilder builder = new SSLContextBuilder();
99-
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
100-
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
101-
final CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
102-
103-
// new
104-
105-
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
106-
final HttpResponse response = httpClient.execute(getMethod);
107-
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
108-
}
38+
// "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1" // local
39+
// "https://mms.nw.ru/" // hosted
40+
private static final String HOST_WITH_SSL = "https://mms.nw.ru/";
41+
42+
// tests
43+
44+
@Test(expected = SSLException.class)
45+
public final void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException {
46+
final CloseableHttpClient httpClient = HttpClientBuilder.create().build();
47+
48+
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
49+
final HttpResponse response = httpClient.execute(getMethod);
50+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
51+
}
52+
53+
@SuppressWarnings("deprecation")
54+
@Test
55+
public final void givenHttpClientPre4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
56+
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
57+
@Override
58+
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
59+
return true;
60+
}
61+
};
62+
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
63+
final SchemeRegistry registry = new SchemeRegistry();
64+
registry.register(new Scheme("https", 443, sf));
65+
final ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);
66+
67+
final CloseableHttpClient httpClient = new DefaultHttpClient(ccm);
68+
69+
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
70+
final HttpResponse response = httpClient.execute(getMethod);
71+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
72+
73+
httpClient.close();
74+
}
75+
76+
@Test
77+
public final void givenHttpClientAfter4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
78+
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
79+
@Override
80+
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
81+
return true;
82+
}
83+
};
84+
final SSLContext sslContext = SSLContexts.custom()
85+
.loadTrustMaterial(null, acceptingTrustStrategy).build();
86+
87+
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
88+
sslContext,
89+
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
90+
91+
final CloseableHttpClient httpClient = HttpClients
92+
.custom()
93+
.setHostnameVerifier(
94+
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
95+
.setSSLSocketFactory(sslsf).build();
96+
97+
98+
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
99+
final HttpResponse response = httpClient.execute(getMethod);
100+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
101+
102+
httpClient.close();
103+
}
104+
105+
@Test
106+
public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
107+
final SSLContextBuilder builder = new SSLContextBuilder();
108+
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
109+
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
110+
final CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
111+
112+
// new
113+
114+
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
115+
final HttpResponse response = httpClient.execute(getMethod);
116+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
117+
}
109118

110119
}

0 commit comments

Comments
 (0)