httpclient - custom header

This commit is contained in:
eugenp 2014-01-25 17:11:42 +02:00
parent 29f02a2a8b
commit 4c5bb1b133
2 changed files with 123 additions and 30 deletions

View File

@ -0,0 +1,123 @@
package org.baeldung.httpclient;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpProtocolParams;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.google.common.collect.Lists;
public class HttpClientHeadersLiveTest {
private static final String SAMPLE_URL = "http://www.github.com";
private CloseableHttpClient client;
private CloseableHttpResponse response;
@Before
public final void before() {
client = HttpClientBuilder.create().build();
}
@After
public final void after() throws IllegalStateException, IOException {
if (response == null) {
return;
}
try {
final HttpEntity entity = response.getEntity();
if (entity != null) {
final InputStream instream = entity.getContent();
instream.close();
}
} finally {
response.close();
}
}
// tests - headers - deprecated
@SuppressWarnings("deprecation")
@Test
public final void givenDeprecatedApi_whenClientUsesCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 Firefox/26.0");
HttpProtocolParams.setUserAgent(client.getParams(), "Mozilla/5.0 Firefox/26.0");
final HttpGet request = new HttpGet(SAMPLE_URL);
response = client.execute(request);
}
// tests - headers - user agent
@Test
public final void givenConfigOnRequest_whenRequestHasCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
client = HttpClients.custom().build();
final HttpGet request = new HttpGet(SAMPLE_URL);
request.setHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 Firefox/26.0");
response = client.execute(request);
}
@Test
public final void givenConfigOnClient_whenRequestHasCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
client = HttpClients.custom().setUserAgent("Mozilla/5.0 Firefox/26.0").build();
response = client.execute(new HttpGet(SAMPLE_URL));
}
// tests - headers - content type
@SuppressWarnings("deprecation")
@Test
public final void givenUsingDeprecatedApi_whenRequestHasCustomContentType_thenCorrect() throws ClientProtocolException, IOException {
client = new DefaultHttpClient();
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();
final HttpGet request = new HttpGet(SAMPLE_URL);
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
response = client2.execute(request);
}
@Test
public final void givenRequestBuildWithBuilder_whenRequestHasCustomContentType_thenCorrect() throws ClientProtocolException, IOException {
client = HttpClients.custom().build();
final HttpUriRequest request = RequestBuilder.get().setUri(SAMPLE_URL).setHeader(HttpHeaders.CONTENT_TYPE, "application/json").build();
response = client.execute(request);
}
@Test
public final void givenConfigOnClient_whenRequestHasCustomContentType_thenCorrect() throws ClientProtocolException, IOException {
final Header header = new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json");
final List<Header> headers = Lists.newArrayList(header);
client = HttpClients.custom().setDefaultHeaders(headers).build();
final HttpUriRequest request = RequestBuilder.get().setUri(SAMPLE_URL).build();
response = client.execute(request);
}
}

View File

@ -16,12 +16,9 @@ import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpProtocolParams;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -89,33 +86,6 @@ public class HttpClientLiveTest {
assertThat(headers, not(emptyArray()));
}
// tests - headers
@SuppressWarnings("deprecation")
@Test
public final void givenDeprecatedApi_whenClientUsesCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
final DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 Firefox/26.0");
HttpProtocolParams.setUserAgent(client.getParams(), "Mozilla/5.0 Firefox/26.0");
final HttpGet request = new HttpGet(SAMPLE_URL);
response = client.execute(request);
}
@Test
public final void givenDeprecatedApi_whenRequestHasCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
instance = HttpClients.custom().build();
final HttpGet request = new HttpGet(SAMPLE_URL);
request.setHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 Firefox/26.0");
response = instance.execute(request);
}
@Test
public final void whenRequestHasCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException {
instance = HttpClients.custom().setUserAgent("Mozilla/5.0 Firefox/26.0").build();
response = instance.execute(new HttpGet(SAMPLE_URL));
}
// tests - cancel request
@Test