2727package org .apache .hc .client5 .testing .compatibility .async ;
2828
2929import java .util .Queue ;
30+ import java .util .concurrent .Callable ;
3031import java .util .concurrent .ConcurrentLinkedQueue ;
3132import java .util .concurrent .CountDownLatch ;
3233import java .util .concurrent .Future ;
3334
35+ import javax .security .auth .Subject ;
36+
3437import org .apache .hc .client5 .http .ContextBuilder ;
3538import org .apache .hc .client5 .http .async .methods .SimpleHttpRequest ;
3639import org .apache .hc .client5 .http .async .methods .SimpleHttpResponse ;
4649import org .apache .hc .client5 .testing .compatibility .spnego .SpnegoAuthenticationStrategy ;
4750import org .apache .hc .client5 .testing .compatibility .spnego .SpnegoTestUtil ;
4851import org .apache .hc .client5 .testing .extension .async .HttpAsyncClientResource ;
52+ import org .apache .hc .client5 .testing .util .SecurityUtils ;
4953import org .apache .hc .core5 .concurrent .FutureCallback ;
5054import org .apache .hc .core5 .http .HttpHost ;
5155import org .apache .hc .core5 .http .HttpStatus ;
5761import org .junit .jupiter .api .Test ;
5862import org .junit .jupiter .api .extension .RegisterExtension ;
5963
64+
6065public abstract class HttpAsyncClientCompatibilityTest {
6166
6267 static final Timeout TIMEOUT = Timeout .ofSeconds (5 );
@@ -69,21 +74,32 @@ public abstract class HttpAsyncClientCompatibilityTest {
6974 private final BasicCredentialsProvider credentialsProvider ;
7075 protected final Credentials targetCreds ;
7176 protected String secretPath = "/private/big-secret.txt" ;
77+ protected Subject doAs ;
7278
7379 public HttpAsyncClientCompatibilityTest (
7480 final HttpVersionPolicy versionPolicy ,
7581 final HttpHost target ,
7682 final Credentials targetCreds ,
7783 final HttpHost proxy ,
7884 final Credentials proxyCreds ) throws Exception {
85+ this (versionPolicy , target , targetCreds , proxy , proxyCreds , null );
86+ }
87+
88+ public HttpAsyncClientCompatibilityTest (
89+ final HttpVersionPolicy versionPolicy ,
90+ final HttpHost target ,
91+ final Credentials targetCreds ,
92+ final HttpHost proxy ,
93+ final Credentials proxyCreds ,
94+ final Subject doAs ) throws Exception {
95+ this .doAs = doAs ;
7996 this .versionPolicy = versionPolicy ;
8097 this .target = target ;
8198 this .targetCreds = targetCreds ;
8299 this .credentialsProvider = new BasicCredentialsProvider ();
83100 this .clientResource = new HttpAsyncClientResource (versionPolicy );
84- if (targetCreds != null ) {
85- //this.setCredentials(new AuthScope(target), targetCreds);
86- if (targetCreds instanceof GssCredentials ) {
101+ if (targetCreds != null || doAs != null ) {
102+ if (targetCreds instanceof GssCredentials || doAs != null ) {
87103 secretPath = "/private_spnego/big-secret.txt" ;
88104 this .clientResource .configure (builder -> builder
89105 .setTargetAuthenticationStrategy (new SpnegoAuthenticationStrategy ())
@@ -238,6 +254,22 @@ void test_auth_failure_wrong_auth_credentials() throws Exception {
238254
239255 @ Test
240256 void test_auth_success () throws Exception {
257+ if (doAs != null ) {
258+ SecurityUtils .callAs (doAs , () -> {
259+ final CloseableHttpAsyncClient client = client ();
260+ final HttpClientContext context = context ();
261+
262+ final SimpleHttpRequest httpGetSecret = SimpleRequestBuilder .get ()
263+ .setHttpHost (target )
264+ .setPath (secretPath )
265+ .build ();
266+ final Future <SimpleHttpResponse > future = client .execute (httpGetSecret , context , null );
267+ final SimpleHttpResponse response = future .get (TIMEOUT .getDuration (), TIMEOUT .getTimeUnit ());
268+ Assertions .assertEquals (HttpStatus .SC_OK , response .getCode ());
269+ assertProtocolVersion (context );
270+ return 0 ;
271+ });
272+ } else {
241273 setCredentials (
242274 new AuthScope (target ),
243275 targetCreds );
@@ -253,5 +285,6 @@ void test_auth_success() throws Exception {
253285 Assertions .assertEquals (HttpStatus .SC_OK , response .getCode ());
254286 assertProtocolVersion (context );
255287 }
288+ }
256289
257290}
0 commit comments