Skip to content

Commit 876d0f0

Browse files
committed
Re-enabled async client authentication tests
1 parent e6a4104 commit 876d0f0

File tree

3 files changed

+73
-57
lines changed

3 files changed

+73
-57
lines changed

httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttpAsyncClientAuthenticationTest.java

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.concurrent.ExecutionException;
3737
import java.util.concurrent.Future;
3838
import java.util.concurrent.atomic.AtomicLong;
39+
import java.util.function.Consumer;
3940
import java.util.stream.Collectors;
4041

4142
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
@@ -60,6 +61,7 @@
6061
import org.apache.hc.client5.testing.extension.async.ClientProtocolLevel;
6162
import org.apache.hc.client5.testing.extension.async.ServerProtocolLevel;
6263
import org.apache.hc.client5.testing.extension.async.TestAsyncClient;
64+
import org.apache.hc.client5.testing.extension.async.TestAsyncServerBootstrap;
6365
import org.apache.hc.core5.http.ContentType;
6466
import org.apache.hc.core5.http.HttpHeaders;
6567
import org.apache.hc.core5.http.HttpHost;
@@ -82,10 +84,25 @@ public AbstractHttpAsyncClientAuthenticationTest(final URIScheme scheme, final C
8284
super(scheme, clientProtocolLevel, serverProtocolLevel);
8385
}
8486

