Skip to content

Commit 4c5bb1b

Browse files
author
eugenp
committed
httpclient - custom header
1 parent 29f02a2 commit 4c5bb1b

2 files changed

Lines changed: 123 additions & 30 deletions

File tree

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package org.baeldung.httpclient;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.util.List;
6+
7+
import org.apache.http.Header;
8+
import org.apache.http.HttpEntity;
9+
import org.apache.http.HttpHeaders;
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.HttpUriRequest;
14+
import org.apache.http.client.methods.RequestBuilder;
15+
import org.apache.http.impl.client.CloseableHttpClient;
16+
import org.apache.http.impl.client.DefaultHttpClient;
17+
import org.apache.http.impl.client.HttpClientBuilder;
18+
import org.apache.http.impl.client.HttpClients;
19+
import org.apache.http.message.BasicHeader;
20+
import org.apache.http.params.CoreProtocolPNames;
21+
import org.apache.http.params.HttpProtocolParams;
22+
import org.junit.After;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
26+
import com.google.common.collect.Lists;
27+
28+
public class HttpClientHeadersLiveTest {
29+
30+
private static final String SAMPLE_URL = "http://www.github.com";
31+
32+
private CloseableHttpClient client;
33+
34+
private CloseableHttpResponse response;
35+
36+
@Before
37+
public final void before() {
38+
client = HttpClientBuilder.create().build();
39+
}
40+
41+
@After
42+
public final void after() throws IllegalStateException, IOException {
43+
if (response == null) {
44+
return;
45+
}
46+
47+
try {
48+
final HttpEntity entity = response.getEntity();
49+
if (entity != null) {
50+
final InputStream instream = entity.getContent();
51+
instream.close();
52+
}
53+
} finally {
54+
response.close();
55+
}
56+
}
57+
58+
// tests - headers - deprecated
59+
60+
@SuppressWarnings("deprecation")
61+
@Test
62+
public final void givenDeprecatedApi_whenClientUsesCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
63+
client = new DefaultHttpClient();
64+
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 Firefox/26.0");
65+
HttpProtocolParams.setUserAgent(client.getParams(), "Mozilla/5.0 Firefox/26.0");
66+
67+
final HttpGet request = new HttpGet(SAMPLE_URL);
68+
response = client.execute(request);
69+
}
70+
71+
// tests - headers - user agent
72+
73+
@Test
74+
public final void givenConfigOnRequest_whenRequestHasCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
75+
client = HttpClients.custom().build();
76+
final HttpGet request = new HttpGet(SAMPLE_URL);
77+
request.setHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 Firefox/26.0");
78+
response = client.execute(request);
79+
}
80+
81+
@Test
82+
public final void givenConfigOnClient_whenRequestHasCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
83+
client = HttpClients.custom().setUserAgent("Mozilla/5.0 Firefox/26.0").build();
84+
response = client.execute(new HttpGet(SAMPLE_URL));
85+
}
86+
87+
// tests - headers - content type
88+
89+
@SuppressWarnings("deprecation")
90+
@Test
91+
public final void givenUsingDeprecatedApi_whenRequestHasCustomContentType_thenCorrect() throws ClientProtocolException, IOException {
92+
client = new DefaultHttpClient();
93+
final HttpGet request = new HttpGet(SAMPLE_URL);
94+
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
95+
response = client.execute(request);
96+
}
97+
98+
@SuppressWarnings("deprecation")
99+
@Test
100+
public final void givenRequestBuildWithBuilderWithDeprecatedApi_whenRequestHasCustomContentType_thenCorrect() throws ClientProtocolException, IOException {
101+
final DefaultHttpClient client2 = new DefaultHttpClient();
102+
final HttpGet request = new HttpGet(SAMPLE_URL);
103+
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
104+
response = client2.execute(request);
105+
}
106+
107+
@Test
108+
public final void givenRequestBuildWithBuilder_whenRequestHasCustomContentType_thenCorrect() throws ClientProtocolException, IOException {
109+
client = HttpClients.custom().build();
110+
final HttpUriRequest request = RequestBuilder.get().setUri(SAMPLE_URL).setHeader(HttpHeaders.CONTENT_TYPE, "application/json").build();
111+
response = client.execute(request);
112+
}
113+
114+
@Test
115+
public final void givenConfigOnClient_whenRequestHasCustomContentType_thenCorrect() throws ClientProtocolException, IOException {
116+
final Header header = new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json");
117+
final List<Header> headers = Lists.newArrayList(header);
118+
client = HttpClients.custom().setDefaultHeaders(headers).build();
119+
final HttpUriRequest request = RequestBuilder.get().setUri(SAMPLE_URL).build();
120+
response = client.execute(request);
121+
}
122+
123+
}

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

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@
1616
import org.apache.http.client.methods.HttpGet;
1717
import org.apache.http.conn.ConnectTimeoutException;
1818
import org.apache.http.impl.client.CloseableHttpClient;
19-
import org.apache.http.impl.client.DefaultHttpClient;
2019
import org.apache.http.impl.client.HttpClientBuilder;
2120
import org.apache.http.impl.client.HttpClients;
2221
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
23-
import org.apache.http.params.CoreProtocolPNames;
24-
import org.apache.http.params.HttpProtocolParams;
2522
import org.junit.After;
2623
import org.junit.Before;
2724
import org.junit.Test;
@@ -89,33 +86,6 @@ public final void givenRequestWasSet_whenAnalyzingTheHeadersOfTheResponse_thenCo
8986
assertThat(headers, not(emptyArray()));
9087
}
9188

92-
// tests - headers
93-
94-
@SuppressWarnings("deprecation")
95-
@Test
96-
public final void givenDeprecatedApi_whenClientUsesCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
97-
final DefaultHttpClient client = new DefaultHttpClient();
98-
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 Firefox/26.0");
99-
HttpProtocolParams.setUserAgent(client.getParams(), "Mozilla/5.0 Firefox/26.0");
100-
101-
final HttpGet request = new HttpGet(SAMPLE_URL);
102-
response = client.execute(request);
103-
}
104-
105-
@Test
106-
public final void givenDeprecatedApi_whenRequestHasCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
107-
instance = HttpClients.custom().build();
108-
final HttpGet request = new HttpGet(SAMPLE_URL);
109-
request.setHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 Firefox/26.0");
110-
response = instance.execute(request);
111-
}
112-
113-
@Test
114-
public final void whenRequestHasCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
115-
instance = HttpClients.custom().setUserAgent("Mozilla/5.0 Firefox/26.0").build();
116-
response = instance.execute(new HttpGet(SAMPLE_URL));
117-
}
118-
11989
// tests - cancel request
12090

12191
@Test

0 commit comments

Comments
 (0)