5959import java .util .Arrays ;
6060import java .util .Collections ;
6161import java .util .HashSet ;
62+ import java .util .List ;
6263import java .util .Set ;
6364import java .util .concurrent .ExecutorService ;
6465import java .util .concurrent .Executors ;
7071import static org .elasticsearch .client .RestClientTestUtil .getOkStatusCodes ;
7172import static org .elasticsearch .client .RestClientTestUtil .randomStatusCode ;
7273import static org .elasticsearch .client .SyncResponseListenerTests .assertExceptionStackContainsCallingMethod ;
74+ import static org .hamcrest .CoreMatchers .containsString ;
7375import static org .hamcrest .CoreMatchers .equalTo ;
7476import static org .hamcrest .CoreMatchers .instanceOf ;
7577import static org .junit .Assert .assertArrayEquals ;
7678import static org .junit .Assert .assertEquals ;
79+ import static org .junit .Assert .assertFalse ;
7780import static org .junit .Assert .assertThat ;
7881import static org .junit .Assert .assertTrue ;
7982import static org .junit .Assert .fail ;
@@ -96,6 +99,7 @@ public class RestClientSingleHostTests extends RestClientTestCase {
9699 private Node node ;
97100 private CloseableHttpAsyncClient httpClient ;
98101 private HostsTrackingFailureListener failureListener ;
102+ private boolean strictDeprecationMode ;
99103
100104 @ Before
101105 @ SuppressWarnings ("unchecked" )
@@ -147,8 +151,9 @@ public void run() {
147151 defaultHeaders = RestClientTestUtil .randomHeaders (getRandom (), "Header-default" );
148152 node = new Node (new HttpHost ("localhost" , 9200 ));
149153 failureListener = new HostsTrackingFailureListener ();
154+ strictDeprecationMode = randomBoolean ();
150155 restClient = new RestClient (httpClient , 10000 , defaultHeaders ,
151- singletonList (node ), null , failureListener , NodeSelector .ANY , false );
156+ singletonList (node ), null , failureListener , NodeSelector .ANY , strictDeprecationMode );
152157 }
153158
154159 /**
@@ -331,9 +336,54 @@ public void testHeaders() throws IOException {
331336 }
332337 assertThat (esResponse .getStatusLine ().getStatusCode (), equalTo (statusCode ));
333338 assertHeaders (defaultHeaders , requestHeaders , esResponse .getHeaders (), Collections .<String >emptySet ());
339+ assertFalse (esResponse .hasWarnings ());
334340 }
335341 }
336342
343+ public void testDeprecationWarnings () throws IOException {
344+ String chars = randomAsciiAlphanumOfLength (5 );
345+ assertDeprecationWarnings (singletonList ("poorly formatted " + chars ), singletonList ("poorly formatted " + chars ));
346+ assertDeprecationWarnings (singletonList (formatWarning (chars )), singletonList (chars ));
347+ assertDeprecationWarnings (
348+ Arrays .asList (formatWarning (chars ), "another one" , "and another" ),
349+ Arrays .asList (chars , "another one" , "and another" ));
350+
351+ }
352+
353+ private void assertDeprecationWarnings (List <String > warningHeaderTexts , List <String > warningBodyTexts ) throws IOException {
354+ String method = randomFrom (getHttpMethods ());
355+ Request request = new Request (method , "/200" );
356+ RequestOptions .Builder options = request .getOptions ().toBuilder ();
357+ for (String warningHeaderText : warningHeaderTexts ) {
358+ options .addHeader ("Warning" , warningHeaderText );
359+ }
360+ request .setOptions (options );
361+
362+ Response response ;
363+ if (strictDeprecationMode ) {
364+ try {
365+ restClient .performRequest (request );
366+ fail ("expected ResponseException because strict deprecation mode is enabled" );
367+ return ;
368+ } catch (ResponseException e ) {
369+ assertThat (e .getMessage (), containsString ("\n Warnings: " + warningBodyTexts ));
370+ response = e .getResponse ();
371+ }
372+ } else {
373+ response = restClient .performRequest (request );
374+ }
375+ assertTrue (response .hasWarnings ());
376+ assertEquals (warningBodyTexts , response .getWarnings ());
377+ }
378+
379+ /**
380+ * Emulates Elasticsearch's DeprecationLogger.formatWarning in simple
381+ * cases. We don't have that available because we're testing against 1.7.
382+ */
383+ private static String formatWarning (String warningBody ) {
384+ return "299 Elasticsearch-1.2.2-SNAPSHOT-eeeeeee \" " + warningBody + "\" \" Mon, 01 Jan 2001 00:00:00 GMT\" " ;
385+ }
386+
337387 private HttpUriRequest performRandomRequest (String method ) throws Exception {
338388 String uriAsString = "/" + randomStatusCode (getRandom ());
339389 Request request = new Request (method , uriAsString );
0 commit comments