[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:
@@ -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
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user