JAVA-15014 Upgrade HttpClient 5 (#13479)
* JAVA-15014 Upgrade HttpClient 5 * JAVA-15014 Minor improvements * JAVA-15014 Moved httpclient4 code from apache-httpclient-2 to apache-httpclient4
This commit is contained in:
committed by
GitHub
parent
d204e587f5
commit
6ae24f4064
@@ -21,17 +21,6 @@
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<!-- http client -->
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${httpclient.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents.client5</groupId>
|
||||
<artifactId>httpclient5</artifactId>
|
||||
@@ -97,8 +86,7 @@
|
||||
<properties>
|
||||
<assertj.version>3.22.0</assertj.version>
|
||||
<mockserver.version>5.11.2</mockserver.version>
|
||||
<httpclient.version>4.5.8</httpclient.version>
|
||||
<httpclient5.version>5.2</httpclient5.version>
|
||||
<httpclient5.version>5.2.1</httpclient5.version>
|
||||
<maven.compiler.source.version>11</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>11</maven.compiler.target.version>
|
||||
<spring-boot.version>2.1.7.RELEASE</spring-boot.version>
|
||||
|
||||
@@ -1,37 +1,40 @@
|
||||
package com.baeldung.httpclient.httpclient;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.hc.client5.http.classic.HttpClient;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
|
||||
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
|
||||
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
import org.apache.hc.core5.http.HttpResponse;
|
||||
import org.apache.hc.core5.http.HttpStatus;
|
||||
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class ApacheHttpClientUnitTest extends GetRequestMockServer {
|
||||
|
||||
@Test
|
||||
void givenDeveloperUsedHttpClient_whenExecutingGetRequest_thenStatusIsOkButSonarReportsAnIssue() throws IOException {
|
||||
HttpClient httpClient = HttpClients.createDefault();
|
||||
HttpGet httpGet = new HttpGet(serviceOneUrl);
|
||||
HttpResponse response = httpClient.execute(httpGet);
|
||||
assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK);
|
||||
httpClient.execute(httpGet, response -> {
|
||||
assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK);
|
||||
return response;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDeveloperUsedCloseableHttpClient_whenExecutingGetRequest_thenStatusIsOk() throws IOException {
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
HttpGet httpGet = new HttpGet(serviceOneUrl);
|
||||
HttpResponse response = httpClient.execute(httpGet);
|
||||
assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK);
|
||||
httpClient.execute(httpGet, response -> {
|
||||
assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK);
|
||||
return response;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,20 +42,10 @@ class ApacheHttpClientUnitTest extends GetRequestMockServer {
|
||||
void givenDeveloperUsedHttpClientBuilder_whenExecutingGetRequest_thenStatusIsOk() throws IOException {
|
||||
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
|
||||
HttpGet httpGet = new HttpGet(serviceOneUrl);
|
||||
HttpResponse response = httpClient.execute(httpGet);
|
||||
assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDeveloperUsedCloseableHttpResponse_whenExecutingGetRequest_thenStatusIsOk() throws IOException {
|
||||
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
|
||||
HttpGet httpGet = new HttpGet(serviceOneUrl);
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
||||
HttpEntity entity = response.getEntity();
|
||||
EntityUtils.consume(entity);
|
||||
httpClient.execute(httpGet, response -> {
|
||||
assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK);
|
||||
}
|
||||
return response;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,18 +53,20 @@ class ApacheHttpClientUnitTest extends GetRequestMockServer {
|
||||
void givenDeveloperUsedSingleClient_whenExecutingTwoGetRequest_thenStatusIsOk() throws IOException {
|
||||
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
|
||||
HttpGet httpGetOne = new HttpGet(serviceOneUrl);
|
||||
try (CloseableHttpResponse responseOne = httpClient.execute(httpGetOne)) {
|
||||
httpClient.execute(httpGetOne, responseOne -> {
|
||||
HttpEntity entityOne = responseOne.getEntity();
|
||||
EntityUtils.consume(entityOne);
|
||||
assertThat(responseOne.getCode()).isEqualTo(HttpStatus.SC_OK);
|
||||
}
|
||||
return responseOne;
|
||||
});
|
||||
|
||||
HttpGet httpGetTwo = new HttpGet(serviceTwoUrl);
|
||||
try (CloseableHttpResponse responseTwo = httpClient.execute(httpGetTwo)) {
|
||||
HttpEntity entityTwo = responseTwo.getEntity();
|
||||
httpClient.execute(httpGetTwo, responseTwo -> {
|
||||
HttpEntity entityTwo = httpGetTwo.getEntity();
|
||||
EntityUtils.consume(entityTwo);
|
||||
assertThat(responseTwo.getCode()).isEqualTo(HttpStatus.SC_OK);
|
||||
}
|
||||
return responseTwo;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
package com.baeldung.httpclient.readresponsebodystring;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ApacheHttpClientUnitTest {
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
public static final String DUMMY_URL = "https://postman-echo.com/get";
|
||||
|
||||
@Test
|
||||
public void whenUseApacheHttpClient_thenCorrect() throws IOException {
|
||||
HttpGet request = new HttpGet(DUMMY_URL);
|
||||
|
||||
try (CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = client.execute(request)) {
|
||||
HttpEntity entity = response.getEntity();
|
||||
logger.debug("Response -> {}", EntityUtils.toString(entity));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user