Skip to content

Commit

Permalink
Re-enabled async client authentication tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ok2c committed Oct 16, 2024
1 parent e6a4104 commit 876d0f0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
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;
Expand All @@ -60,6 +61,7 @@
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;
Expand All @@ -82,10 +84,25 @@ public AbstractHttpAsyncClientAuthenticationTest(final URIScheme scheme, final C
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
void testBasicAuthenticationNoCreds() throws Exception {
configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
final HttpHost target = startServer();
configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));

final TestAsyncClient client = startClient();

Expand All @@ -107,8 +124,8 @@ void testBasicAuthenticationNoCreds() throws Exception {

@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();

Expand All @@ -132,8 +149,8 @@ void testBasicAuthenticationFailure() throws Exception {

@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();

Expand All @@ -158,8 +175,8 @@ void testBasicAuthenticationSuccess() throws Exception {

@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();

Expand All @@ -184,8 +201,8 @@ void testBasicAuthenticationWithEntitySuccess() throws Exception {

@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();

Expand All @@ -209,8 +226,8 @@ void testBasicAuthenticationExpectationFailure() throws Exception {

@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();

Expand All @@ -236,8 +253,8 @@ void testBasicAuthenticationExpectationSuccess() throws Exception {

@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));
Expand All @@ -263,8 +280,8 @@ void testBasicAuthenticationCredentialsCaching() throws Exception {

@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<HttpResponse> responseQueue = new ConcurrentLinkedQueue<>();
Expand Down Expand Up @@ -328,8 +345,8 @@ void testBasicAuthenticationCredentialsCachingByPathPrefix() throws Exception {

@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();

Expand Down Expand Up @@ -359,8 +376,7 @@ public boolean authenticate(final URIAuthority authority, final String requestUr
}
};

final HttpHost target = startServer();
configureServer(bootstrap -> bootstrap
configureServerWithBasicAuth(bootstrap -> bootstrap
.register("*", AsyncEchoHandler::new)
.setExchangeHandlerDecorator(exchangeHandler -> new AuthenticatingAsyncDecorator(exchangeHandler, authenticator) {

Expand All @@ -371,6 +387,7 @@ protected void customizeUnauthorizedResponse(final HttpResponse unauthorized) {
}

}));
final HttpHost target = startServer();

final CredentialsProvider credsProvider = Mockito.mock(CredentialsProvider.class);
Mockito.when(credsProvider.getCredentials(Mockito.any(), Mockito.any()))
Expand All @@ -392,7 +409,6 @@ public String getName() {
configureClient(builder -> builder.setDefaultAuthSchemeRegistry(authSchemeRegistry));
final TestAsyncClient client = startClient();


final RequestConfig config = RequestConfig.custom()
.setTargetPreferredAuthSchemes(Collections.singletonList("MyBasic"))
.build();
Expand All @@ -414,8 +430,7 @@ public String getName() {

@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")) {

Expand All @@ -425,6 +440,7 @@ protected void customizeUnauthorizedResponse(final HttpResponse unauthorized) {
}

}));
final HttpHost target = startServer();

final TestAsyncClient client = startClient();

Expand Down Expand Up @@ -457,14 +473,14 @@ void testBearerTokenAuthentication() throws Exception {
}
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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,44 +173,44 @@ public RedirectsH2Tls() {

}

// @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/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);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public TestHttp1ClientAuthentication(final URIScheme scheme) {

@Test
void testBasicAuthenticationSuccessNonPersistentConnection() throws Exception {
final HttpHost target = startServer();
configureServer(bootstrap -> bootstrap
.register("*", AsyncEchoHandler::new)
.setExchangeHandlerDecorator(exchangeHandler ->
Expand All @@ -68,6 +67,7 @@ protected void customizeUnauthorizedResponse(final HttpResponse unauthorized) {
unauthorized.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
}
}));
final HttpHost target = startServer();

final TestAsyncClient client = startClient();

Expand Down

0 comments on commit 876d0f0

Please sign in to comment.