3636import java .util .concurrent .ExecutionException ;
3737import java .util .concurrent .Future ;
3838import java .util .concurrent .atomic .AtomicLong ;
39+ import java .util .function .Consumer ;
3940import java .util .stream .Collectors ;
4041
4142import org .apache .hc .client5 .http .async .methods .SimpleHttpRequest ;
6061import org .apache .hc .client5 .testing .extension .async .ClientProtocolLevel ;
6162import org .apache .hc .client5 .testing .extension .async .ServerProtocolLevel ;
6263import org .apache .hc .client5 .testing .extension .async .TestAsyncClient ;
64+ import org .apache .hc .client5 .testing .extension .async .TestAsyncServerBootstrap ;
6365import org .apache .hc .core5 .http .ContentType ;
6466import org .apache .hc .core5 .http .HttpHeaders ;
6567import 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
0 commit comments