From 1cd7f8f2d085aa64ea504b4304d9d5c99ec060c8 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Fri, 21 Jun 2024 11:06:57 +0200 Subject: [PATCH] Moved test cases that require an embedded test server to the integration test module --- .../TestFutureRequestExecutionService.java | 5 +- .../TestHttpClientBuilderInterceptors.java | 53 +++++++------------ .../extension/StandardTestClientBuilder.java | 13 +++++ .../sync/extension/TestClientBuilder.java | 9 ++++ 4 files changed, 44 insertions(+), 36 deletions(-) rename {httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic => httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync}/TestFutureRequestExecutionService.java (96%) rename {httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic => httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync}/TestHttpClientBuilderInterceptors.java (64%) diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestFutureRequestExecutionService.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestFutureRequestExecutionService.java similarity index 96% rename from httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestFutureRequestExecutionService.java rename to httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestFutureRequestExecutionService.java index 4b2137486..f7fd40617 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestFutureRequestExecutionService.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestFutureRequestExecutionService.java @@ -24,7 +24,7 @@ * . * */ -package org.apache.hc.client5.http.impl.classic; +package org.apache.hc.client5.testing.sync; import static org.hamcrest.MatcherAssert.assertThat; @@ -43,6 +43,9 @@ import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; 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.FutureRequestExecutionService; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; import org.apache.hc.client5.http.io.HttpClientConnectionManager; import org.apache.hc.client5.http.protocol.HttpClientContext; diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestHttpClientBuilderInterceptors.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestHttpClientBuilderInterceptors.java similarity index 64% rename from httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestHttpClientBuilderInterceptors.java rename to httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestHttpClientBuilderInterceptors.java index 49fcecf21..1fda23f7a 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestHttpClientBuilderInterceptors.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestHttpClientBuilderInterceptors.java @@ -24,68 +24,51 @@ * . * */ -package org.apache.hc.client5.http.impl.classic; - -import java.io.IOException; +package org.apache.hc.client5.testing.sync; import org.apache.hc.client5.http.classic.methods.HttpPost; -import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; -import org.apache.hc.client5.http.io.HttpClientConnectionManager; +import org.apache.hc.client5.testing.sync.extension.ClientProtocolLevel; +import org.apache.hc.client5.testing.sync.extension.TestClient; import org.apache.hc.core5.http.ClassicHttpRequest; import org.apache.hc.core5.http.Header; -import org.apache.hc.core5.http.HttpException; +import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.HttpResponse; -import org.apache.hc.core5.http.impl.bootstrap.HttpServer; -import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap; +import org.apache.hc.core5.http.URIScheme; import org.apache.hc.core5.http.io.entity.EntityUtils; -import org.apache.hc.core5.io.CloseMode; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @SuppressWarnings("boxing") // test code -public class TestHttpClientBuilderInterceptors { +public class TestHttpClientBuilderInterceptors extends AbstractIntegrationTestBase { - private HttpServer localServer; - private String uri; - private CloseableHttpClient httpClient; + public TestHttpClientBuilderInterceptors() { + super(URIScheme.HTTP, ClientProtocolLevel.STANDARD); + } @BeforeEach public void before() throws Exception { - this.localServer = ServerBootstrap.bootstrap() + configureServer(bootstrap -> bootstrap .register("/test", (request, response, context) -> { final Header testInterceptorHeader = request.getHeader("X-Test-Interceptor"); if (testInterceptorHeader != null) { response.setHeader(testInterceptorHeader); } response.setCode(200); - }).create(); - - this.localServer.start(); - uri = "http://localhost:" + this.localServer.getLocalPort() + "/test"; - final HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create() - .setMaxConnPerRoute(5) - .build(); - httpClient = HttpClientBuilder.create() - .setConnectionManager(cm) + })); + configureClient(builder -> builder .addExecInterceptorLast("test-interceptor", (request, scope, chain) -> { request.setHeader("X-Test-Interceptor", "active"); return chain.proceed(request, scope); - }) - .build(); - } - - @AfterEach - public void after() throws Exception { - this.httpClient.close(CloseMode.IMMEDIATE); - this.localServer.stop(); + })); } @Test - public void testAddExecInterceptorLastShouldBeExecuted() throws IOException, HttpException { - final ClassicHttpRequest request = new HttpPost(uri); - final HttpResponse response = httpClient.execute(request, httpResponse -> { + public void testAddExecInterceptorLastShouldBeExecuted() throws Exception { + final HttpHost httpHost = startServer(); + final TestClient client = client(); + final ClassicHttpRequest request = new HttpPost("/test"); + final HttpResponse response = client.execute(httpHost, request, httpResponse -> { EntityUtils.consume(httpResponse.getEntity()); return httpResponse; }); diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/extension/StandardTestClientBuilder.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/extension/StandardTestClientBuilder.java index 378737858..aac369e70 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/extension/StandardTestClientBuilder.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/extension/StandardTestClientBuilder.java @@ -33,6 +33,7 @@ import org.apache.hc.client5.http.AuthenticationStrategy; import org.apache.hc.client5.http.HttpRequestRetryStrategy; import org.apache.hc.client5.http.UserTokenHandler; import org.apache.hc.client5.http.auth.AuthSchemeFactory; +import org.apache.hc.client5.http.classic.ExecChainHandler; import org.apache.hc.client5.http.config.ConnectionConfig; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; @@ -137,6 +138,18 @@ final class StandardTestClientBuilder implements TestClientBuilder { return this; } + @Override + public TestClientBuilder addExecInterceptorFirst(final String name, final ExecChainHandler interceptor) { + this.clientBuilder.addExecInterceptorFirst(name, interceptor); + return this; + } + + @Override + public TestClientBuilder addExecInterceptorLast(final String name, final ExecChainHandler interceptor) { + this.clientBuilder.addExecInterceptorLast(name, interceptor); + return this; + } + @Override public TestClient build() throws Exception { final HttpClientConnectionManager connectionManagerCopy = connectionManager != null ? connectionManager : diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/extension/TestClientBuilder.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/extension/TestClientBuilder.java index 27a0381eb..7dec1ff92 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/extension/TestClientBuilder.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/extension/TestClientBuilder.java @@ -33,6 +33,7 @@ import org.apache.hc.client5.http.AuthenticationStrategy; import org.apache.hc.client5.http.HttpRequestRetryStrategy; import org.apache.hc.client5.http.UserTokenHandler; import org.apache.hc.client5.http.auth.AuthSchemeFactory; +import org.apache.hc.client5.http.classic.ExecChainHandler; import org.apache.hc.client5.http.io.HttpClientConnectionManager; import org.apache.hc.core5.http.Header; import org.apache.hc.core5.http.HttpRequestInterceptor; @@ -89,6 +90,14 @@ public interface TestClientBuilder { throw new UnsupportedOperationException("Operation not supported by " + getProtocolLevel()); } + default TestClientBuilder addExecInterceptorFirst(String name, ExecChainHandler interceptor) { + throw new UnsupportedOperationException("Operation not supported by " + getProtocolLevel()); + } + + default TestClientBuilder addExecInterceptorLast(String name, ExecChainHandler interceptor) { + throw new UnsupportedOperationException("Operation not supported by " + getProtocolLevel()); + } + TestClient build() throws Exception; }