Skip to content

Commit 84d3367

Browse files
author
eugenp
committed
small fixes and formatting work
1 parent 1c79503 commit 84d3367

4 files changed

Lines changed: 292 additions & 302 deletions

File tree

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.apache.http.client.config.RequestConfig;
1212
import org.apache.http.client.methods.CloseableHttpResponse;
1313
import org.apache.http.client.methods.HttpGet;
14+
import org.apache.http.conn.ConnectTimeoutException;
1415
import org.apache.http.impl.client.CloseableHttpClient;
1516
import org.apache.http.impl.client.DefaultHttpClient;
1617
import org.apache.http.impl.client.HttpClientBuilder;
@@ -93,17 +94,15 @@ public final void givenUsingNewApi_whenSettingTimeoutViaHighLevelApi_thenCorrect
9394
/**
9495
* This simulates a timeout against a domain with multiple routes/IPs to it (not a single raw IP)
9596
*/
96-
@Test
97-
public final void givenTimeoutIsConfigured_whenTimingOut_thenCorrect() throws ClientProtocolException, IOException {
97+
@Test(expected = ConnectTimeoutException.class)
98+
public final void givenTimeoutIsConfigured_whenTimingOut_thenTimeoutException() throws ClientProtocolException, IOException {
9899
final int timeout = 3;
99100

100101
final RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build();
101102
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
102103

103104
final HttpGet request = new HttpGet("http://www.google.com:81");
104-
response = client.execute(request);
105-
106-
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
105+
client.execute(request);
107106
}
108107

109108
}

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

Lines changed: 72 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -35,85 +35,77 @@
3535
* */
3636
public class HttpsClientSslLiveTest {
3737

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-
}
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().loadTrustMaterial(null, acceptingTrustStrategy).build();
85+
86+
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
87+
88+
final CloseableHttpClient httpClient = HttpClients.custom().setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER).setSSLSocketFactory(sslsf).build();
89+
90+
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
91+
final HttpResponse response = httpClient.execute(getMethod);
92+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
93+
94+
httpClient.close();
95+
}
96+
97+
@Test
98+
public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
99+
final SSLContextBuilder builder = new SSLContextBuilder();
100+
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
101+
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
102+
final CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
103+
104+
// new
105+
106+
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
107+
final HttpResponse response = httpClient.execute(getMethod);
108+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
109+
}
118110

119111
}

0 commit comments

Comments
 (0)