Skip to content

Commit ccb9c4a

Browse files
author
eugenp
committed
cleanup and further http client examples
1 parent e597d29 commit ccb9c4a

2 files changed

Lines changed: 149 additions & 11 deletions

File tree

‎httpclient/src/test/java/org/baeldung/httpclient/HttpClientLiveTest.java‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import java.io.IOException;
1010
import java.io.InputStream;
11-
import java.net.SocketTimeoutException;
1211

1312
import org.apache.http.Header;
1413
import org.apache.http.HttpEntity;
@@ -18,8 +17,10 @@
1817
import org.apache.http.client.methods.CloseableHttpResponse;
1918
import org.apache.http.client.methods.HttpGet;
2019
import org.apache.http.client.methods.HttpPost;
20+
import org.apache.http.conn.ConnectTimeoutException;
2121
import org.apache.http.entity.ContentType;
2222
import org.apache.http.impl.client.CloseableHttpClient;
23+
import org.apache.http.impl.client.DefaultHttpClient;
2324
import org.apache.http.impl.client.HttpClientBuilder;
2425
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
2526
import org.apache.http.util.EntityUtils;
@@ -29,7 +30,7 @@
2930

3031
public class HttpClientLiveTest {
3132

32-
private static final String SAMPLE_URL = "http://www.google.com";
33+
private static final String SAMPLE_URL = "http://www.github.com";
3334

3435
private CloseableHttpClient instance;
3536

@@ -88,9 +89,9 @@ public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectBo
8889
assertThat(bodyAsString, notNullValue());
8990
}
9091

91-
@Test(expected = SocketTimeoutException.class)
92+
@Test(expected = ConnectTimeoutException.class)
9293
public final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws ClientProtocolException, IOException {
93-
final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(50).setConnectTimeout(50).setSocketTimeout(50).build();
94+
final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(50).setConnectTimeout(50).setSocketTimeout(20).build();
9495
final HttpGet request = new HttpGet(SAMPLE_URL);
9596
request.setConfig(requestConfig);
9697
response = instance.execute(request);
@@ -111,13 +112,6 @@ public final void givenHttpClientIsConfiguredWithCustomConnectionManager_whenExe
111112
response = instance.execute(new HttpGet(SAMPLE_URL));
112113
}
113114

114-
@Test
115-
public final void givenRedirectsAreDisabled_whenConsumingUrlWhichRedirects_thenNotRedirected() throws ClientProtocolException, IOException {
116-
instance = HttpClientBuilder.create().disableRedirectHandling().build();
117-
response = instance.execute(new HttpGet("http://t.co/I5YYd9tddw"));
118-
assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
119-
}
120-
121115
@Test
122116
public final void givenCustomHeaderIsSet_whenSendingRequest_thenNoExceptions() throws ClientProtocolException, IOException {
123117
final HttpGet request = new HttpGet(SAMPLE_URL);
@@ -133,4 +127,18 @@ public final void givenRequestWasSet_whenAnalyzingTheHeadersOfTheResponse_thenCo
133127
assertThat(headers, not(emptyArray()));
134128
}
135129

