Skip to content

Commit bdaa336

Browse files
author
eugenp
committed
httpclient clenaup
1 parent c7ab61f commit bdaa336

6 files changed

Lines changed: 50 additions & 36 deletions

File tree

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,9 @@ public final void after() throws IllegalStateException, IOException {
5757

5858
// tests - headers - deprecated
5959

60-
@SuppressWarnings("deprecation")
6160
@Test
6261
public final void givenDeprecatedApi_whenClientUsesCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
63-
client = new DefaultHttpClient();
62+
client = HttpClients.custom().build();
6463
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 Firefox/26.0");
6564
HttpProtocolParams.setUserAgent(client.getParams(), "Mozilla/5.0 Firefox/26.0");
6665

@@ -86,16 +85,14 @@ public final void givenConfigOnClient_whenRequestHasCustomUserAgent_thenCorrect(
8685

8786
// tests - headers - content type
8887

89-
@SuppressWarnings("deprecation")
9088
@Test
9189
public final void givenUsingDeprecatedApi_whenRequestHasCustomContentType_thenCorrect() throws ClientProtocolException, IOException {
92-
client = new DefaultHttpClient();
90+
client = HttpClients.custom().build();
9391
final HttpGet request = new HttpGet(SAMPLE_URL);
9492
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
9593
response = client.execute(request);
9694
}
9795

98-
@SuppressWarnings("deprecation")
9996
@Test
10097
public final void givenRequestBuildWithBuilderWithDeprecatedApi_whenRequestHasCustomContentType_thenCorrect() throws ClientProtocolException, IOException {
10198
final DefaultHttpClient client2 = new DefaultHttpClient();

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public final void after() throws IllegalStateException, IOException {
5555

5656
// tests
5757

58-
@SuppressWarnings("deprecation")
5958
@Test
6059
public final void givenRedirectsAreDisabledViaDeprecatedApi_whenConsumingUrlWhichRedirects_thenNotRedirected() throws ClientProtocolException, IOException {
6160
instance = new DefaultHttpClient();
@@ -87,7 +86,6 @@ public final void givenPostRequest_whenConsumingUrlWhichRedirects_thenNotRedirec
8786
assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
8887
}
8988

90-
@SuppressWarnings("deprecation")
9189
@Test
9290
public final void givenRedirectingPOSTViaPre4_2Api_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() throws ClientProtocolException, IOException {
9391
final DefaultHttpClient client = new DefaultHttpClient();
@@ -110,7 +108,6 @@ protected boolean isRedirectable(final String method) {
110108
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
111109
}
112110

113-
@SuppressWarnings("deprecation")
114111
@Test
115112
public final void givenRedirectingPOSTVia4_2Api_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() throws ClientProtocolException, IOException {
116113
final DefaultHttpClient client = new DefaultHttpClient();

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public final void after() throws IllegalStateException, IOException {
4343

4444
// tests
4545

46-
@SuppressWarnings("deprecation")
4746
@Test
4847
public final void givenUsingDeprecatedApi_whenSettingTimeoutViaRawParams_thenCorrect() throws ClientProtocolException, IOException {
4948
final int timeout = 2;
@@ -61,7 +60,6 @@ public final void givenUsingDeprecatedApi_whenSettingTimeoutViaRawParams_thenCor
6160
client.close();
6261
}
6362

64-
@SuppressWarnings("deprecation")
6563
@Test
6664
public final void givenUsingDeprecatedApi_whenSettingTimeoutViaHigherLevelApi_thenCorrect() throws ClientProtocolException, IOException {
6765
final int timeout = 2;

spring-security-rest-basic-auth/src/test/java/org/baeldung/client/HttpsClientLiveManualTest.java renamed to httpclient/src/test/java/org/baeldung/httpclient/HttpsClientLiveManualTest.java

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
package org.baeldung.client;
1+
package org.baeldung.httpclient;
22

3-
import static org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
43
import static org.hamcrest.Matchers.equalTo;
54
import static org.junit.Assert.assertThat;
65

@@ -28,10 +27,6 @@
2827
import org.apache.http.impl.conn.PoolingClientConnectionManager;
2928
import org.junit.Ignore;
3029
import org.junit.Test;
31-
import org.springframework.http.HttpMethod;
32-
import org.springframework.http.ResponseEntity;
33-
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
34-
import org.springframework.web.client.RestTemplate;
3530

3631
/**
3732
* This test requires a localhost server over HTTPS <br>
@@ -76,25 +71,6 @@ public final boolean isTrusted(final X509Certificate[] certificate, final String
7671
httpClient.close();
7772
}
7873

79-
@Test
80-
public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException() throws GeneralSecurityException {
81-
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
82-
final CloseableHttpClient httpClient = (CloseableHttpClient) requestFactory.getHttpClient();
83-
84-
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
85-
@Override
86-
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
87-
return true;
88-
}
89-
};
90-
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, ALLOW_ALL_HOSTNAME_VERIFIER);
91-
httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 8443, sf));
92-
93-
final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1";
94-
final ResponseEntity<String> response = new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class);
95-
assertThat(response.getStatusCode().value(), equalTo(200));
96-
}
97-
9874
@Test
9975
public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
10076
final SSLContextBuilder builder = new SSLContextBuilder();

httpclient/src/test/java/org/baeldung/httpclient/sec/HttpClientCookieLiveTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public final void whenSettingCookiesOnARequest_thenCorrect() throws ClientProtoc
6363
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
6464
}
6565

66-
@SuppressWarnings("deprecation")
6766
@Test
6867
public final void givenUsingDeprecatedApi_whenSettingCookiesOnTheHttpClient_thenCorrect() throws ClientProtocolException, IOException {
6968
final BasicCookieStore cookieStore = new BasicCookieStore();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.baeldung.client;
2+
3+
import static org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
4+
import static org.hamcrest.Matchers.equalTo;
5+
import static org.junit.Assert.assertThat;
6+
7+
import java.security.GeneralSecurityException;
8+
import java.security.cert.X509Certificate;
9+
10+
import org.apache.http.conn.scheme.Scheme;
11+
import org.apache.http.conn.ssl.SSLSocketFactory;
12+
import org.apache.http.conn.ssl.TrustStrategy;
13+
import org.apache.http.impl.client.CloseableHttpClient;
14+
import org.junit.Test;
15+
import org.springframework.http.HttpMethod;
16+
import org.springframework.http.ResponseEntity;
17+
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
18+
import org.springframework.web.client.RestTemplate;
19+
20+
/**
21+
* This test requires a localhost server over HTTPS <br>
22+
* It should only be manually run, not part of the automated build
23+
* */
24+
public class RestClientLiveManualTest {
25+
26+
// tests
27+
28+
@Test
29+
public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException() throws GeneralSecurityException {
30+
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
31+
final CloseableHttpClient httpClient = (CloseableHttpClient) requestFactory.getHttpClient();
32+
33+
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
34+
@Override
35+
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
36+
return true;
37+
}
38+
};
39+
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, ALLOW_ALL_HOSTNAME_VERIFIER);
40+
httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 8443, sf));
41+
42+
final String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1";
43+
final ResponseEntity<String> response = new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class);
44+
assertThat(response.getStatusCode().value(), equalTo(200));
45+
}
46+
47+
}

0 commit comments

Comments
 (0)