Skip to content

Commit d0a376e

Browse files
author
Grzegorz Piwowarek
committed
Refactor HttpClient test
1 parent b018c6c commit d0a376e

File tree

3 files changed

+34
-61
lines changed

3 files changed

+34
-61
lines changed

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

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,29 @@
11
package org.baeldung.httpclient;
22

3-
import static org.junit.Assert.assertThat;
4-
5-
import java.io.IOException;
6-
import java.security.GeneralSecurityException;
7-
import java.security.KeyManagementException;
8-
import java.security.KeyStoreException;
9-
import java.security.NoSuchAlgorithmException;
10-
import java.security.cert.X509Certificate;
11-
12-
import javax.net.ssl.SSLContext;
13-
import javax.net.ssl.SSLException;
14-
153
import org.apache.http.HttpResponse;
16-
import org.apache.http.client.ClientProtocolException;
174
import org.apache.http.client.methods.HttpGet;
185
import org.apache.http.conn.ClientConnectionManager;
196
import org.apache.http.conn.scheme.Scheme;
207
import org.apache.http.conn.scheme.SchemeRegistry;
21-
import org.apache.http.conn.ssl.NoopHostnameVerifier;
22-
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
23-
import org.apache.http.conn.ssl.SSLContextBuilder;
24-
import org.apache.http.conn.ssl.SSLContexts;
25-
import org.apache.http.conn.ssl.SSLSocketFactory;
26-
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
27-
import org.apache.http.conn.ssl.TrustStrategy;
8+
import org.apache.http.conn.ssl.*;
289
import org.apache.http.impl.client.CloseableHttpClient;
2910
import org.apache.http.impl.client.DefaultHttpClient;
3011
import org.apache.http.impl.client.HttpClientBuilder;
3112
import org.apache.http.impl.client.HttpClients;
3213
import org.apache.http.impl.conn.PoolingClientConnectionManager;
3314
import org.junit.Test;
3415

