Moved test cases that require an embedded test server to the integration test module

This commit is contained in:
Oleg Kalnichevski 2024-06-21 11:06:57 +02:00
parent 034cd65aa5
commit 1cd7f8f2d0
4 changed files with 44 additions and 36 deletions

View File

@ -24,7 +24,7 @@
* <http://www.apache.org/>. * <http://www.apache.org/>.
* *
*/ */
package org.apache.hc.client5.http.impl.classic; package org.apache.hc.client5.testing.sync;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
@ -43,6 +43,9 @@ import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.hc.client5.http.classic.methods.HttpGet; 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.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.io.HttpClientConnectionManager; import org.apache.hc.client5.http.io.HttpClientConnectionManager;
import org.apache.hc.client5.http.protocol.HttpClientContext; import org.apache.hc.client5.http.protocol.HttpClientContext;

View File

@ -24,68 +24,51 @@
* <http://www.apache.org/>. * <http://www.apache.org/>.
* *
*/ */
package org.apache.hc.client5.http.impl.classic; package org.apache.hc.client5.testing.sync;
import java.io.IOException;
import org.apache.hc.client5.http.classic.methods.HttpPost; import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; import org.apache.hc.client5.testing.sync.extension.ClientProtocolLevel;
import org.apache.hc.client5.http.io.HttpClientConnectionManager; import org.apache.hc.client5.testing.sync.extension.TestClient;
import org.apache.hc.core5.http.ClassicHttpRequest; import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.Header; 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.HttpResponse;
import org.apache.hc.core5.http.impl.bootstrap.HttpServer; import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
import org.apache.hc.core5.http.io.entity.EntityUtils; 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.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@SuppressWarnings("boxing") // test code @SuppressWarnings("boxing") // test code
public class TestHttpClientBuilderInterceptors { public class TestHttpClientBuilderInterceptors extends AbstractIntegrationTestBase {
private HttpServer localServer; public TestHttpClientBuilderInterceptors() {
private String uri; super(URIScheme.HTTP, ClientProtocolLevel.STANDARD);
private CloseableHttpClient httpClient; }
@BeforeEach @BeforeEach
public void before() throws Exception { public void before() throws Exception {
this.localServer = ServerBootstrap.bootstrap() configureServer(bootstrap -> bootstrap
.register("/test", (request, response, context) -> { .register("/test", (request, response, context) -> {
final Header testInterceptorHeader = request.getHeader("X-Test-Interceptor"); final Header testInterceptorHeader = request.getHeader("X-Test-Interceptor");
if (testInterceptorHeader != null) { if (testInterceptorHeader != null) {
response.setHeader(testInterceptorHeader); response.setHeader(testInterceptorHeader);
} }
response.setCode(200); response.setCode(200);
}).create(); }));
configureClient(builder -> builder
this.localServer.start();
uri = "http://localhost:" + this.localServer.getLocalPort() + "/test";
final HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create()
.setMaxConnPerRoute(5)
.build();
httpClient = HttpClientBuilder.create()
.setConnectionManager(cm)
.addExecInterceptorLast("test-interceptor", (request, scope, chain) -> { .addExecInterceptorLast("test-interceptor", (request, scope, chain) -> {
request.setHeader("X-Test-Interceptor", "active"); request.setHeader("X-Test-Interceptor", "active");
return chain.proceed(request, scope); return chain.proceed(request, scope);
}) }));
.build();
}
@AfterEach
public void after() throws Exception {
this.httpClient.close(CloseMode.IMMEDIATE);
this.localServer.stop();
} }
@Test @Test
public void testAddExecInterceptorLastShouldBeExecuted() throws IOException, HttpException { public void testAddExecInterceptorLastShouldBeExecuted() throws Exception {
final ClassicHttpRequest request = new HttpPost(uri); final HttpHost httpHost = startServer();
final HttpResponse response = httpClient.execute(request, httpResponse -> { final TestClient client = client();
final ClassicHttpRequest request = new HttpPost("/test");
final HttpResponse response = client.execute(httpHost, request, httpResponse -> {
EntityUtils.consume(httpResponse.getEntity()); EntityUtils.consume(httpResponse.getEntity());
return httpResponse; return httpResponse;
}); });

View File

@ -33,6 +33,7 @@ import org.apache.hc.client5.http.AuthenticationStrategy;
import org.apache.hc.client5.http.HttpRequestRetryStrategy; import org.apache.hc.client5.http.HttpRequestRetryStrategy;
import org.apache.hc.client5.http.UserTokenHandler; import org.apache.hc.client5.http.UserTokenHandler;
import org.apache.hc.client5.http.auth.AuthSchemeFactory; 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.config.ConnectionConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
@ -137,6 +138,18 @@ final class StandardTestClientBuilder implements TestClientBuilder {
return this; 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 @Override
public TestClient build() throws Exception { public TestClient build() throws Exception {
final HttpClientConnectionManager connectionManagerCopy = connectionManager != null ? connectionManager : final HttpClientConnectionManager connectionManagerCopy = connectionManager != null ? connectionManager :

View File

@ -33,6 +33,7 @@ import org.apache.hc.client5.http.AuthenticationStrategy;
import org.apache.hc.client5.http.HttpRequestRetryStrategy; import org.apache.hc.client5.http.HttpRequestRetryStrategy;
import org.apache.hc.client5.http.UserTokenHandler; import org.apache.hc.client5.http.UserTokenHandler;
import org.apache.hc.client5.http.auth.AuthSchemeFactory; 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.client5.http.io.HttpClientConnectionManager;
import org.apache.hc.core5.http.Header; import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpRequestInterceptor; import org.apache.hc.core5.http.HttpRequestInterceptor;
@ -89,6 +90,14 @@ public interface TestClientBuilder {
throw new UnsupportedOperationException("Operation not supported by " + getProtocolLevel()); 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; TestClient build() throws Exception;
} }