From 6ae24f40640563e41ca03cfd98871c1b48c5c736 Mon Sep 17 00:00:00 2001
From: Anastasios Ioannidis
<121166333+anastasiosioannidis@users.noreply.github.com>
Date: Mon, 6 Mar 2023 20:06:59 +0200
Subject: [PATCH] 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
---
apache-httpclient-2/pom.xml | 14 +---
.../httpclient/ApacheHttpClientUnitTest.java | 51 ++++++------
apache-httpclient4/pom.xml | 6 ++
.../httpclient/ApacheHttpClientUnitTest.java | 31 ++++++++
.../httpclient/GetRequestMockServer.java | 78 +++++++++++++++++++
.../ApacheHttpClientUnitTest.java | 2 +-
6 files changed, 140 insertions(+), 42 deletions(-)
create mode 100644 apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/ApacheHttpClientUnitTest.java
create mode 100644 apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/GetRequestMockServer.java
rename {apache-httpclient-2 => apache-httpclient4}/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClientUnitTest.java (99%)
diff --git a/apache-httpclient-2/pom.xml b/apache-httpclient-2/pom.xml
index 91c4c718c0..8b7217dbe1 100644
--- a/apache-httpclient-2/pom.xml
+++ b/apache-httpclient-2/pom.xml
@@ -21,17 +21,6 @@
${commons-lang3.version}
-
- org.apache.httpcomponents
- httpclient
- ${httpclient.version}
-
-
- commons-logging
- commons-logging
-
-
-
org.apache.httpcomponents.client5
httpclient5
@@ -97,8 +86,7 @@
3.22.0
5.11.2
- 4.5.8
- 5.2
+ 5.2.1
11
11
2.1.7.RELEASE
diff --git a/apache-httpclient-2/src/test/java/com/baeldung/httpclient/httpclient/ApacheHttpClientUnitTest.java b/apache-httpclient-2/src/test/java/com/baeldung/httpclient/httpclient/ApacheHttpClientUnitTest.java
index 0d45eedc12..07bd356d62 100644
--- a/apache-httpclient-2/src/test/java/com/baeldung/httpclient/httpclient/ApacheHttpClientUnitTest.java
+++ b/apache-httpclient-2/src/test/java/com/baeldung/httpclient/httpclient/ApacheHttpClientUnitTest.java
@@ -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;
+ });
}
}
diff --git a/apache-httpclient4/pom.xml b/apache-httpclient4/pom.xml
index 6b449e3be1..140dc0fbdb 100644
--- a/apache-httpclient4/pom.xml
+++ b/apache-httpclient4/pom.xml
@@ -152,6 +152,11 @@
+
+ org.mock-server
+ mockserver-netty
+ ${mockserver.version}
+
com.github.tomakehurst
wiremock
@@ -284,6 +289,7 @@
2.5.1
4.4.16
4.5.14
+ 5.11.2
1.6.1
diff --git a/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/ApacheHttpClientUnitTest.java b/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/ApacheHttpClientUnitTest.java
new file mode 100644
index 0000000000..0d394b4ce7
--- /dev/null
+++ b/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/ApacheHttpClientUnitTest.java
@@ -0,0 +1,31 @@
+package com.baeldung.httpclient.httpclient;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpStatus;
+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.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+import org.junit.jupiter.api.Test;
+
+class ApacheHttpClientUnitTest extends GetRequestMockServer {
+
+
+ @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);
+ assertThat(response.getStatusLine().getStatusCode()).isEqualTo(HttpStatus.SC_OK);
+ }
+ }
+ }
+
+}
diff --git a/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/GetRequestMockServer.java b/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/GetRequestMockServer.java
new file mode 100644
index 0000000000..3473117cef
--- /dev/null
+++ b/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/GetRequestMockServer.java
@@ -0,0 +1,78 @@
+package com.baeldung.httpclient.httpclient;
+
+import org.apache.http.HttpStatus;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.mockserver.client.MockServerClient;
+import org.mockserver.integration.ClientAndServer;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.net.URISyntaxException;
+
+import static org.mockserver.integration.ClientAndServer.startClientAndServer;
+import static org.mockserver.matchers.Times.exactly;
+import static org.mockserver.model.HttpRequest.request;
+import static org.mockserver.model.HttpResponse.response;
+
+public class GetRequestMockServer {
+
+ public static ClientAndServer mockServer;
+ public static String serviceOneUrl;
+ public static String serviceTwoUrl;
+
+ private static int serverPort;
+
+ public static final String SERVER_ADDRESS = "127.0.0.1";
+ public static final String PATH_ONE = "/test1";
+ public static final String PATH_TWO = "/test2";
+ public static final String METHOD = "GET";
+
+ @BeforeAll
+ static void startServer() throws IOException, URISyntaxException {
+ serverPort = getFreePort();
+ serviceOneUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_ONE;
+ serviceTwoUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_TWO;
+ mockServer = startClientAndServer(serverPort);
+ mockGetRequest();
+ }
+
+ @AfterAll
+ static void stopServer() {
+ mockServer.stop();
+ }
+
+ private static void mockGetRequest() {
+ new MockServerClient(SERVER_ADDRESS, serverPort)
+ .when(
+ request()
+ .withPath(PATH_ONE)
+ .withMethod(METHOD),
+ exactly(5)
+ )
+ .respond(
+ response()
+ .withStatusCode(HttpStatus.SC_OK)
+ .withBody("{\"status\":\"ok\"}")
+ );
+ new MockServerClient(SERVER_ADDRESS, serverPort)
+ .when(
+ request()
+ .withPath(PATH_TWO)
+ .withMethod(METHOD),
+ exactly(1)
+ )
+ .respond(
+ response()
+ .withStatusCode(HttpStatus.SC_OK)
+ .withBody("{\"status\":\"ok\"}")
+ );
+ }
+
+ private static int getFreePort () throws IOException {
+ try (ServerSocket serverSocket = new ServerSocket(0)) {
+ return serverSocket.getLocalPort();
+ }
+ }
+
+}
diff --git a/apache-httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClientUnitTest.java b/apache-httpclient4/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClientUnitTest.java
similarity index 99%
rename from apache-httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClientUnitTest.java
rename to apache-httpclient4/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClientUnitTest.java
index 7625b415f2..4d88211d0d 100644
--- a/apache-httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClientUnitTest.java
+++ b/apache-httpclient4/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClientUnitTest.java
@@ -25,4 +25,4 @@ public class ApacheHttpClientUnitTest {
logger.debug("Response -> {}", EntityUtils.toString(entity));
}
}
-}
+}
\ No newline at end of file