130+
// tests - headers
131+
132+
@SuppressWarnings("deprecation")
133+
@Test
134+
public final void givenDeprecatedApi_whenRequestHasCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
135+
instance = new DefaultHttpClient();
136+
final HttpGet request = new HttpGet(SAMPLE_URL);
137+
request.setHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 Firefox/26.0");
138+
139+
response = instance.execute(request);
140+
141+
System.out.println(response);
142+
}
143+
136144
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package org.baeldung.httpclient;
2+
3+
import static org.hamcrest.Matchers.equalTo;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
9+
import org.apache.http.HttpEntity;
10+
import org.apache.http.client.ClientProtocolException;
11+
import org.apache.http.client.methods.CloseableHttpResponse;
12+
import org.apache.http.client.methods.HttpGet;
13+
import org.apache.http.client.methods.HttpHead;
14+
import org.apache.http.client.methods.HttpPost;
15+
import org.apache.http.client.params.ClientPNames;
16+
import org.apache.http.client.params.HttpClientParams;
17+
import org.apache.http.impl.client.CloseableHttpClient;
18+
import org.apache.http.impl.client.DefaultHttpClient;
19+
import org.apache.http.impl.client.DefaultRedirectStrategy;
20+
import org.apache.http.impl.client.HttpClientBuilder;
21+
import org.apache.http.impl.client.LaxRedirectStrategy;
22+
import org.apache.http.params.BasicHttpParams;
23+
import org.apache.http.params.HttpParams;
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
28+
public class HttpClientRedirectLiveTest {
29+
30+
private CloseableHttpClient instance;
31+
32+
private CloseableHttpResponse response;
33+
34+
@Before
35+
public final void before() {
36+
instance = HttpClientBuilder.create().build();
37+
}
38+
39+
@After
40+
public final void after() throws IllegalStateException, IOException {
41+
if (response == null) {
42+
return;
43+
}
44+
45+
try {
46+
final HttpEntity entity = response.getEntity();
47+
if (entity != null) {
48+
final InputStream instream = entity.getContent();
49+
instream.close();
50+
}
51+
} finally {
52+
response.close();
53+
}
54+
}
55+
56+
// tests
57+
58+
@SuppressWarnings("deprecation")
59+
@Test
60+
public final void givenRedirectsAreDisabledViaDeprecatedApi_whenConsumingUrlWhichRedirects_thenNotRedirected() throws ClientProtocolException, IOException {
61+
instance = new DefaultHttpClient();
62+
63+
final HttpParams params = new BasicHttpParams();
64+
params.setParameter(ClientPNames.HANDLE_REDIRECTS, false);
65+
HttpClientParams.setRedirecting(params, false);
66+
67+
final HttpGet httpGet = new HttpGet("http://t.co/I5YYd9tddw");
68+
httpGet.setParams(params);
69+
response = instance.execute(httpGet);
70+
71+
assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
72+
}
73+
74+
@Test
75+
public final void givenRedirectsAreDisabled_whenConsumingUrlWhichRedirects_thenNotRedirected() throws ClientProtocolException, IOException {
76+
instance = HttpClientBuilder.create().disableRedirectHandling().build();
77+
response = instance.execute(new HttpGet("http://t.co/I5YYd9tddw"));
78+
assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
79+
}
80+
81+
// redirect with POST
82+
83+
@Test
84+
public final void givenPostRequest_whenConsumingUrlWhichRedirects_thenNotRedirected() throws ClientProtocolException, IOException {
85+
instance = HttpClientBuilder.create().build();
86+
response = instance.execute(new HttpPost("http://t.co/I5YYd9tddw"));
87+
assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
88+
}
89+
90+
@SuppressWarnings("deprecation")
91+
@Test
92+
public final void givenRedirectingPOSTViaPre4_2Api_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() throws ClientProtocolException, IOException {
93+
final DefaultHttpClient client = new DefaultHttpClient();
94+
client.setRedirectStrategy(new DefaultRedirectStrategy() {
95+
/** Redirectable methods. */
96+
private final String[] REDIRECT_METHODS = new String[] { HttpGet.METHOD_NAME, HttpPost.METHOD_NAME, HttpHead.METHOD_NAME };
97+
98+
@Override
99+
protected boolean isRedirectable(final String method) {
100+
for (final String m : REDIRECT_METHODS) {
101+
if (m.equalsIgnoreCase(method)) {
102+
return true;
103+
}
104+
}
105+
return false;
106+
}
107+
});
108+
109+
response = client.execute(new HttpPost("http://t.co/I5YYd9tddw"));
110+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
111+
}
112+
113+
@SuppressWarnings("deprecation")
114+
@Test
115+
public final void givenRedirectingPOSTVia4_2Api_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() throws ClientProtocolException, IOException {
116+
final DefaultHttpClient client = new DefaultHttpClient();
117+
client.setRedirectStrategy(new LaxRedirectStrategy());
118+
119+
response = client.execute(new HttpPost("http://t.co/I5YYd9tddw"));
120+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
121+
}
122+
123+
@Test
124+
public final void givenRedirectingPOST_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() throws ClientProtocolException, IOException {
125+
instance = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
126+
response = instance.execute(new HttpPost("http://t.co/I5YYd9tddw"));
127+
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
128+
}
129+
130+
}

0 commit comments

Comments
 (0)