diff --git a/apache-httpclient4/pom.xml b/apache-httpclient4/pom.xml
index e0bf9dd5f6..21c675db35 100644
--- a/apache-httpclient4/pom.xml
+++ b/apache-httpclient4/pom.xml
@@ -199,33 +199,7 @@
true
-
-
- org.apache.maven.plugins
- maven-war-plugin
- ${maven-war-plugin.version}
-
-
- org.codehaus.cargo
- cargo-maven2-plugin
- ${cargo-maven2-plugin.version}
-
- true
-
- jetty8x
- embedded
-
-
-
-
-
-
- 8082
-
-
-
-
-
+
@@ -233,26 +207,6 @@
live
-
- org.codehaus.cargo
- cargo-maven2-plugin
-
-
- start-server
- pre-integration-test
-
- start
-
-
-
- stop-server
- post-integration-test
-
- stop
-
-
-
-
org.apache.maven.plugins
maven-surefire-plugin
@@ -269,9 +223,6 @@
**/*LiveTest.java
-
- cargo
-
@@ -291,7 +242,6 @@
4.5.14
5.11.2
- 1.6.1
3.3.2
diff --git a/apache-httpclient4/src/test/java/com/baeldung/GetRequestMockServer.java b/apache-httpclient4/src/test/java/com/baeldung/GetRequestMockServer.java
new file mode 100644
index 0000000000..52f1baa30d
--- /dev/null
+++ b/apache-httpclient4/src/test/java/com/baeldung/GetRequestMockServer.java
@@ -0,0 +1,56 @@
+package com.baeldung;
+
+import static org.mockserver.integration.ClientAndServer.startClientAndServer;
+import static org.mockserver.model.HttpRequest.request;
+import static org.mockserver.model.HttpResponse.response;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+
+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;
+
+public class GetRequestMockServer {
+
+ public static ClientAndServer mockServer;
+ public static int serverPort;
+ public static String simplePathUrl;
+ public static final String SERVER_ADDRESS = "127.0.0.1";
+ public static final String SIMPLE_PATH = "/httpclient-simple/api/bars/1";
+
+ @BeforeAll
+ static void startServer() throws IOException {
+ serverPort = getFreePort();
+ System.out.println("Free port " + serverPort);
+ mockServer = startClientAndServer(serverPort);
+
+ simplePathUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + SIMPLE_PATH;
+
+ mockGetRequest();
+ }
+
+ @AfterAll
+ static void stopServer() {
+ mockServer.stop();
+ }
+
+ private static void mockGetRequest() {
+
+ MockServerClient client = new MockServerClient(SERVER_ADDRESS, serverPort);
+
+ client.when(request().withPath(SIMPLE_PATH)
+ .withMethod("GET"))
+ .respond(response().withStatusCode(HttpStatus.SC_OK)
+ .withBody("{\"status\":\"ok\"}"));
+ }
+
+ private static int getFreePort() throws IOException {
+ try (ServerSocket serverSocket = new ServerSocket(0)) {
+ return serverSocket.getLocalPort();
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/apache-httpclient4/src/test/java/com/baeldung/client/ClientLiveTest.java b/apache-httpclient4/src/test/java/com/baeldung/client/ClientLiveTest.java
index 2785bc5d08..9d1052b7b3 100644
--- a/apache-httpclient4/src/test/java/com/baeldung/client/ClientLiveTest.java
+++ b/apache-httpclient4/src/test/java/com/baeldung/client/ClientLiveTest.java
@@ -10,7 +10,7 @@ import java.io.IOException;
import java.security.GeneralSecurityException;
import javax.net.ssl.SSLContext;
-import javax.net.ssl.SSLPeerUnverifiedException;
+import javax.net.ssl.SSLHandshakeException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
@@ -31,10 +31,10 @@ import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
+import com.baeldung.GetRequestMockServer;
-class ClientLiveTest {
+class ClientLiveTest extends GetRequestMockServer {
- final String urlOverHttps = "http://localhost:8082/httpclient-simple/api/bars/1";
@Test
void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk_2() throws GeneralSecurityException {
@@ -54,13 +54,13 @@ class ClientLiveTest {
.build();
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
- final ResponseEntity response = new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class);
+ final ResponseEntity response = new RestTemplate(requestFactory).exchange(simplePathUrl, HttpMethod.GET, null, String.class);
assertThat(response.getStatusCode().value(), equalTo(200));
}
@Test
void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenCorrect() throws IOException {
- final HttpGet getMethod = new HttpGet(urlOverHttps);
+ final HttpGet getMethod = new HttpGet(simplePathUrl);
try (final CloseableHttpClient httpClient = HttpClients.custom()
.setSSLHostnameVerifier(new NoopHostnameVerifier())
@@ -80,20 +80,22 @@ class ClientLiveTest {
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
- final ResponseEntity response = new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class);
+ final ResponseEntity response = new RestTemplate(requestFactory).exchange(simplePathUrl, HttpMethod.GET, null, String.class);
assertThat(response.getStatusCode().value(), equalTo(200));
}
@Test
void whenHttpsUrlIsConsumed_thenException() {
- String urlOverHttps = "https://localhost:8082/httpclient-simple";
+ String urlOverHttps = "https://localhost:"+serverPort+"/httpclient-simple/api/bars/1";
HttpGet getMethod = new HttpGet(urlOverHttps);
- assertThrows(SSLPeerUnverifiedException.class, () -> {
+ assertThrows(SSLHandshakeException.class, () -> {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(getMethod);
assertThat(response.getStatusLine()
.getStatusCode(), equalTo(200));
});
}
+
+
}
\ No newline at end of file
diff --git a/apache-httpclient4/src/test/java/com/baeldung/client/RestClientV4LiveManualTest.java b/apache-httpclient4/src/test/java/com/baeldung/client/RestClientV4LiveManualTest.java
index c336e6a068..3c0f5b7c63 100644
--- a/apache-httpclient4/src/test/java/com/baeldung/client/RestClientV4LiveManualTest.java
+++ b/apache-httpclient4/src/test/java/com/baeldung/client/RestClientV4LiveManualTest.java
@@ -1,6 +1,5 @@
package com.baeldung.client;
-import static org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
@@ -89,4 +88,5 @@ public class RestClientV4LiveManualTest {
HttpResponse response = httpClient.execute(getMethod);
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
+
}
diff --git a/apache-httpclient4/src/test/java/com/baeldung/httpclient/GetRequestMockServer.java b/apache-httpclient4/src/test/java/com/baeldung/httpclient/GetRequestMockServer.java
deleted file mode 100644
index ae432e68f0..0000000000
--- a/apache-httpclient4/src/test/java/com/baeldung/httpclient/GetRequestMockServer.java
+++ /dev/null
@@ -1,77 +0,0 @@
-package com.baeldung.httpclient;
-
-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;
-
-import java.io.IOException;
-import java.net.ServerSocket;
-import java.net.URISyntaxException;
-
-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;
-
-public class GetRequestMockServer {
-
- public static ClientAndServer mockServer;
- public static String serviceOneUrl;
- public static String serviceTwoUrl;
-
- public 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-httpclient4/src/test/java/com/baeldung/httpclient/HttpAsyncClientV4LiveTest.java b/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpAsyncClientV4LiveTest.java
index e097f9f511..80b16d7f07 100644
--- a/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpAsyncClientV4LiveTest.java
+++ b/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpAsyncClientV4LiveTest.java
@@ -31,6 +31,7 @@ import org.apache.http.protocol.HttpContext;
import org.apache.http.ssl.SSLContexts;
import org.junit.jupiter.api.Test;
+import com.baeldung.GetRequestMockServer;
class HttpAsyncClientV4LiveTest extends GetRequestMockServer {
diff --git a/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientCancelRequestV4LiveTest.java b/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientCancelRequestV4LiveTest.java
index 226a7b8cf7..446c47c200 100644
--- a/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientCancelRequestV4LiveTest.java
+++ b/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientCancelRequestV4LiveTest.java
@@ -44,7 +44,9 @@ public class HttpClientCancelRequestV4LiveTest {
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
+ entity.getContent().close();
}
+
System.out.println("----------------------------------------");
// Do not feel like reading the response body
diff --git a/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientTimeoutV4LiveTest.java b/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientTimeoutV4LiveTest.java
index 4d4dd7be15..ed22913ddd 100644
--- a/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientTimeoutV4LiveTest.java
+++ b/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientTimeoutV4LiveTest.java
@@ -21,7 +21,9 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
-class HttpClientTimeoutV4LiveTest {
+import com.baeldung.GetRequestMockServer;
+
+class HttpClientTimeoutV4LiveTest extends GetRequestMockServer {
private CloseableHttpResponse response;
@@ -97,7 +99,7 @@ class HttpClientTimeoutV4LiveTest {
int timeout = 20; // seconds
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout * 1000)
.setConnectTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build();
- HttpGet getMethod = new HttpGet("http://localhost:8082/httpclient-simple/api/bars/1");
+ HttpGet getMethod = new HttpGet(simplePathUrl);
getMethod.setConfig(requestConfig);
int hardTimeout = 5; // seconds
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
index 0d394b4ce7..9a7a734b65 100644
--- a/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/ApacheHttpClientUnitTest.java
+++ b/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/ApacheHttpClientUnitTest.java
@@ -13,13 +13,15 @@ import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;
+import com.baeldung.GetRequestMockServer;
+
class ApacheHttpClientUnitTest extends GetRequestMockServer {
@Test
void givenDeveloperUsedCloseableHttpResponse_whenExecutingGetRequest_thenStatusIsOk() throws IOException {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
- HttpGet httpGet = new HttpGet(serviceOneUrl);
+ HttpGet httpGet = new HttpGet(simplePathUrl);
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
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
deleted file mode 100644
index 3473117cef..0000000000
--- a/apache-httpclient4/src/test/java/com/baeldung/httpclient/httpclient/GetRequestMockServer.java
+++ /dev/null
@@ -1,78 +0,0 @@
-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();
- }
- }
-
-}