Skip to content

Commit eb49512

Browse files
committed
upgrade httpClient test
1 parent 2e93891 commit eb49512

File tree

3 files changed

+23
-47
lines changed

3 files changed

+23
-47
lines changed

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@
1313
import org.apache.http.client.methods.HttpUriRequest;
1414
import org.apache.http.client.methods.RequestBuilder;
1515
import org.apache.http.impl.client.CloseableHttpClient;
16-
import org.apache.http.impl.client.DefaultHttpClient;
1716
import org.apache.http.impl.client.HttpClientBuilder;
1817
import org.apache.http.impl.client.HttpClients;
1918
import org.apache.http.message.BasicHeader;
20-
import org.apache.http.params.CoreProtocolPNames;
21-
import org.apache.http.params.HttpProtocolParams;
2219
import org.junit.After;
2320
import org.junit.Before;
2421
import org.junit.Test;
@@ -58,10 +55,8 @@ public final void after() throws IllegalStateException, IOException {
5855
// tests - headers - deprecated
5956

6057
@Test
61-
public final void givenDeprecatedApi_whenClientUsesCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
62-
client = HttpClients.custom().build();
63-
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 Firefox/26.0");
64-
HttpProtocolParams.setUserAgent(client.getParams(), "Mozilla/5.0 Firefox/26.0");
58+
public final void givenNewApi_whenClientUsesCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
59+
client = HttpClients.custom().setUserAgent("Mozilla/5.0 Firefox/26.0").build();
6560

6661
final HttpGet request = new HttpGet(SAMPLE_URL);
6762
response = client.execute(request);
@@ -86,16 +81,16 @@ public final void givenConfigOnClient_whenRequestHasCustomUserAgent_thenCorrect(
8681
// tests - headers - content type
8782

8883
@Test
89-
public final void givenUsingDeprecatedApi_whenRequestHasCustomContentType_thenCorrect() throws ClientProtocolException, IOException {
84+
public final void givenUsingNewApi_whenRequestHasCustomContentType_thenCorrect() throws ClientProtocolException, IOException {
9085
client = HttpClients.custom().build();
9186
final HttpGet request = new HttpGet(SAMPLE_URL);
9287
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
9388
response = client.execute(request);
9489
}
9590

9691
@Test
97-
public final void givenRequestBuildWithBuilderWithDeprecatedApi_whenRequestHasCustomContentType_thenCorrect() throws ClientProtocolException, IOException {
98-
final DefaultHttpClient client2 = new DefaultHttpClient();
92+
public final void givenRequestBuildWithBuilderWithNewApi_whenRequestHasCustomContentType_thenCorrect() throws ClientProtocolException, IOException {
93+
final CloseableHttpClient client2 = HttpClients.custom().build();
9994
final HttpGet request = new HttpGet(SAMPLE_URL);
10095
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
10196
response = client2.execute(request);

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

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,11 @@
1212
import org.apache.http.client.methods.HttpGet;
1313
import org.apache.http.client.methods.HttpHead;
1414
import org.apache.http.client.methods.HttpPost;
15-
import org.apache.http.client.params.ClientPNames;
16-
import org.apache.http.client.params.HttpClientParams;
1715
import org.apache.http.impl.client.CloseableHttpClient;
18-
import org.apache.http.impl.client.DefaultHttpClient;
1916
import org.apache.http.impl.client.DefaultRedirectStrategy;
2017
import org.apache.http.impl.client.HttpClientBuilder;
18+
import org.apache.http.impl.client.HttpClients;
2119
import org.apache.http.impl.client.LaxRedirectStrategy;
22-
import org.apache.http.params.BasicHttpParams;
23-
import org.apache.http.params.HttpParams;
2420
import org.junit.After;
2521
import org.junit.Before;
2622
import org.junit.Test;
@@ -56,15 +52,10 @@ public final void after() throws IllegalStateException, IOException {
5652
// tests
5753

5854
@Test
59-
public final void givenRedirectsAreDisabledViaDeprecatedApi_whenConsumingUrlWhichRedirects_thenNotRedirected() throws ClientProtocolException, IOException {
60-
instance = new DefaultHttpClient();
61-
62-
final HttpParams params = new BasicHttpParams();
63-
params.setParameter(ClientPNames.HANDLE_REDIRECTS, false);
64-
HttpClientParams.setRedirecting(params, false);
55+
public final void givenRedirectsAreDisabledViaNewApi_whenConsumingUrlWhichRedirects_thenNotRedirected() throws ClientProtocolException, IOException {
56+
instance = HttpClients.custom().disableRedirectHandling().build();
6557

6658
final HttpGet httpGet = new HttpGet("http://t.co/I5YYd9tddw");
67-
httpGet.setParams(params);
6859
response = instance.execute(httpGet);
6960

7061
assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
@@ -87,9 +78,8 @@ public final void givenPostRequest_whenConsumingUrlWhichRedirects_thenNotRedirec
8778
}
8879

8980
@Test
90-
public final void givenRedirectingPOSTViaPre4_2Api_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() throws ClientProtocolException, IOException {
91-
final DefaultHttpClient client = new DefaultHttpClient();
92-
client.setRedirectStrategy(new DefaultRedirectStrategy() {
81+
public final void givenRedirectingPOSTViaPost4_2Api_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() throws ClientProtocolException, IOException {
82+
final CloseableHttpClient client = HttpClients.custom().setRedirectStrategy(new DefaultRedirectStrategy() {
9383
/** Redirectable methods. */
9484
private final String[] REDIRECT_METHODS = new String[] { HttpGet.METHOD_NAME, HttpPost.METHOD_NAME, HttpHead.METHOD_NAME };
9585

@@ -102,16 +92,15 @@ protected boolean isRedirectable(final String method) {
10292
}
10393
return false;
10494
}
105-
});
95+
}).build();
10696

10797
response = client.execute(new HttpPost("http://t.co/I5YYd9tddw"));
10898
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
10999
}
110100

111101
@Test
112102
public final void givenRedirectingPOSTVia4_2Api_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() throws ClientProtocolException, IOException {
113-
final DefaultHttpClient client = new DefaultHttpClient();
114-
client.setRedirectStrategy(new LaxRedirectStrategy());
103+
final CloseableHttpClient client = HttpClients.custom().setRedirectStrategy(new LaxRedirectStrategy()).build();
115104

116105
response = client.execute(new HttpPost("http://t.co/I5YYd9tddw"));
117106
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));

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

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@
1111
import org.apache.http.client.config.RequestConfig;
1212
import org.apache.http.client.methods.CloseableHttpResponse;
1313
import org.apache.http.client.methods.HttpGet;
14-
import org.apache.http.conn.ConnectTimeoutException;
14+
import org.apache.http.config.SocketConfig;
15+
import org.apache.http.conn.HttpHostConnectException;
1516
import org.apache.http.impl.client.CloseableHttpClient;
16-
import org.apache.http.impl.client.DefaultHttpClient;
1717
import org.apache.http.impl.client.HttpClientBuilder;
18-
import org.apache.http.params.CoreConnectionPNames;
19-
import org.apache.http.params.HttpConnectionParams;
20-
import org.apache.http.params.HttpParams;
2118
import org.junit.After;
2219
import org.junit.Test;
2320

@@ -45,36 +42,31 @@ public final void after() throws IllegalStateException, IOException {
4542
// tests
4643

4744
@Test
48-
public final void givenUsingDeprecatedApi_whenSettingTimeoutViaRawParams_thenCorrect() throws ClientProtocolException, IOException {
45+
public final void givenUsingNewApi_whenSettingTimeoutViaRequestConfig_thenCorrect() throws ClientProtocolException, IOException {
4946
final int timeout = 2;
50-
final DefaultHttpClient client = new DefaultHttpClient();
47+
final RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build();
48+
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
5149
final HttpGet request = new HttpGet("http://www.github.com");
5250

53-
final HttpParams httpParams = client.getParams();
54-
httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000);
55-
httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout * 1000);
5651
// httpParams.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, new Long(timeout * 1000)); // https://issues.apache.org/jira/browse/HTTPCLIENT-1418
5752

5853
response = client.execute(request);
5954

6055
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
61-
client.close();
6256
}
6357

6458
@Test
65-
public final void givenUsingDeprecatedApi_whenSettingTimeoutViaHigherLevelApi_thenCorrect() throws ClientProtocolException, IOException {
59+
public final void givenUsingNewApi_whenSettingTimeoutViaSocketConfig_thenCorrect() throws ClientProtocolException, IOException {
6660
final int timeout = 2;
67-
final DefaultHttpClient client = new DefaultHttpClient();
68-
final HttpGet request = new HttpGet("http://www.github.com");
6961

70-
final HttpParams httpParams = client.getParams();
71-
HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000); // http.connection.timeout
72-
HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000); // http.socket.timeout
62+
final SocketConfig config = SocketConfig.custom().setSoTimeout(timeout * 1000).build();
63+
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultSocketConfig(config).build();
64+
65+
final HttpGet request = new HttpGet("http://www.github.com");
7366

7467
response = client.execute(request);
7568

7669
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
77-
client.close();
7870
}
7971

8072
@Test
@@ -94,7 +86,7 @@ public final void givenUsingNewApi_whenSettingTimeoutViaHighLevelApi_thenCorrect
9486
/**
9587
* This simulates a timeout against a domain with multiple routes/IPs to it (not a single raw IP)
9688
*/
97-
@Test(expected = ConnectTimeoutException.class)
89+
@Test(expected = HttpHostConnectException.class)
9890
public final void givenTimeoutIsConfigured_whenTimingOut_thenTimeoutException() throws ClientProtocolException, IOException {
9991
final int timeout = 3;
10092

0 commit comments

Comments
 (0)