From ccec4cbf91a06c0b6ed4cc0a8fb88aacf4f676e6 Mon Sep 17 00:00:00 2001 From: panos-kakos <102670093+panos-kakos@users.noreply.github.com> Date: Tue, 6 Jun 2023 08:27:42 +0300 Subject: [PATCH] Java 15022 (#14174) * [JAVA-15022] Clean up * [JAVA-15022] Clean up --- .../httpclient/GetRequestMockServer.java | 7 +- .../httpclient/HttpAsyncClientLiveTest.java | 34 +++----- .../httpclient/GetRequestMockServer.java | 77 +++++++++++++++++++ .../httpclient/HttpAsyncClientV4LiveTest.java | 5 +- 4 files changed, 93 insertions(+), 30 deletions(-) create mode 100644 apache-httpclient4/src/test/java/com/baeldung/httpclient/GetRequestMockServer.java diff --git a/apache-httpclient/src/test/java/com/baeldung/httpclient/GetRequestMockServer.java b/apache-httpclient/src/test/java/com/baeldung/httpclient/GetRequestMockServer.java index f65558906f..988a89e7af 100644 --- a/apache-httpclient/src/test/java/com/baeldung/httpclient/GetRequestMockServer.java +++ b/apache-httpclient/src/test/java/com/baeldung/httpclient/GetRequestMockServer.java @@ -21,7 +21,7 @@ public class GetRequestMockServer { public static String serviceOneUrl; public static String serviceTwoUrl; - private static int serverPort; + public static int serverPort; public static final String SERVER_ADDRESS = "127.0.0.1"; public static final String PATH_ONE = "/test1"; @@ -29,9 +29,8 @@ public class GetRequestMockServer { public static final String METHOD = "GET"; @BeforeAll - static void startServer() throws IOException, URISyntaxException { - //serverPort = getFreePort(); - serverPort = 8080; + static void startServer() throws IOException { + serverPort = getFreePort(); System.out.println("Free port "+serverPort); serviceOneUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_ONE; serviceTwoUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_TWO; diff --git a/apache-httpclient/src/test/java/com/baeldung/httpclient/HttpAsyncClientLiveTest.java b/apache-httpclient/src/test/java/com/baeldung/httpclient/HttpAsyncClientLiveTest.java index f4b9266d7e..123c51bb86 100644 --- a/apache-httpclient/src/test/java/com/baeldung/httpclient/HttpAsyncClientLiveTest.java +++ b/apache-httpclient/src/test/java/com/baeldung/httpclient/HttpAsyncClientLiveTest.java @@ -9,6 +9,7 @@ import java.util.concurrent.Future; import javax.net.ssl.SSLContext; +import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner; import org.junit.jupiter.api.Test; import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; @@ -55,12 +56,12 @@ class HttpAsyncClientLiveTest extends GetRequestMockServer { @Test void whenUseHttpAsyncClient_thenCorrect() throws InterruptedException, ExecutionException, IOException { - final HttpHost target = new HttpHost(HOST); + final HttpHost target = new HttpHost(HOST_WITH_COOKIE); final SimpleHttpRequest request = SimpleRequestBuilder.get() .setHttpHost(target) .build(); - final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault(); + final CloseableHttpAsyncClient client = HttpAsyncClients.custom().build(); client.start(); @@ -102,30 +103,15 @@ class HttpAsyncClientLiveTest extends GetRequestMockServer { @Test void whenUseProxyWithHttpClient_thenCorrect() throws Exception { - final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault(); + final HttpHost proxy = new HttpHost("127.0.0.1", GetRequestMockServer.serverPort); + DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); + final CloseableHttpAsyncClient client = HttpAsyncClients.custom() + .setRoutePlanner(routePlanner) + .build(); client.start(); - final HttpHost proxy = new HttpHost("127.0.0.1", 8080); - final RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); + final SimpleHttpRequest request = new SimpleHttpRequest("GET" ,HOST_WITH_PROXY); - request.setConfig(config); - final Future future = client.execute(request, new FutureCallback<>(){ - @Override - public void completed(SimpleHttpResponse response) { - - System.out.println("responseData"); - } - - @Override - public void failed(Exception ex) { - System.out.println("Error executing HTTP request: " + ex.getMessage()); - } - - @Override - public void cancelled() { - System.out.println("HTTP request execution cancelled"); - } - }); - + final Future future = client.execute(request, null); final HttpResponse response = future.get(); assertThat(response.getCode(), equalTo(200)); client.close(); diff --git a/apache-httpclient4/src/test/java/com/baeldung/httpclient/GetRequestMockServer.java b/apache-httpclient4/src/test/java/com/baeldung/httpclient/GetRequestMockServer.java new file mode 100644 index 0000000000..ae432e68f0 --- /dev/null +++ b/apache-httpclient4/src/test/java/com/baeldung/httpclient/GetRequestMockServer.java @@ -0,0 +1,77 @@ +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 dc0055c5ae..e097f9f511 100644 --- a/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpAsyncClientV4LiveTest.java +++ b/apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpAsyncClientV4LiveTest.java @@ -31,7 +31,8 @@ import org.apache.http.protocol.HttpContext; import org.apache.http.ssl.SSLContexts; import org.junit.jupiter.api.Test; -class HttpAsyncClientV4LiveTest { + +class HttpAsyncClientV4LiveTest extends GetRequestMockServer { private static final String HOST = "http://www.google.com"; private static final String HOST_WITH_SSL = "https://mms.nw.ru/"; @@ -87,7 +88,7 @@ class HttpAsyncClientV4LiveTest { void whenUseProxyWithHttpClient_thenCorrect() throws Exception { final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault(); client.start(); - final HttpHost proxy = new HttpHost("127.0.0.1", 8080); + final HttpHost proxy = new HttpHost("127.0.0.1", GetRequestMockServer.serverPort); final RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); final HttpGet request = new HttpGet(HOST_WITH_PROXY); request.setConfig(config);