|
| 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