From 29bf4bde45d686407c36b427c27f76b0519d5bc3 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Wed, 16 Oct 2024 13:44:45 +0200 Subject: [PATCH] Re-enabled async client authentication tests --- ...ractHttpAsyncClientAuthenticationTest.java | 48 ++++++++---- .../testing/async/HttpIntegrationTests.java | 78 +++++++++---------- .../async/TestHttp1ClientAuthentication.java | 2 +- 3 files changed, 72 insertions(+), 56 deletions(-) diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttpAsyncClientAuthenticationTest.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttpAsyncClientAuthenticationTest.java index 2bcb6709e..76f455f64 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttpAsyncClientAuthenticationTest.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttpAsyncClientAuthenticationTest.java @@ -36,6 +36,7 @@ import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Consumer; import java.util.stream.Collectors; import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; @@ -60,6 +61,7 @@ import org.apache.hc.client5.testing.auth.BearerAuthenticationHandler; import org.apache.hc.client5.testing.extension.async.ClientProtocolLevel; import org.apache.hc.client5.testing.extension.async.ServerProtocolLevel; import org.apache.hc.client5.testing.extension.async.TestAsyncClient; +import org.apache.hc.client5.testing.extension.async.TestAsyncServerBootstrap; import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.HttpHeaders; import org.apache.hc.core5.http.HttpHost; @@ -82,10 +84,25 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra super(scheme, clientProtocolLevel, serverProtocolLevel); } + public void configureServerWithBasicAuth(final Authenticator authenticator, + final Consumer serverCustomizer) { + configureServer(bootstrap -> { + bootstrap.setExchangeHandlerDecorator(requestHandler -> + new AuthenticatingAsyncDecorator(requestHandler, authenticator)); + serverCustomizer.accept(bootstrap); + }); + } + + public void configureServerWithBasicAuth(final Consumer serverCustomizer) { + configureServerWithBasicAuth( + new BasicTestAuthenticator("test:test", "test realm"), + serverCustomizer); + } + @Test void testBasicAuthenticationNoCreds() throws Exception { + configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new)); final HttpHost target = startServer(); - configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new)); final TestAsyncClient client = startClient(); @@ -107,8 +124,8 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra @Test void testBasicAuthenticationFailure() throws Exception { + configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new)); final HttpHost target = startServer(); - configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new)); final TestAsyncClient client = startClient(); @@ -132,8 +149,8 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra @Test void testBasicAuthenticationSuccess() throws Exception { + configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new)); final HttpHost target = startServer(); - configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new)); final TestAsyncClient client = startClient(); @@ -158,8 +175,8 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra @Test void testBasicAuthenticationWithEntitySuccess() throws Exception { + configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new)); final HttpHost target = startServer(); - configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new)); final TestAsyncClient client = startClient(); @@ -184,8 +201,8 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra @Test void testBasicAuthenticationExpectationFailure() throws Exception { + configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new)); final HttpHost target = startServer(); - configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new)); final TestAsyncClient client = startClient(); @@ -209,8 +226,8 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra @Test void testBasicAuthenticationExpectationSuccess() throws Exception { + configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new)); final HttpHost target = startServer(); - configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new)); final TestAsyncClient client = startClient(); @@ -236,8 +253,8 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra @Test void testBasicAuthenticationCredentialsCaching() throws Exception { + configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new)); final HttpHost target = startServer(); - configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new)); final DefaultAuthenticationStrategy authStrategy = Mockito.spy(new DefaultAuthenticationStrategy()); configureClient(builder -> builder.setTargetAuthenticationStrategy(authStrategy)); @@ -263,8 +280,8 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra @Test void testBasicAuthenticationCredentialsCachingByPathPrefix() throws Exception { + configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new)); final HttpHost target = startServer(); - configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new)); final DefaultAuthenticationStrategy authStrategy = Mockito.spy(new DefaultAuthenticationStrategy()); final Queue responseQueue = new ConcurrentLinkedQueue<>(); @@ -328,8 +345,8 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra @Test void testAuthenticationUserinfoInRequestFailure() throws Exception { + configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new)); final HttpHost target = startServer(); - configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new)); final TestAsyncClient client = startClient(); @@ -359,8 +376,7 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra } }; - final HttpHost target = startServer(); - configureServer(bootstrap -> bootstrap + configureServerWithBasicAuth(bootstrap -> bootstrap .register("*", AsyncEchoHandler::new) .setExchangeHandlerDecorator(exchangeHandler -> new AuthenticatingAsyncDecorator(exchangeHandler, authenticator) { @@ -371,6 +387,7 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra } })); + final HttpHost target = startServer(); final CredentialsProvider credsProvider = Mockito.mock(CredentialsProvider.class); Mockito.when(credsProvider.getCredentials(Mockito.any(), Mockito.any())) @@ -392,7 +409,6 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra configureClient(builder -> builder.setDefaultAuthSchemeRegistry(authSchemeRegistry)); final TestAsyncClient client = startClient(); - final RequestConfig config = RequestConfig.custom() .setTargetPreferredAuthSchemes(Collections.singletonList("MyBasic")) .build(); @@ -414,8 +430,7 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra @Test void testAuthenticationFallback() throws Exception { - final HttpHost target = startServer(); - configureServer(bootstrap -> bootstrap + configureServerWithBasicAuth(bootstrap -> bootstrap .register("*", AsyncEchoHandler::new) .setExchangeHandlerDecorator(exchangeHandler -> new AuthenticatingAsyncDecorator(exchangeHandler, new BasicTestAuthenticator("test:test", "test realm")) { @@ -425,6 +440,7 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra } })); + final HttpHost target = startServer(); final TestAsyncClient client = startClient(); @@ -457,14 +473,14 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra } final String token = buf.toString(); - final HttpHost target = startServer(); - configureServer(bootstrap -> bootstrap + configureServerWithBasicAuth(bootstrap -> bootstrap .register("*", AsyncEchoHandler::new) .setExchangeHandlerDecorator(requestHandler -> new AuthenticatingAsyncDecorator( requestHandler, new BearerAuthenticationHandler(), new BasicTestAuthenticator(token, "test realm")))); + final HttpHost target = startServer(); final TestAsyncClient client = startClient(); diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/HttpIntegrationTests.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/HttpIntegrationTests.java index d5dfc1324..d0d02dda3 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/HttpIntegrationTests.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/HttpIntegrationTests.java @@ -173,44 +173,44 @@ class HttpIntegrationTests { } -// @Nested -// @DisplayName("Client authentication (HTTP/1.1)") -// class AuthenticationHttp1 extends TestHttp1ClientAuthentication { -// -// public AuthenticationHttp1() throws Exception { -// super(URIScheme.HTTP); -// } -// -// } -// -// @Nested -// @DisplayName("Client authentication (HTTP/1.1, TLS)") -// class AuthenticationHttp1Tls extends TestHttp1ClientAuthentication { -// -// public AuthenticationHttp1Tls() throws Exception { -// super(URIScheme.HTTPS); -// } -// -// } + @Nested + @DisplayName("Client authentication (HTTP/1.1)") + class AuthenticationHttp1 extends TestHttp1ClientAuthentication { + + public AuthenticationHttp1() throws Exception { + super(URIScheme.HTTP); + } + + } + + @Nested + @DisplayName("Client authentication (HTTP/1.1, TLS)") + class AuthenticationHttp1Tls extends TestHttp1ClientAuthentication { + + public AuthenticationHttp1Tls() throws Exception { + super(URIScheme.HTTPS); + } + + } + + @Nested + @DisplayName("Client authentication (HTTP/2)") + class AuthenticationH2 extends TestH2ClientAuthentication { + + public AuthenticationH2() throws Exception { + super(URIScheme.HTTP); + } + + } + + @Nested + @DisplayName("Client authentication (HTTP/2, TLS)") + class AuthenticationH2Tls extends TestH2ClientAuthentication { + + public AuthenticationH2Tls() throws Exception { + super(URIScheme.HTTPS); + } + + } -// @Nested -// @DisplayName("Client authentication (HTTP/2)") -// class AuthenticationH2 extends TestH2ClientAuthentication { -// -// public AuthenticationH2() throws Exception { -// super(URIScheme.HTTP); -// } -// -// } -// -// @Nested -// @DisplayName("Client authentication (HTTP/2, TLS)") -// class AuthenticationH2Tls extends TestH2ClientAuthentication { -// -// public AuthenticationH2Tls() throws Exception { -// super(URIScheme.HTTPS); -// } -// -// } -// } \ No newline at end of file diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/TestHttp1ClientAuthentication.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/TestHttp1ClientAuthentication.java index 9baa619e7..8cd4bcf2c 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/TestHttp1ClientAuthentication.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/TestHttp1ClientAuthentication.java @@ -57,7 +57,6 @@ abstract class TestHttp1ClientAuthentication extends AbstractHttpAsyncClientAuth @Test void testBasicAuthenticationSuccessNonPersistentConnection() throws Exception { - final HttpHost target = startServer(); configureServer(bootstrap -> bootstrap .register("*", AsyncEchoHandler::new) .setExchangeHandlerDecorator(exchangeHandler -> @@ -68,6 +67,7 @@ abstract class TestHttp1ClientAuthentication extends AbstractHttpAsyncClientAuth unauthorized.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE); } })); + final HttpHost target = startServer(); final TestAsyncClient client = startClient();