87+
public void configureServerWithBasicAuth(final Authenticator authenticator,
88+
final Consumer<TestAsyncServerBootstrap> serverCustomizer) {
89+
configureServer(bootstrap -> {
90+
bootstrap.setExchangeHandlerDecorator(requestHandler ->
91+
new AuthenticatingAsyncDecorator(requestHandler, authenticator));
92+
serverCustomizer.accept(bootstrap);
93+
});
94+
}
95+
96+
public void configureServerWithBasicAuth(final Consumer<TestAsyncServerBootstrap> serverCustomizer) {
97+
configureServerWithBasicAuth(
98+
new BasicTestAuthenticator("test:test", "test realm"),
99+
serverCustomizer);
100+
}
101+
85102
@Test
86103
void testBasicAuthenticationNoCreds() throws Exception {
104+
configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
87105
final HttpHost target = startServer();
88-
configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
89106

90107
final TestAsyncClient client = startClient();
91108

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

108125
@Test
109126
void testBasicAuthenticationFailure() throws Exception {
127+
configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
110128
final HttpHost target = startServer();
111-
configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
112129

113130
final TestAsyncClient client = startClient();
114131

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

133150
@Test
134151
void testBasicAuthenticationSuccess() throws Exception {
152+
configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
135153
final HttpHost target = startServer();
136-
configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
137154

138155
final TestAsyncClient client = startClient();
139156

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

159176
@Test
160177
void testBasicAuthenticationWithEntitySuccess() throws Exception {
178+
configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
161179
final HttpHost target = startServer();
162-
configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
163180

164181
final TestAsyncClient client = startClient();
165182

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

185202
@Test
186203
void testBasicAuthenticationExpectationFailure() throws Exception {
204+
configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
187205
final HttpHost target = startServer();
188-
configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
189206

190207
final TestAsyncClient client = startClient();
191208

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

210227
@Test
211228
void testBasicAuthenticationExpectationSuccess() throws Exception {
229+
configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
212230
final HttpHost target = startServer();
213-
configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
214231

215232
final TestAsyncClient client = startClient();
216233

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

237254
@Test
238255
void testBasicAuthenticationCredentialsCaching() throws Exception {
256+
configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
239257
final HttpHost target = startServer();
240-
configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
241258

242259
final DefaultAuthenticationStrategy authStrategy = Mockito.spy(new DefaultAuthenticationStrategy());
243260
configureClient(builder -> builder.setTargetAuthenticationStrategy(authStrategy));
@@ -263,8 +280,8 @@ void testBasicAuthenticationCredentialsCaching() throws Exception {
263280

264281
@Test
265282
void testBasicAuthenticationCredentialsCachingByPathPrefix() throws Exception {
283+
configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
266284
final HttpHost target = startServer();
267-
configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
268285

269286
final DefaultAuthenticationStrategy authStrategy = Mockito.spy(new DefaultAuthenticationStrategy());
270287
final Queue<HttpResponse> responseQueue = new ConcurrentLinkedQueue<>();
@@ -328,8 +345,8 @@ void testBasicAuthenticationCredentialsCachingByPathPrefix() throws Exception {
328345

329346
@Test
330347
void testAuthenticationUserinfoInRequestFailure() throws Exception {
348+
configureServerWithBasicAuth(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
331349
final HttpHost target = startServer();
332-
configureServer(bootstrap -> bootstrap.register("*", AsyncEchoHandler::new));
333350

334351
final TestAsyncClient client = startClient();
335352

@@ -359,8 +376,7 @@ public boolean authenticate(final URIAuthority authority, final String requestUr
359376
}
360377
};
361378

362-
final HttpHost target = startServer();
363-
configureServer(bootstrap -> bootstrap
379+
configureServerWithBasicAuth(bootstrap -> bootstrap
364380
.register("*", AsyncEchoHandler::new)
365381
.setExchangeHandlerDecorator(exchangeHandler -> new AuthenticatingAsyncDecorator(exchangeHandler, authenticator) {
366382

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

373389
}));
390+
final HttpHost target = startServer();
374391

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

395-
396412
final RequestConfig config = RequestConfig.custom()
397413
.setTargetPreferredAuthSchemes(Collections.singletonList("MyBasic"))
398414
.build();
@@ -414,8 +430,7 @@ public String getName() {
414430

415431
@Test
416432
void testAuthenticationFallback() throws Exception {
417-
final HttpHost target = startServer();
418-
configureServer(bootstrap -> bootstrap
433+
configureServerWithBasicAuth(bootstrap -> bootstrap
419434
.register("*", AsyncEchoHandler::new)
420435
.setExchangeHandlerDecorator(exchangeHandler -> new AuthenticatingAsyncDecorator(exchangeHandler, new BasicTestAuthenticator("test:test", "test realm")) {
421436

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

427442
}));
443+
final HttpHost target = startServer();
428444

429445
final TestAsyncClient client = startClient();
430446

@@ -457,14 +473,14 @@ void testBearerTokenAuthentication() throws Exception {
457473
}
458474
final String token = buf.toString();
459475

460-
final HttpHost target = startServer();
461-
configureServer(bootstrap -> bootstrap
476+
configureServerWithBasicAuth(bootstrap -> bootstrap
462477
.register("*", AsyncEchoHandler::new)
463478
.setExchangeHandlerDecorator(requestHandler ->
464479
new AuthenticatingAsyncDecorator(
465480
requestHandler,
466481
new BearerAuthenticationHandler(),
467482
new BasicTestAuthenticator(token, "test realm"))));
483+
final HttpHost target = startServer();
468484

469485
final TestAsyncClient client = startClient();
470486

httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/HttpIntegrationTests.java

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -173,44 +173,44 @@ public RedirectsH2Tls() {
173173

174174
}
175175

176-
// @Nested
177-
// @DisplayName("Client authentication (HTTP/1.1)")
178-
// class AuthenticationHttp1 extends TestHttp1ClientAuthentication {
179-
//
180-
// public AuthenticationHttp1() throws Exception {
181-
// super(URIScheme.HTTP);
182-
// }
183-
//
184-
// }
185-
//
186-
// @Nested
187-
// @DisplayName("Client authentication (HTTP/1.1, TLS)")
188-
// class AuthenticationHttp1Tls extends TestHttp1ClientAuthentication {
189-
//
190-
// public AuthenticationHttp1Tls() throws Exception {
191-
// super(URIScheme.HTTPS);
192-
// }
193-
//
194-
// }
195-
196-
// @Nested
197-
// @DisplayName("Client authentication (HTTP/2)")
198-
// class AuthenticationH2 extends TestH2ClientAuthentication {
199-
//
200-
// public AuthenticationH2() throws Exception {
201-
// super(URIScheme.HTTP);
202-
// }
203-
//
204-
// }
205-
//
206-
// @Nested
207-
// @DisplayName("Client authentication (HTTP/2, TLS)")
208-
// class AuthenticationH2Tls extends TestH2ClientAuthentication {
209-
//
210-
// public AuthenticationH2Tls() throws Exception {
211-
// super(URIScheme.HTTPS);
212-
// }
213-
//
214-
// }
215-
//
176+
@Nested
177+
@DisplayName("Client authentication (HTTP/1.1)")
178+
class AuthenticationHttp1 extends TestHttp1ClientAuthentication {
179+
180+
public AuthenticationHttp1() throws Exception {
181+
super(URIScheme.HTTP);
182+
}
183+
184+
}
185+
186+
@Nested
187+
@DisplayName("Client authentication (HTTP/1.1, TLS)")
188+
class AuthenticationHttp1Tls extends TestHttp1ClientAuthentication {
189+
190+
public AuthenticationHttp1Tls() throws Exception {
191+
super(URIScheme.HTTPS);
192+
}
193+
194+
}
195+
196+
@Nested
197+
@DisplayName("Client authentication (HTTP/2)")
198+
class AuthenticationH2 extends TestH2ClientAuthentication {
199+
200+
public AuthenticationH2() throws Exception {
201+
super(URIScheme.HTTP);
202+
}
203+
204+
}
205+
206+
@Nested
207+
@DisplayName("Client authentication (HTTP/2, TLS)")
208+
class AuthenticationH2Tls extends TestH2ClientAuthentication {
209+
210+
public AuthenticationH2Tls() throws Exception {
211+
super(URIScheme.HTTPS);
212+
}
213+
214+
}
215+
216216
}

httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/TestHttp1ClientAuthentication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public TestHttp1ClientAuthentication(final URIScheme scheme) {
5757

5858
@Test
5959
void testBasicAuthenticationSuccessNonPersistentConnection() throws Exception {
60-
final HttpHost target = startServer();
6160
configureServer(bootstrap -> bootstrap
6261
.register("*", AsyncEchoHandler::new)
6362
.setExchangeHandlerDecorator(exchangeHandler ->
@@ -68,6 +67,7 @@ protected void customizeUnauthorizedResponse(final HttpResponse unauthorized) {
6867
unauthorized.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
6968
}
7069
}));
70+
final HttpHost target = startServer();
7171

7272
final TestAsyncClient client = startClient();
7373

0 commit comments

Comments
 (0)