Refactor HttpClient test
This commit is contained in:
parent
b018c6ca3b
commit
d0a376e622
@ -1,30 +1,11 @@
|
|||||||
package org.baeldung.httpclient;
|
package org.baeldung.httpclient;
|
||||||
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.security.GeneralSecurityException;
|
|
||||||
import java.security.KeyManagementException;
|
|
||||||
import java.security.KeyStoreException;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
import java.security.cert.X509Certificate;
|
|
||||||
|
|
||||||
import javax.net.ssl.SSLContext;
|
|
||||||
import javax.net.ssl.SSLException;
|
|
||||||
|
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.client.ClientProtocolException;
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.conn.ClientConnectionManager;
|
import org.apache.http.conn.ClientConnectionManager;
|
||||||
import org.apache.http.conn.scheme.Scheme;
|
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.NoopHostnameVerifier;
|
import org.apache.http.conn.ssl.*;
|
||||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
||||||
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.TrustSelfSignedStrategy;
|
|
||||||
import org.apache.http.conn.ssl.TrustStrategy;
|
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
@ -32,6 +13,17 @@ import org.apache.http.impl.client.HttpClients;
|
|||||||
import org.apache.http.impl.conn.PoolingClientConnectionManager;
|
import org.apache.http.impl.conn.PoolingClientConnectionManager;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import javax.net.ssl.SSLContext;
|
||||||
|
import javax.net.ssl.SSLException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.security.GeneralSecurityException;
|
||||||
|
import java.security.KeyManagementException;
|
||||||
|
import java.security.KeyStoreException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This test requires a localhost server over HTTPS <br>
|
* This test requires a localhost server over HTTPS <br>
|
||||||
* It should only be manually run, not part of the automated build
|
* It should only be manually run, not part of the automated build
|
||||||
@ -45,7 +37,7 @@ public class HttpsClientSslLiveTest {
|
|||||||
// tests
|
// tests
|
||||||
|
|
||||||
@Test(expected = SSLException.class)
|
@Test(expected = SSLException.class)
|
||||||
public final void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException {
|
public final void whenHttpsUrlIsConsumed_thenException() throws 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);
|
||||||
@ -56,12 +48,7 @@ public class HttpsClientSslLiveTest {
|
|||||||
@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 = (certificate, authType) -> true;
|
||||||
@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 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));
|
||||||
@ -78,12 +65,7 @@ public class HttpsClientSslLiveTest {
|
|||||||
|
|
||||||
@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 = (certificate, authType) -> true;
|
||||||
@Override
|
|
||||||
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
|
final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
|
||||||
|
|
||||||
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
||||||
@ -112,14 +94,9 @@ public class HttpsClientSslLiveTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenIgnoringCertificates_whenHttpsUrlIsConsumed_thenCorrect() throws ClientProtocolException, IOException {
|
public final void givenIgnoringCertificates_whenHttpsUrlIsConsumed_thenCorrect() throws IOException {
|
||||||
|
|
||||||
TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
|
TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true;
|
||||||
@Override
|
|
||||||
public boolean isTrusted(X509Certificate[] certificate, String authType) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
SSLContext sslContext = null;
|
SSLContext sslContext = null;
|
||||||
try {
|
try {
|
||||||
|
@ -1,16 +1,8 @@
|
|||||||
package org.baeldung.httpclient.base;
|
package org.baeldung.httpclient.base;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.emptyArray;
|
|
||||||
import static org.hamcrest.Matchers.not;
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
import org.apache.http.Header;
|
import org.apache.http.Header;
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.HttpHeaders;
|
import org.apache.http.HttpHeaders;
|
||||||
import org.apache.http.client.ClientProtocolException;
|
|
||||||
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
@ -23,6 +15,13 @@ import org.junit.After;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.emptyArray;
|
||||||
|
import static org.hamcrest.Matchers.not;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
public class HttpClientLiveTest {
|
public class HttpClientLiveTest {
|
||||||
|
|
||||||
private static final String SAMPLE_URL = "http://www.github.com";
|
private static final String SAMPLE_URL = "http://www.github.com";
|
||||||
@ -56,7 +55,7 @@ public class HttpClientLiveTest {
|
|||||||
// tests
|
// tests
|
||||||
|
|
||||||
@Test(expected = ConnectTimeoutException.class)
|
@Test(expected = ConnectTimeoutException.class)
|
||||||
public final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws ClientProtocolException, IOException {
|
public final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws IOException {
|
||||||
final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(50).setConnectTimeout(50).setSocketTimeout(20).build();
|
final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(50).setConnectTimeout(50).setSocketTimeout(20).build();
|
||||||
final HttpGet request = new HttpGet(SAMPLE_URL);
|
final HttpGet request = new HttpGet(SAMPLE_URL);
|
||||||
request.setConfig(requestConfig);
|
request.setConfig(requestConfig);
|
||||||
@ -66,20 +65,20 @@ public class HttpClientLiveTest {
|
|||||||
// tests - configs
|
// tests - configs
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenHttpClientIsConfiguredWithCustomConnectionManager_whenExecutingRequest_thenNoExceptions() throws ClientProtocolException, IOException {
|
public final void givenHttpClientIsConfiguredWithCustomConnectionManager_whenExecutingRequest_thenNoExceptions() throws IOException {
|
||||||
instance = HttpClientBuilder.create().setConnectionManager(new BasicHttpClientConnectionManager()).build();
|
instance = HttpClientBuilder.create().setConnectionManager(new BasicHttpClientConnectionManager()).build();
|
||||||
response = instance.execute(new HttpGet(SAMPLE_URL));
|
response = instance.execute(new HttpGet(SAMPLE_URL));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenCustomHeaderIsSet_whenSendingRequest_thenNoExceptions() throws ClientProtocolException, IOException {
|
public final void givenCustomHeaderIsSet_whenSendingRequest_thenNoExceptions() throws IOException {
|
||||||
final HttpGet request = new HttpGet(SAMPLE_URL);
|
final HttpGet request = new HttpGet(SAMPLE_URL);
|
||||||
request.addHeader(HttpHeaders.ACCEPT, "application/xml");
|
request.addHeader(HttpHeaders.ACCEPT, "application/xml");
|
||||||
response = instance.execute(request);
|
response = instance.execute(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenRequestWasSet_whenAnalyzingTheHeadersOfTheResponse_thenCorrect() throws ClientProtocolException, IOException {
|
public final void givenRequestWasSet_whenAnalyzingTheHeadersOfTheResponse_thenCorrect() throws IOException {
|
||||||
response = instance.execute(new HttpGet(SAMPLE_URL));
|
response = instance.execute(new HttpGet(SAMPLE_URL));
|
||||||
|
|
||||||
final Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);
|
final Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);
|
||||||
@ -89,7 +88,7 @@ public class HttpClientLiveTest {
|
|||||||
// tests - cancel request
|
// tests - cancel request
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenRequestIsCanceled_thenCorrect() throws ClientProtocolException, IOException {
|
public final void whenRequestIsCanceled_thenCorrect() throws IOException {
|
||||||
instance = HttpClients.custom().build();
|
instance = HttpClients.custom().build();
|
||||||
final HttpGet request = new HttpGet(SAMPLE_URL);
|
final HttpGet request = new HttpGet(SAMPLE_URL);
|
||||||
response = instance.execute(request);
|
response = instance.execute(request);
|
||||||
|
@ -1,23 +1,20 @@
|
|||||||
package org.baeldung.httpclient.base;
|
package org.baeldung.httpclient.base;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.auth.AuthScope;
|
import org.apache.http.auth.AuthScope;
|
||||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
import org.apache.http.client.ClientProtocolException;
|
|
||||||
import org.apache.http.client.CredentialsProvider;
|
import org.apache.http.client.CredentialsProvider;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
|
||||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
public class HttpClientSandboxLiveTest {
|
public class HttpClientSandboxLiveTest {
|
||||||
|
|
||||||
private CloseableHttpClient client;
|
private CloseableHttpClient client;
|
||||||
@ -47,7 +44,7 @@ public class HttpClientSandboxLiveTest {
|
|||||||
// simple request - response
|
// simple request - response
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws ClientProtocolException, IOException {
|
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws IOException {
|
||||||
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||||
final AuthScope authscp = new AuthScope("localhost", 8080);
|
final AuthScope authscp = new AuthScope("localhost", 8080);
|
||||||
credentialsProvider.setCredentials(authscp, new UsernamePasswordCredentials("user1", "user1Pass"));
|
credentialsProvider.setCredentials(authscp, new UsernamePasswordCredentials("user1", "user1Pass"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user