parent
0e4baeda6a
commit
ccec4cbf91
@ -21,7 +21,7 @@ public class GetRequestMockServer {
|
|||||||
public static String serviceOneUrl;
|
public static String serviceOneUrl;
|
||||||
public static String serviceTwoUrl;
|
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 SERVER_ADDRESS = "127.0.0.1";
|
||||||
public static final String PATH_ONE = "/test1";
|
public static final String PATH_ONE = "/test1";
|
||||||
@ -29,9 +29,8 @@ public class GetRequestMockServer {
|
|||||||
public static final String METHOD = "GET";
|
public static final String METHOD = "GET";
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
static void startServer() throws IOException, URISyntaxException {
|
static void startServer() throws IOException {
|
||||||
//serverPort = getFreePort();
|
serverPort = getFreePort();
|
||||||
serverPort = 8080;
|
|
||||||
System.out.println("Free port "+serverPort);
|
System.out.println("Free port "+serverPort);
|
||||||
serviceOneUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_ONE;
|
serviceOneUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_ONE;
|
||||||
serviceTwoUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_TWO;
|
serviceTwoUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_TWO;
|
||||||
|
@ -9,6 +9,7 @@ import java.util.concurrent.Future;
|
|||||||
|
|
||||||
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.SSLContext;
|
||||||
|
|
||||||
|
import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
|
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
|
||||||
@ -55,12 +56,12 @@ class HttpAsyncClientLiveTest extends GetRequestMockServer {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whenUseHttpAsyncClient_thenCorrect() throws InterruptedException, ExecutionException, IOException {
|
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()
|
final SimpleHttpRequest request = SimpleRequestBuilder.get()
|
||||||
.setHttpHost(target)
|
.setHttpHost(target)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
|
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().build();
|
||||||
client.start();
|
client.start();
|
||||||
|
|
||||||
|
|
||||||
@ -102,30 +103,15 @@ class HttpAsyncClientLiveTest extends GetRequestMockServer {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whenUseProxyWithHttpClient_thenCorrect() throws Exception {
|
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();
|
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);
|
final SimpleHttpRequest request = new SimpleHttpRequest("GET" ,HOST_WITH_PROXY);
|
||||||
request.setConfig(config);
|
final Future<SimpleHttpResponse> future = client.execute(request, null);
|
||||||
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 HttpResponse response = future.get();
|
final HttpResponse response = future.get();
|
||||||
assertThat(response.getCode(), equalTo(200));
|
assertThat(response.getCode(), equalTo(200));
|
||||||
client.close();
|
client.close();
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -31,7 +31,8 @@ import org.apache.http.protocol.HttpContext;
|
|||||||
import org.apache.http.ssl.SSLContexts;
|
import org.apache.http.ssl.SSLContexts;
|
||||||
import org.junit.jupiter.api.Test;
|
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 = "http://www.google.com";
|
||||||
private static final String HOST_WITH_SSL = "https://mms.nw.ru/";
|
private static final String HOST_WITH_SSL = "https://mms.nw.ru/";
|
||||||
@ -87,7 +88,7 @@ class HttpAsyncClientV4LiveTest {
|
|||||||
void whenUseProxyWithHttpClient_thenCorrect() throws Exception {
|
void whenUseProxyWithHttpClient_thenCorrect() throws Exception {
|
||||||
final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
|
final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
|
||||||
client.start();
|
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 RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
|
||||||
final HttpGet request = new HttpGet(HOST_WITH_PROXY);
|
final HttpGet request = new HttpGet(HOST_WITH_PROXY);
|
||||||
request.setConfig(config);
|
request.setConfig(config);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user