31
31
import org .apache .http .entity .ContentType ;
32
32
import org .apache .http .entity .StringEntity ;
33
33
import org .apache .http .impl .client .BasicCredentialsProvider ;
34
+ import org .apache .http .impl .client .TargetAuthenticationStrategy ;
34
35
import org .apache .http .impl .nio .client .HttpAsyncClientBuilder ;
36
+ import org .apache .http .message .BasicHeader ;
35
37
import org .apache .http .nio .entity .NStringEntity ;
36
38
import org .apache .http .util .EntityUtils ;
37
39
import org .elasticsearch .mocksocket .MockHttpServer ;
38
40
import org .junit .After ;
39
- import org .junit .AfterClass ;
40
41
import org .junit .Before ;
41
- import org .junit .BeforeClass ;
42
42
43
43
import java .io .IOException ;
44
44
import java .io .InputStreamReader ;
@@ -147,6 +147,8 @@ public HttpAsyncClientBuilder customizeHttpClient(final HttpAsyncClientBuilder h
147
147
if (usePreemptiveAuth == false ) {
148
148
// disable preemptive auth by ignoring any authcache
149
149
httpClientBuilder .disableAuthCaching ();
150
+ // don't use the "persistent credentials strategy"
151
+ httpClientBuilder .setTargetAuthenticationStrategy (new TargetAuthenticationStrategy ());
150
152
}
151
153
152
154
return httpClientBuilder .setDefaultCredentialsProvider (credentialsProvider );
@@ -193,7 +195,7 @@ public void onFailure(Exception exception) {
193
195
assertTrue ("timeout waiting for requests to be sent" , latch .await (10 , TimeUnit .SECONDS ));
194
196
if (exceptions .isEmpty () == false ) {
195
197
AssertionError error = new AssertionError ("expected no failures but got some. see suppressed for first 10 of ["
196
- + exceptions .size () + "] failures" );
198
+ + exceptions .size () + "] failures" );
197
199
for (Exception exception : exceptions .subList (0 , Math .min (10 , exceptions .size ()))) {
198
200
error .addSuppressed (exception );
199
201
}
@@ -217,7 +219,7 @@ public void testHeaders() throws IOException {
217
219
Response esResponse ;
218
220
try {
219
221
esResponse = restClient .performRequest (method , "/" + statusCode , Collections .<String , String >emptyMap (), requestHeaders );
220
- } catch (ResponseException e ) {
222
+ } catch (ResponseException e ) {
221
223
esResponse = e .getResponse ();
222
224
}
223
225
@@ -291,8 +293,8 @@ public void testEncodeParams() throws IOException {
291
293
/**
292
294
* Verify that credentials are sent on the first request with preemptive auth enabled (default when provided with credentials).
293
295
*/
294
- public void testPreemptiveAuthEnabled () throws IOException {
295
- final String [] methods = { "POST" , "PUT" , "GET" , "DELETE" };
296
+ public void testPreemptiveAuthEnabled () throws IOException {
297
+ final String [] methods = {"POST" , "PUT" , "GET" , "DELETE" };
296
298
297
299
try (RestClient restClient = createRestClient (true , true )) {
298
300
for (final String method : methods ) {
@@ -306,8 +308,8 @@ public void testPreemptiveAuthEnabled() throws IOException {
306
308
/**
307
309
* Verify that credentials are <em>not</em> sent on the first request with preemptive auth disabled.
308
310
*/
309
- public void testPreemptiveAuthDisabled () throws IOException {
310
- final String [] methods = { "POST" , "PUT" , "GET" , "DELETE" };
311
+ public void testPreemptiveAuthDisabled () throws IOException {
312
+ final String [] methods = {"POST" , "PUT" , "GET" , "DELETE" };
311
313
312
314
try (RestClient restClient = createRestClient (true , false )) {
313
315
for (final String method : methods ) {
@@ -318,12 +320,31 @@ public void testPreemptiveAuthDisabled() throws IOException {
318
320
}
319
321
}
320
322
323
+ /**
324
+ * Verify that credentials continue to be sent even if a 401 (Unauthorized) response is received
325
+ */
326
+ public void testAuthCredentialsAreNotClearedOnAuthChallenge () throws IOException {
327
+ final String [] methods = {"POST" , "PUT" , "GET" , "DELETE" };
328
+
329
+ try (RestClient restClient = createRestClient (true , true )) {
330
+ for (final String method : methods ) {
331
+ Header realmHeader = new BasicHeader ("WWW-Authenticate" , "Basic realm=\" test\" " );
332
+ final Response response401 = bodyTest (restClient , method , 401 , new Header []{realmHeader });
333
+ assertThat (response401 .getHeader ("Authorization" ), startsWith ("Basic" ));
334
+
335
+ final Response response200 = bodyTest (restClient , method , 200 , new Header [0 ]);
336
+ assertThat (response200 .getHeader ("Authorization" ), startsWith ("Basic" ));
337
+ }
338
+ }
339
+
340
+ }
341
+
321
342
public void testUrlWithoutLeadingSlash () throws Exception {
322
343
if (pathPrefix .length () == 0 ) {
323
344
try {
324
345
restClient .performRequest ("GET" , "200" );
325
346
fail ("request should have failed" );
326
- } catch (ResponseException e ) {
347
+ } catch (ResponseException e ) {
327
348
assertEquals (404 , e .getResponse ().getStatusLine ().getStatusCode ());
328
349
}
329
350
} else {
@@ -335,8 +356,8 @@ public void testUrlWithoutLeadingSlash() throws Exception {
335
356
{
336
357
//pathPrefix is not required to start with '/', will be added automatically
337
358
try (RestClient restClient = RestClient .builder (
338
- new HttpHost (httpServer .getAddress ().getHostString (), httpServer .getAddress ().getPort ()))
339
- .setPathPrefix (pathPrefix .substring (1 )).build ()) {
359
+ new HttpHost (httpServer .getAddress ().getHostString (), httpServer .getAddress ().getPort ()))
360
+ .setPathPrefix (pathPrefix .substring (1 )).build ()) {
340
361
Response response = restClient .performRequest ("GET" , "200" );
341
362
//a trailing slash gets automatically added if a pathPrefix is configured
342
363
assertEquals (200 , response .getStatusLine ().getStatusCode ());
@@ -350,10 +371,15 @@ private Response bodyTest(final String method) throws IOException {
350
371
}
351
372
352
373
private Response bodyTest (final RestClient restClient , final String method ) throws IOException {
353
- String requestBody = "{ \" field\" : \" value\" }" ;
354
374
int statusCode = randomStatusCode (getRandom ());
375
+ return bodyTest (restClient , method , statusCode , new Header [0 ]);
376
+ }
377
+
378
+ private Response bodyTest (RestClient restClient , String method , int statusCode , Header [] headers ) throws IOException {
379
+ String requestBody = "{ \" field\" : \" value\" }" ;
355
380
Request request = new Request (method , "/" + statusCode );
356
381
request .setJsonEntity (requestBody );
382
+ request .setHeaders (headers );
357
383
Response esResponse ;
358
384
try {
359
385
esResponse = restClient .performRequest (request );
0 commit comments