16+
import javax.net.ssl.SSLContext;
17+
import javax.net.ssl.SSLException;
18+
import java.io.IOException;
19+
import java.security.GeneralSecurityException;
20+
import java.security.KeyManagementException;
21+
import java.security.KeyStoreException;
22+
import java.security.NoSuchAlgorithmException;
23+
24+
import static org.hamcrest.CoreMatchers.equalTo;
25+
import static org.junit.Assert.assertThat;
26+
3527
/**
3628
* This test requires a localhost server over HTTPS <br>
3729
* It should only be manually run, not part of the automated build
@@ -45,7 +37,7 @@ public class HttpsClientSslLiveTest {
4537
// tests
4638

4739
@Test(expected = SSLException.class)
48-
public final void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException {
40+
public final void whenHttpsUrlIsConsumed_thenException() throws IOException {
4941
final CloseableHttpClient httpClient = HttpClientBuilder.create().build();
5042

5143
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
@@ -56,12 +48,7 @@ public final void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolEx
5648
@SuppressWarnings("deprecation")
5749
@Test
5850
public final void givenHttpClientPre4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
59-
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
60-
@Override
61-
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
62-
return true;
63-
}
64-
};
51+
final TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true;
6552
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
6653
final SchemeRegistry registry = new SchemeRegistry();
6754
registry.register(new Scheme("https", 443, sf));
@@ -78,12 +65,7 @@ public final boolean isTrusted(final X509Certificate[] certificate, final String
7865

7966
@Test
8067
public final void givenHttpClientAfter4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
81-
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
82-
@Override
83-
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
84-
return true;
85-
}
86-
};
68+
final TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true;
8769
final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
8870

8971
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
@@ -112,14 +94,9 @@ public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanCon
11294
}
11395

11496
@Test
115-
public final void givenIgnoringCertificates_whenHttpsUrlIsConsumed_thenCorrect() throws ClientProtocolException, IOException {
116-
117-
TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
118-
@Override
119-
public boolean isTrusted(X509Certificate[] certificate, String authType) {
120-
return true;
121-
}
122-
};
97+
public final void givenIgnoringCertificates_whenHttpsUrlIsConsumed_thenCorrect() throws IOException {
98+
99+
TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true;
123100

124101
SSLContext sslContext = null;
125102
try {

httpclient/src/test/java/org/baeldung/httpclient/base/HttpClientLiveTest.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
package org.baeldung.httpclient.base;
22

3-
import static org.hamcrest.Matchers.emptyArray;
4-
import static org.hamcrest.Matchers.not;
5-
import static org.junit.Assert.assertThat;
6-
7-
import java.io.IOException;
8-
import java.io.InputStream;
9-
103
import org.apache.http.Header;
114
import org.apache.http.HttpEntity;
125
import org.apache.http.HttpHeaders;
13-
import org.apache.http.client.ClientProtocolException;
146
import org.apache.http.client.config.RequestConfig;
157
import org.apache.http.client.methods.CloseableHttpResponse;
168
import org.apache.http.client.methods.HttpGet;
@@ -23,6 +15,13 @@
2315
import org.junit.Before;
2416
import org.junit.Test;
2517

18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
21+
import static org.hamcrest.Matchers.emptyArray;
22+
import static org.hamcrest.Matchers.not;
23+
import static org.junit.Assert.assertThat;
24+
2625
public class HttpClientLiveTest {
2726

2827
private static final String SAMPLE_URL = "http://www.github.com";
@@ -56,7 +55,7 @@ public final void after() throws IllegalStateException, IOException {
5655
// tests
5756

5857
@Test(expected = ConnectTimeoutException.class)
59-
public final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws ClientProtocolException, IOException {
58+
public final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws IOException {
6059
final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(50).setConnectTimeout(50).setSocketTimeout(20).build();
6160
final HttpGet request = new HttpGet(SAMPLE_URL);
6261
request.setConfig(requestConfig);
@@ -66,20 +65,20 @@ public final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException(
6665
// tests - configs
6766

6867
@Test
69-
public final void givenHttpClientIsConfiguredWithCustomConnectionManager_whenExecutingRequest_thenNoExceptions() throws ClientProtocolException, IOException {
68+
public final void givenHttpClientIsConfiguredWithCustomConnectionManager_whenExecutingRequest_thenNoExceptions() throws IOException {
7069
instance = HttpClientBuilder.create().setConnectionManager(new BasicHttpClientConnectionManager()).build();
7170
response = instance.execute(new HttpGet(SAMPLE_URL));
7271
}
7372

7473
@Test
75-
public final void givenCustomHeaderIsSet_whenSendingRequest_thenNoExceptions() throws ClientProtocolException, IOException {
74+
public final void givenCustomHeaderIsSet_whenSendingRequest_thenNoExceptions() throws IOException {
7675
final HttpGet request = new HttpGet(SAMPLE_URL);
7776
request.addHeader(HttpHeaders.ACCEPT, "application/xml");
7877
response = instance.execute(request);
7978
}
8079

8180
@Test
82-
public final void givenRequestWasSet_whenAnalyzingTheHeadersOfTheResponse_thenCorrect() throws ClientProtocolException, IOException {
81+
public final void givenRequestWasSet_whenAnalyzingTheHeadersOfTheResponse_thenCorrect() throws IOException {
8382
response = instance.execute(new HttpGet(SAMPLE_URL));
8483

8584
final Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);
@@ -89,7 +88,7 @@ public final void givenRequestWasSet_whenAnalyzingTheHeadersOfTheResponse_thenCo
8988
// tests - cancel request
9089

9190
@Test
92-
public final void whenRequestIsCanceled_thenCorrect() throws ClientProtocolException, IOException {
91+
public final void whenRequestIsCanceled_thenCorrect() throws IOException {
9392
instance = HttpClients.custom().build();
9493
final HttpGet request = new HttpGet(SAMPLE_URL);
9594
response = instance.execute(request);

httpclient/src/test/java/org/baeldung/httpclient/base/HttpClientSandboxLiveTest.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
package org.baeldung.httpclient.base;
22

3-
import java.io.IOException;
4-
import java.io.InputStream;
5-
63
import org.apache.http.HttpEntity;
74
import org.apache.http.auth.AuthScope;
85
import org.apache.http.auth.UsernamePasswordCredentials;
9-
import org.apache.http.client.ClientProtocolException;
106
import org.apache.http.client.CredentialsProvider;
117
import org.apache.http.client.methods.CloseableHttpResponse;
128
import org.apache.http.client.methods.HttpGet;
13-
import org.apache.http.conn.ssl.NoopHostnameVerifier;
149
import org.apache.http.impl.client.BasicCredentialsProvider;
1510
import org.apache.http.impl.client.CloseableHttpClient;
1611
import org.apache.http.impl.client.HttpClientBuilder;
17-
import org.apache.http.impl.client.HttpClients;
1812
import org.junit.After;
1913
import org.junit.Test;
2014

15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
2118
public class HttpClientSandboxLiveTest {
2219

2320
private CloseableHttpClient client;
@@ -47,7 +44,7 @@ public final void after() throws IllegalStateException, IOException {
4744
// simple request - response
4845

4946
@Test
50-
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws ClientProtocolException, IOException {
47+
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws IOException {
5148
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
5249
final AuthScope authscp = new AuthScope("localhost", 8080);
5350
credentialsProvider.setCredentials(authscp, new UsernamePasswordCredentials("user1", "user1Pass"));

0 commit comments

Comments
 (0)