From bdaa336264afffc89808d6453b1804f4914584e8 Mon Sep 17 00:00:00 2001 From: eugenp Date: Mon, 24 Nov 2014 14:53:05 +0200 Subject: [PATCH] httpclient clenaup --- .../httpclient/HttpClientHeadersLiveTest.java | 7 +-- .../HttpClientRedirectLiveTest.java | 3 -- .../httpclient/HttpClientTimeoutLiveTest.java | 2 - .../HttpsClientLiveManualTest.java | 26 +--------- .../sec/HttpClientCookieLiveTest.java | 1 - .../client/RestClientLiveManualTest.java | 47 +++++++++++++++++++ 6 files changed, 50 insertions(+), 36 deletions(-) rename {spring-security-rest-basic-auth/src/test/java/org/baeldung/client => httpclient/src/test/java/org/baeldung/httpclient}/HttpsClientLiveManualTest.java (73%) create mode 100644 spring-security-rest-basic-auth/src/test/java/org/baeldung/client/RestClientLiveManualTest.java diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientHeadersLiveTest.java b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientHeadersLiveTest.java index 081f83c905..3a97d4d09a 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientHeadersLiveTest.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientHeadersLiveTest.java @@ -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(); diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientRedirectLiveTest.java b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientRedirectLiveTest.java index 3d01a02fbb..fa3d20a483 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientRedirectLiveTest.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientRedirectLiveTest.java @@ -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(); diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java index 85d7f3456e..41b8d0551c 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java @@ -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; diff --git a/spring-security-rest-basic-auth/src/test/java/org/baeldung/client/HttpsClientLiveManualTest.java b/httpclient/src/test/java/org/baeldung/httpclient/HttpsClientLiveManualTest.java similarity index 73% rename from spring-security-rest-basic-auth/src/test/java/org/baeldung/client/HttpsClientLiveManualTest.java rename to httpclient/src/test/java/org/baeldung/httpclient/HttpsClientLiveManualTest.java index 4d371dc1f0..df3ac3f25b 100644 --- a/spring-security-rest-basic-auth/src/test/java/org/baeldung/client/HttpsClientLiveManualTest.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/HttpsClientLiveManualTest.java @@ -1,6 +1,5 @@ -package org.baeldung.client; +package org.baeldung.httpclient; -import static org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER; import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertThat; @@ -28,10 +27,6 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingClientConnectionManager; import org.junit.Ignore; import org.junit.Test; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; -import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; -import org.springframework.web.client.RestTemplate; /** * This test requires a localhost server over HTTPS
@@ -76,25 +71,6 @@ public class HttpsClientLiveManualTest { httpClient.close(); } - @Test - public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException() throws GeneralSecurityException { - final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); - final CloseableHttpClient httpClient = (CloseableHttpClient) requestFactory.getHttpClient(); - - final TrustStrategy acceptingTrustStrategy = new TrustStrategy() { - @Override - public final boolean isTrusted(final X509Certificate[] certificate, final String authType) { - return true; - } - }; - final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, ALLOW_ALL_HOSTNAME_VERIFIER); - httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 8443, sf)); - - final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1"; - final ResponseEntity response = new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class); - assertThat(response.getStatusCode().value(), equalTo(200)); - } - @Test public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException { final SSLContextBuilder builder = new SSLContextBuilder(); diff --git a/httpclient/src/test/java/org/baeldung/httpclient/sec/HttpClientCookieLiveTest.java b/httpclient/src/test/java/org/baeldung/httpclient/sec/HttpClientCookieLiveTest.java index a36569a34b..8ad3e13d82 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/sec/HttpClientCookieLiveTest.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/sec/HttpClientCookieLiveTest.java @@ -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(); diff --git a/spring-security-rest-basic-auth/src/test/java/org/baeldung/client/RestClientLiveManualTest.java b/spring-security-rest-basic-auth/src/test/java/org/baeldung/client/RestClientLiveManualTest.java new file mode 100644 index 0000000000..34c50f4b6d --- /dev/null +++ b/spring-security-rest-basic-auth/src/test/java/org/baeldung/client/RestClientLiveManualTest.java @@ -0,0 +1,47 @@ +package org.baeldung.client; + +import static org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +import java.security.GeneralSecurityException; +import java.security.cert.X509Certificate; + +import org.apache.http.conn.scheme.Scheme; +import org.apache.http.conn.ssl.SSLSocketFactory; +import org.apache.http.conn.ssl.TrustStrategy; +import org.apache.http.impl.client.CloseableHttpClient; +import org.junit.Test; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.web.client.RestTemplate; + +/** + * This test requires a localhost server over HTTPS
+ * It should only be manually run, not part of the automated build + * */ +public class RestClientLiveManualTest { + + // tests + + @Test + public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException() throws GeneralSecurityException { + final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); + final CloseableHttpClient httpClient = (CloseableHttpClient) requestFactory.getHttpClient(); + + final TrustStrategy acceptingTrustStrategy = new TrustStrategy() { + @Override + public final boolean isTrusted(final X509Certificate[] certificate, final String authType) { + return true; + } + }; + final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, ALLOW_ALL_HOSTNAME_VERIFIER); + httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 8443, sf)); + + final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1"; + final ResponseEntity response = new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class); + assertThat(response.getStatusCode().value(), equalTo(200)); + } + +}