Java 15022 (#14174)

* [JAVA-15022] Clean up

* [JAVA-15022] Clean up
This commit is contained in:
panos-kakos 2023-06-06 08:27:42 +03:00 committed by GitHub
parent 0e4baeda6a
commit ccec4cbf91
4 changed files with 93 additions and 30 deletions

View File

@ -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;

View File

@ -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<SimpleHttpResponse> 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<SimpleHttpResponse> future = client.execute(request, null);
final HttpResponse response = future.get();
assertThat(response.getCode(), equalTo(200));
client.close();

View File

@ -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();
}
}
}

View File

@ -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);