httpclient clenaup
This commit is contained in:
@@ -57,10 +57,9 @@ public class HttpClientHeadersLiveTest {
|
||||
|
||||
// tests - headers - deprecated
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public final void givenDeprecatedApi_whenClientUsesCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
|
||||
client = new DefaultHttpClient();
|
||||
client = HttpClients.custom().build();
|
||||
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 Firefox/26.0");
|
||||
HttpProtocolParams.setUserAgent(client.getParams(), "Mozilla/5.0 Firefox/26.0");
|
||||
|
||||
@@ -86,16 +85,14 @@ public class HttpClientHeadersLiveTest {
|
||||
|
||||
// tests - headers - content type
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public final void givenUsingDeprecatedApi_whenRequestHasCustomContentType_thenCorrect() throws ClientProtocolException, IOException {
|
||||
client = new DefaultHttpClient();
|
||||
client = HttpClients.custom().build();
|
||||
final HttpGet request = new HttpGet(SAMPLE_URL);
|
||||
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||
response = client.execute(request);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public final void givenRequestBuildWithBuilderWithDeprecatedApi_whenRequestHasCustomContentType_thenCorrect() throws ClientProtocolException, IOException {
|
||||
final DefaultHttpClient client2 = new DefaultHttpClient();
|
||||
|
||||
@@ -55,7 +55,6 @@ public class HttpClientRedirectLiveTest {
|
||||
|
||||
// tests
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public final void givenRedirectsAreDisabledViaDeprecatedApi_whenConsumingUrlWhichRedirects_thenNotRedirected() throws ClientProtocolException, IOException {
|
||||
instance = new DefaultHttpClient();
|
||||
@@ -87,7 +86,6 @@ public class HttpClientRedirectLiveTest {
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public final void givenRedirectingPOSTViaPre4_2Api_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() throws ClientProtocolException, IOException {
|
||||
final DefaultHttpClient client = new DefaultHttpClient();
|
||||
@@ -110,7 +108,6 @@ public class HttpClientRedirectLiveTest {
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public final void givenRedirectingPOSTVia4_2Api_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() throws ClientProtocolException, IOException {
|
||||
final DefaultHttpClient client = new DefaultHttpClient();
|
||||
|
||||
@@ -43,7 +43,6 @@ public class HttpClientTimeoutLiveTest {
|
||||
|
||||
// tests
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public final void givenUsingDeprecatedApi_whenSettingTimeoutViaRawParams_thenCorrect() throws ClientProtocolException, IOException {
|
||||
final int timeout = 2;
|
||||
@@ -61,7 +60,6 @@ public class HttpClientTimeoutLiveTest {
|
||||
client.close();
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public final void givenUsingDeprecatedApi_whenSettingTimeoutViaHigherLevelApi_thenCorrect() throws ClientProtocolException, IOException {
|
||||
final int timeout = 2;
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package org.baeldung.httpclient;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
import javax.net.ssl.SSLPeerUnverifiedException;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.conn.ClientConnectionManager;
|
||||
import org.apache.http.conn.scheme.Scheme;
|
||||
import org.apache.http.conn.scheme.SchemeRegistry;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.conn.ssl.SSLContextBuilder;
|
||||
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.DefaultHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.PoolingClientConnectionManager;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* This test requires a localhost server over HTTPS <br>
|
||||
* It should only be manually run, not part of the automated build
|
||||
* */
|
||||
public class HttpsClientLiveManualTest {
|
||||
|
||||
// tests
|
||||
|
||||
@Test(expected = SSLPeerUnverifiedException.class)
|
||||
@Ignore("Only for a server that has HTTPS enabled (on 8443)")
|
||||
public final void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException {
|
||||
final CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
||||
|
||||
final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1";
|
||||
final HttpGet getMethod = new HttpGet(urlOverHttps);
|
||||
final HttpResponse response = httpClient.execute(getMethod);
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public final void givenHttpClientPre4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
|
||||
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
|
||||
@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 SchemeRegistry registry = new SchemeRegistry();
|
||||
registry.register(new Scheme("https", 8443, sf));
|
||||
final ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);
|
||||
|
||||
final CloseableHttpClient httpClient = new DefaultHttpClient(ccm);
|
||||
|
||||
final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1";
|
||||
final HttpGet getMethod = new HttpGet(urlOverHttps);
|
||||
final HttpResponse response = httpClient.execute(getMethod);
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
|
||||
httpClient.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
|
||||
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 String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1";
|
||||
final HttpGet getMethod = new HttpGet(urlOverHttps);
|
||||
final HttpResponse response = httpClient.execute(getMethod);
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -63,7 +63,6 @@ public class HttpClientCookieLiveTest {
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public final void givenUsingDeprecatedApi_whenSettingCookiesOnTheHttpClient_thenCorrect() throws ClientProtocolException, IOException {
|
||||
final BasicCookieStore cookieStore = new BasicCookieStore();
|
||||
|
||||
Reference in New Issue
Block a user