Re-enabled async client authentication tests

This commit is contained in:
Oleg Kalnichevski 2024-10-16 13:44:45 +02:00
parent 0335839e22
commit 29bf4bde45
3 changed files with 72 additions and 56 deletions

View File

@ -36,6 +36,7 @@ import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; 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.ClientProtocolLevel;
import org.apache.hc.client5.testing.extension.async.ServerProtocolLevel; 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.TestAsyncClient;
import org.apache.hc.client5.testing.extension.async.TestAsyncServerBootstrap;
import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpHeaders; import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.HttpHost;
@ -82,10 +84,25 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra
super(scheme, clientProtocolLevel, serverProtocolLevel); super(scheme, clientProtocolLevel, serverProtocolLevel);
} }
public void configureServerWithBasicAuth(final Authenticator authenticator,
final Consumer<TestAsyncServerBootstrap> serverCustomizer) {
configureServer(bootstrap -> {
bootstrap.setExchangeHandlerDecorator(requestHandler ->
new AuthenticatingAsyncDecorator(requestHandler, authenticator));
serverCustomizer.accept(bootstrap);
});
}
public void configureServerWithBasicAuth(final Consumer<TestAsyncServerBootstrap> serverCustomizer) {
configureServerWithBasicAuth(
new BasicTestAuthenticator("test:test", "test realm"),
serverCustomizer);
}
@Test @Test
void testBasicAuthenticationNoCreds() throws Exception { void testBasicAuthenticationNoCreds() throws Exception {
configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
final HttpHost target = startServer(); final HttpHost target = startServer();
configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
final TestAsyncClient client = startClient(); final TestAsyncClient client = startClient();
@ -107,8 +124,8 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra
@Test @Test
void testBasicAuthenticationFailure() throws Exception { void testBasicAuthenticationFailure() throws Exception {
configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
final HttpHost target = startServer(); final HttpHost target = startServer();
configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
final TestAsyncClient client = startClient(); final TestAsyncClient client = startClient();
@ -132,8 +149,8 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra
@Test @Test
void testBasicAuthenticationSuccess() throws Exception { void testBasicAuthenticationSuccess() throws Exception {
configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
final HttpHost target = startServer(); final HttpHost target = startServer();
configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
final TestAsyncClient client = startClient(); final TestAsyncClient client = startClient();
@ -158,8 +175,8 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra
@Test @Test
void testBasicAuthenticationWithEntitySuccess() throws Exception { void testBasicAuthenticationWithEntitySuccess() throws Exception {
configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
final HttpHost target = startServer(); final HttpHost target = startServer();
configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
final TestAsyncClient client = startClient(); final TestAsyncClient client = startClient();
@ -184,8 +201,8 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra
@Test @Test
void testBasicAuthenticationExpectationFailure() throws Exception { void testBasicAuthenticationExpectationFailure() throws Exception {
configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
final HttpHost target = startServer(); final HttpHost target = startServer();
configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
final TestAsyncClient client = startClient(); final TestAsyncClient client = startClient();
@ -209,8 +226,8 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra
@Test @Test
void testBasicAuthenticationExpectationSuccess() throws Exception { void testBasicAuthenticationExpectationSuccess() throws Exception {
configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
final HttpHost target = startServer(); final HttpHost target = startServer();
configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
final TestAsyncClient client = startClient(); final TestAsyncClient client = startClient();
@ -236,8 +253,8 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra
@Test @Test
void testBasicAuthenticationCredentialsCaching() throws Exception { void testBasicAuthenticationCredentialsCaching() throws Exception {
configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
final HttpHost target = startServer(); final HttpHost target = startServer();
configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
final DefaultAuthenticationStrategy authStrategy = Mockito.spy(new DefaultAuthenticationStrategy()); final DefaultAuthenticationStrategy authStrategy = Mockito.spy(new DefaultAuthenticationStrategy());
configureClient(builder -> builder.setTargetAuthenticationStrategy(authStrategy)); configureClient(builder -> builder.setTargetAuthenticationStrategy(authStrategy));
@ -263,8 +280,8 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra
@Test @Test
void testBasicAuthenticationCredentialsCachingByPathPrefix() throws Exception { void testBasicAuthenticationCredentialsCachingByPathPrefix() throws Exception {
configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
final HttpHost target = startServer(); final HttpHost target = startServer();
configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
final DefaultAuthenticationStrategy authStrategy = Mockito.spy(new DefaultAuthenticationStrategy()); final DefaultAuthenticationStrategy authStrategy = Mockito.spy(new DefaultAuthenticationStrategy());
final Queue<HttpResponse> responseQueue = new ConcurrentLinkedQueue<>(); final Queue<HttpResponse> responseQueue = new ConcurrentLinkedQueue<>();
@ -328,8 +345,8 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra
@Test @Test
void testAuthenticationUserinfoInRequestFailure() throws Exception { void testAuthenticationUserinfoInRequestFailure() throws Exception {
configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
final HttpHost target = startServer(); final HttpHost target = startServer();
configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
final TestAsyncClient client = startClient(); final TestAsyncClient client = startClient();
@ -359,8 +376,7 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra
} }
}; };
final HttpHost target = startServer(); configureServerWithBasicAuth(bootstrap -> bootstrap
configureServer(bootstrap -> bootstrap
.register("*", AsyncEchoHandler::new) .register("*", AsyncEchoHandler::new)
.setExchangeHandlerDecorator(exchangeHandler -> new AuthenticatingAsyncDecorator(exchangeHandler, authenticator) { .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); final CredentialsProvider credsProvider = Mockito.mock(CredentialsProvider.class);
Mockito.when(credsProvider.getCredentials(Mockito.any(), Mockito.any())) Mockito.when(credsProvider.getCredentials(Mockito.any(), Mockito.any()))
@ -392,7 +409,6 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra
configureClient(builder -> builder.setDefaultAuthSchemeRegistry(authSchemeRegistry)); configureClient(builder -> builder.setDefaultAuthSchemeRegistry(authSchemeRegistry));
final TestAsyncClient client = startClient(); final TestAsyncClient client = startClient();
final RequestConfig config = RequestConfig.custom() final RequestConfig config = RequestConfig.custom()
.setTargetPreferredAuthSchemes(Collections.singletonList("MyBasic")) .setTargetPreferredAuthSchemes(Collections.singletonList("MyBasic"))
.build(); .build();
@ -414,8 +430,7 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra
@Test @Test
void testAuthenticationFallback() throws Exception { void testAuthenticationFallback() throws Exception {
final HttpHost target = startServer(); configureServerWithBasicAuth(bootstrap -> bootstrap
configureServer(bootstrap -> bootstrap
.register("*", AsyncEchoHandler::new) .register("*", AsyncEchoHandler::new)
.setExchangeHandlerDecorator(exchangeHandler -> new AuthenticatingAsyncDecorator(exchangeHandler, new BasicTestAuthenticator("test:test", "test realm")) { .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(); final TestAsyncClient client = startClient();
@ -457,14 +473,14 @@ abstract class AbstractHttpAsyncClientAuthenticationTest extends AbstractIntegra
} }
final String token = buf.toString(); final String token = buf.toString();
final HttpHost target = startServer(); configureServerWithBasicAuth(bootstrap -> bootstrap
configureServer(bootstrap -> bootstrap
.register("*", AsyncEchoHandler::new) .register("*", AsyncEchoHandler::new)
.setExchangeHandlerDecorator(requestHandler -> .setExchangeHandlerDecorator(requestHandler ->
new AuthenticatingAsyncDecorator( new AuthenticatingAsyncDecorator(
requestHandler, requestHandler,
new BearerAuthenticationHandler(), new BearerAuthenticationHandler(),
new BasicTestAuthenticator(token, "test realm")))); new BasicTestAuthenticator(token, "test realm"))));
final HttpHost target = startServer();
final TestAsyncClient client = startClient(); final TestAsyncClient client = startClient();

View File

@ -173,44 +173,44 @@ class HttpIntegrationTests {
} }
// @Nested @Nested
// @DisplayName("Client authentication (HTTP/1.1)") @DisplayName("Client authentication (HTTP/1.1)")
// class AuthenticationHttp1 extends TestHttp1ClientAuthentication { class AuthenticationHttp1 extends TestHttp1ClientAuthentication {
//
// public AuthenticationHttp1() throws Exception { public AuthenticationHttp1() throws Exception {
// super(URIScheme.HTTP); super(URIScheme.HTTP);
// } }
//
// } }
//
// @Nested @Nested
// @DisplayName("Client authentication (HTTP/1.1, TLS)") @DisplayName("Client authentication (HTTP/1.1, TLS)")
// class AuthenticationHttp1Tls extends TestHttp1ClientAuthentication { class AuthenticationHttp1Tls extends TestHttp1ClientAuthentication {
//
// public AuthenticationHttp1Tls() throws Exception { public AuthenticationHttp1Tls() throws Exception {
// super(URIScheme.HTTPS); 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);
// }
//
// }
//
} }

View File

@ -57,7 +57,6 @@ abstract class TestHttp1ClientAuthentication extends AbstractHttpAsyncClientAuth
@Test @Test
void testBasicAuthenticationSuccessNonPersistentConnection() throws Exception { void testBasicAuthenticationSuccessNonPersistentConnection() throws Exception {
final HttpHost target = startServer();
configureServer(bootstrap -> bootstrap configureServer(bootstrap -> bootstrap
.register("*", AsyncEchoHandler::new) .register("*", AsyncEchoHandler::new)
.setExchangeHandlerDecorator(exchangeHandler -> .setExchangeHandlerDecorator(exchangeHandler ->
@ -68,6 +67,7 @@ abstract class TestHttp1ClientAuthentication extends AbstractHttpAsyncClientAuth
unauthorized.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE); unauthorized.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
} }
})); }));
final HttpHost target = startServer();
final TestAsyncClient client = startClient(); final TestAsyncClient client = startClient();