[JAVA-15027]Upgraded follow redirects for apache http client + moved … (#14092)

* [JAVA-15027]Upgraded follow redirects for apache http client + moved older version to apache-httpclient4 module

* [JAVA-15027] Added test case
This commit is contained in:
panos-kakos 2023-05-22 19:17:58 +03:00 committed by GitHub
parent 1cee2277c6
commit 4751f16080
3 changed files with 111 additions and 77 deletions

View File

@ -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));
}
}

View File

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

View File

@ -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));
}
}