|
| 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 | +} |
0 commit comments