diff --git a/apache-httpclient/src/test/java/com/baeldung/httpclient/HttpClientRedirectLiveTest.java b/apache-httpclient/src/test/java/com/baeldung/httpclient/HttpClientRedirectLiveTest.java index 9a03ab02a5..04fad84333 100644 --- a/apache-httpclient/src/test/java/com/baeldung/httpclient/HttpClientRedirectLiveTest.java +++ b/apache-httpclient/src/test/java/com/baeldung/httpclient/HttpClientRedirectLiveTest.java @@ -1,98 +1,61 @@ package com.baeldung.httpclient; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpHead; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.DefaultRedirectStrategy; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.impl.client.LaxRedirectStrategy; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.apache.hc.client5.http.impl.DefaultRedirectStrategy; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; import java.io.IOException; -import java.util.Arrays; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; - -public class HttpClientRedirectLiveTest { - - private CloseableHttpClient instance; - - private CloseableHttpResponse response; - - @Before - public final void before() { - instance = HttpClientBuilder.create().build(); - } - - @After - public final void after() throws IllegalStateException, IOException { - ResponseUtil.closeResponse(response); - } - - // tests +class HttpClientRedirectLiveTest { @Test - public final void givenRedirectsAreDisabledViaNewApi_whenConsumingUrlWhichRedirects_thenNotRedirected() throws IOException { - instance = HttpClients.custom().disableRedirectHandling().build(); + void givenRedirectsAreDisabled_whenConsumingUrlWhichRedirects_thenNotRedirected() throws IOException { - final HttpGet httpGet = new HttpGet("http://t.co/I5YYd9tddw"); - response = instance.execute(httpGet); + final HttpGet request = new HttpGet("http://t.co/I5YYd9tddw"); - assertThat(response.getStatusLine().getStatusCode(), equalTo(301)); + try (CloseableHttpClient httpClient = HttpClients.custom() + .disableRedirectHandling() + .build()) { + httpClient.execute(request, response -> { + assertThat(response.getCode(), equalTo(301)); + return response; + }); + } } @Test - public final void givenRedirectsAreDisabled_whenConsumingUrlWhichRedirects_thenNotRedirected() throws IOException { - instance = HttpClientBuilder.create().disableRedirectHandling().build(); - response = instance.execute(new HttpGet("http://t.co/I5YYd9tddw")); - assertThat(response.getStatusLine().getStatusCode(), equalTo(301)); - } + void givenRedirectingPOST_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() throws IOException { - // redirect with POST + final HttpPost request = new HttpPost("http://t.co/I5YYd9tddw"); - @Test - public final void givenPostRequest_whenConsumingUrlWhichRedirects_thenNotRedirected() throws IOException { - instance = HttpClientBuilder.create().build(); - response = instance.execute(new HttpPost("http://t.co/I5YYd9tddw")); - assertThat(response.getStatusLine().getStatusCode(), equalTo(301)); + try (CloseableHttpClient httpClient = HttpClientBuilder.create() + .build()) { + httpClient.execute(request, response -> { + assertThat(response.getCode(), equalTo(200)); + return response; + }); + } } @Test - public final void givenRedirectingPOSTViaPost4_2Api_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() throws IOException { - final CloseableHttpClient client = HttpClients.custom().setRedirectStrategy(new DefaultRedirectStrategy() { - /** Redirectable methods. */ - private final String[] REDIRECT_METHODS = new String[]{HttpGet.METHOD_NAME, HttpPost.METHOD_NAME, HttpHead.METHOD_NAME}; + void givenRedirectingPOST_whenUsingDefaultRedirectStrategy_thenRedirected() throws IOException { - @Override - protected boolean isRedirectable(final String method) { - return Arrays.stream(REDIRECT_METHODS) - .anyMatch(m -> m.equalsIgnoreCase(method)); - } - }).build(); + final HttpPost request = new HttpPost("http://t.co/I5YYd9tddw"); - response = client.execute(new HttpPost("http://t.co/I5YYd9tddw")); - assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); + try (CloseableHttpClient httpClient = HttpClientBuilder.create() + .setRedirectStrategy(new DefaultRedirectStrategy()) + .build()) { + httpClient.execute(request, response -> { + assertThat(response.getCode(), equalTo(200)); + return response; + }); + } } - - @Test - public final void givenRedirectingPOSTVia4_2Api_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() throws IOException { - final CloseableHttpClient client = HttpClients.custom().setRedirectStrategy(new LaxRedirectStrategy()).build(); - - response = client.execute(new HttpPost("http://t.co/I5YYd9tddw")); - assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); - } - - @Test - public final void givenRedirectingPOST_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() throws IOException { - instance = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build(); - response = instance.execute(new HttpPost("http://t.co/I5YYd9tddw")); - assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); - } - } diff --git a/apache-httpclient4/README.md b/apache-httpclient4/README.md index 22b0391e6b..0bc4ac8e83 100644 --- a/apache-httpclient4/README.md +++ b/apache-httpclient4/README.md @@ -11,6 +11,7 @@ This module contains articles about Apache HttpClient 4.5 - [Apache HttpClient vs. CloseableHttpClient](https://www.baeldung.com/apache-httpclient-vs-closeablehttpclient) - [Expand Shortened URLs with Apache HttpClient](https://www.baeldung.com/apache-httpclient-expand-url) - [Retrying Requests using Apache HttpClient](https://www.baeldung.com/java-retrying-requests-using-apache-httpclient) +- [Apache HttpClient – Follow Redirects for POST](https://www.baeldung.com/httpclient-redirect-on-http-post) ### Running the Tests To run the live tests, use the command: mvn clean install -Plive diff --git a/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientRedirectV4LiveTest.java b/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientRedirectV4LiveTest.java new file mode 100644 index 0000000000..f6d65a8d8f --- /dev/null +++ b/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientRedirectV4LiveTest.java @@ -0,0 +1,70 @@ +package com.baeldung.httpclient; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +import java.io.IOException; + +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.client.LaxRedirectStrategy; + +class HttpClientRedirectV4LiveTest { + private CloseableHttpClient instance; + + private CloseableHttpResponse response; + + @BeforeEach + public final void before() { + instance = HttpClientBuilder.create() + .build(); + } + + @AfterEach + public final void after() throws IllegalStateException, IOException { + ResponseUtil.closeResponse(response); + } + + @Test + void givenRedirectsAreDisabled_whenConsumingUrlWhichRedirects_thenNotRedirected() throws IOException { + instance = HttpClients.custom() + .disableRedirectHandling() + .build(); + + final HttpGet httpGet = new HttpGet("http://t.co/I5YYd9tddw"); + response = instance.execute(httpGet); + + assertThat(response.getStatusLine() + .getStatusCode(), equalTo(301)); + } + + // redirect with POST + + @Test + void givenPostRequest_whenConsumingUrlWhichRedirects_thenNotRedirected() throws IOException { + instance = HttpClientBuilder.create() + .build(); + response = instance.execute(new HttpPost("http://t.co/I5YYd9tddw")); + assertThat(response.getStatusLine() + .getStatusCode(), equalTo(301)); + } + + @Test + void givenRedirectingPOST_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() throws IOException { + instance = HttpClientBuilder.create() + .setRedirectStrategy(new LaxRedirectStrategy()) + .build(); + response = instance.execute(new HttpPost("http://t.co/I5YYd9tddw")); + assertThat(response.getStatusLine() + .getStatusCode(), equalTo(200)); + } + +} \ No newline at end of file