6060import java .util .Collections ;
6161import java .util .HashSet ;
6262import java .util .Map ;
63+ import java .util .List ;
6364import java .util .Set ;
6465import java .util .concurrent .ExecutorService ;
6566import java .util .concurrent .Executors ;
7273import static org .elasticsearch .client .RestClientTestUtil .randomHttpMethod ;
7374import static org .elasticsearch .client .RestClientTestUtil .randomStatusCode ;
7475import static org .elasticsearch .client .SyncResponseListenerTests .assertExceptionStackContainsCallingMethod ;
76+ import static org .hamcrest .CoreMatchers .containsString ;
7577import static org .hamcrest .CoreMatchers .equalTo ;
7678import static org .hamcrest .CoreMatchers .instanceOf ;
7779import static org .junit .Assert .assertArrayEquals ;
7880import static org .junit .Assert .assertEquals ;
81+ import static org .junit .Assert .assertFalse ;
7982import static org .junit .Assert .assertThat ;
8083import static org .junit .Assert .assertTrue ;
8184import static org .junit .Assert .fail ;
@@ -98,6 +101,7 @@ public class RestClientSingleHostTests extends RestClientTestCase {
98101 private Node node ;
99102 private CloseableHttpAsyncClient httpClient ;
100103 private HostsTrackingFailureListener failureListener ;
104+ private boolean strictDeprecationMode ;
101105
102106 @ Before
103107 @ SuppressWarnings ("unchecked" )
@@ -149,8 +153,9 @@ public void run() {
149153 defaultHeaders = RestClientTestUtil .randomHeaders (getRandom (), "Header-default" );
150154 node = new Node (new HttpHost ("localhost" , 9200 ));
151155 failureListener = new HostsTrackingFailureListener ();
156+ strictDeprecationMode = randomBoolean ();
152157 restClient = new RestClient (httpClient , 10000 , defaultHeaders ,
153- singletonList (node ), null , failureListener , NodeSelector .ANY , false );
158+ singletonList (node ), null , failureListener , NodeSelector .ANY , strictDeprecationMode );
154159 }
155160
156161 /**
@@ -377,9 +382,54 @@ public void testHeaders() throws IOException {
377382 }
378383 assertThat (esResponse .getStatusLine ().getStatusCode (), equalTo (statusCode ));
379384 assertHeaders (defaultHeaders , requestHeaders , esResponse .getHeaders (), Collections .<String >emptySet ());
385+ assertFalse (esResponse .hasWarnings ());
380386 }
381387 }
382388
389+ public void testDeprecationWarnings () throws IOException {
390+ String chars = randomAsciiAlphanumOfLength (5 );
391+ assertDeprecationWarnings (singletonList ("poorly formatted " + chars ), singletonList ("poorly formatted " + chars ));
392+ assertDeprecationWarnings (singletonList (formatWarning (chars )), singletonList (chars ));
393+ assertDeprecationWarnings (
394+ Arrays .asList (formatWarning (chars ), "another one" , "and another" ),
395+ Arrays .asList (chars , "another one" , "and another" ));
396+
397+ }
398+
399+ private void assertDeprecationWarnings (List <String > warningHeaderTexts , List <String > warningBodyTexts ) throws IOException {
400+ String method = randomFrom (getHttpMethods ());
401+ Request request = new Request (method , "/200" );
402+ RequestOptions .Builder options = request .getOptions ().toBuilder ();
403+ for (String warningHeaderText : warningHeaderTexts ) {
404+ options .addHeader ("Warning" , warningHeaderText );
405+ }
406+ request .setOptions (options );
407+
408+ Response response ;
409+ if (strictDeprecationMode ) {
410+ try {
411+ restClient .performRequest (request );
412+ fail ("expected ResponseException because strict deprecation mode is enabled" );
413+ return ;
414+ } catch (ResponseException e ) {
415+ assertThat (e .getMessage (), containsString ("\n Warnings: " + warningBodyTexts ));
416+ response = e .getResponse ();
417+ }
418+ } else {
419+ response = restClient .performRequest (request );
420+ }
421+ assertTrue (response .hasWarnings ());
422+ assertEquals (warningBodyTexts , response .getWarnings ());
423+ }
424+
425+ /**
426+ * Emulates Elasticsearch's DeprecationLogger.formatWarning in simple
427+ * cases. We don't have that available because we're testing against 1.7.
428+ */
429+ private static String formatWarning (String warningBody ) {
430+ return "299 Elasticsearch-1.2.2-SNAPSHOT-eeeeeee \" " + warningBody + "\" \" Mon, 01 Jan 2001 00:00:00 GMT\" " ;
431+ }
432+
383433 private HttpUriRequest performRandomRequest (String method ) throws Exception {
384434 String uriAsString = "/" + randomStatusCode (getRandom ());
385435 Request request = new Request (method , uriAsString );
0 commit comments