Skip to content

Commit ab0c624

Browse files
nik9000kcm
authored andcommitted
LLRC: Test for warnings behavior (#34143)
Add tests for the Low Level REST Client's strict deprecation handling. Relates to #33708
1 parent 6f6972e commit ab0c624

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
@@ -59,6 +59,7 @@
5959
import java.util.Arrays;
6060
import java.util.Collections;
6161
import java.util.HashSet;
62+
import java.util.List;
6263
import java.util.Set;
6364
import java.util.concurrent.ExecutorService;
6465
import java.util.concurrent.Executors;
@@ -70,10 +71,12 @@
7071
import static org.elasticsearch.client.RestClientTestUtil.getOkStatusCodes;
7172
import static org.elasticsearch.client.RestClientTestUtil.randomStatusCode;
7273
import static org.elasticsearch.client.SyncResponseListenerTests.assertExceptionStackContainsCallingMethod;
74+
import static org.hamcrest.CoreMatchers.containsString;
7375
import static org.hamcrest.CoreMatchers.equalTo;
7476
import static org.hamcrest.CoreMatchers.instanceOf;
7577
import static org.junit.Assert.assertArrayEquals;
7678
import static org.junit.Assert.assertEquals;
79+
import static org.junit.Assert.assertFalse;
7780
import static org.junit.Assert.assertThat;
7881
import static org.junit.Assert.assertTrue;
7982
import 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("\nWarnings: " + 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

Comments
 (0)