Skip to content

Commit fd03b2a

Browse files
committed
LLRC: Test for warnings behavior (#34143)
Add tests for the Low Level REST Client's strict deprecation handling. Relates to #33708
1 parent 705113f commit fd03b2a

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

client/rest/src/main/java/org/elasticsearch/client/Response.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ public List<String> getWarnings() {
124124
final Matcher matcher = WARNING_HEADER_PATTERN.matcher(warning);
125125
if (matcher.matches()) {
126126
warnings.add(matcher.group(1));
127-
continue;
127+
} else {
128+
warnings.add(warning);
128129
}
129-
warnings.add(warning);
130130
}
131131
return warnings;
132132
}

client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostTests.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import java.util.Collections;
6161
import java.util.HashSet;
6262
import java.util.Map;
63+
import java.util.List;
6364
import java.util.Set;
6465
import java.util.concurrent.ExecutorService;
6566
import java.util.concurrent.Executors;
@@ -72,10 +73,12 @@
7273
import static org.elasticsearch.client.RestClientTestUtil.randomHttpMethod;
7374
import static org.elasticsearch.client.RestClientTestUtil.randomStatusCode;
7475
import static org.elasticsearch.client.SyncResponseListenerTests.assertExceptionStackContainsCallingMethod;
76+
import static org.hamcrest.CoreMatchers.containsString;
7577
import static org.hamcrest.CoreMatchers.equalTo;
7678
import static org.hamcrest.CoreMatchers.instanceOf;
7779
import static org.junit.Assert.assertArrayEquals;
7880
import static org.junit.Assert.assertEquals;
81+
import static org.junit.Assert.assertFalse;
7982
import static org.junit.Assert.assertThat;
8083
import static org.junit.Assert.assertTrue;
8184
import 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("\nWarnings: " + 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

Comments
 (0)