Skip to content

Commit

Permalink
LLRC: Test for warnings behavior (#34143)
Browse files Browse the repository at this point in the history
Add tests for the Low Level REST Client's strict deprecation handling.

Relates to #33708
  • Loading branch information
nik9000 committed Oct 6, 2018
1 parent 705113f commit fd03b2a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ public List<String> getWarnings() {
final Matcher matcher = WARNING_HEADER_PATTERN.matcher(warning);
if (matcher.matches()) {
warnings.add(matcher.group(1));
continue;
} else {
warnings.add(warning);
}
warnings.add(warning);
}
return warnings;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -72,10 +73,12 @@
import static org.elasticsearch.client.RestClientTestUtil.randomHttpMethod;
import static org.elasticsearch.client.RestClientTestUtil.randomStatusCode;
import static org.elasticsearch.client.SyncResponseListenerTests.assertExceptionStackContainsCallingMethod;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
Expand All @@ -98,6 +101,7 @@ public class RestClientSingleHostTests extends RestClientTestCase {
private Node node;
private CloseableHttpAsyncClient httpClient;
private HostsTrackingFailureListener failureListener;
private boolean strictDeprecationMode;

@Before
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -149,8 +153,9 @@ public void run() {
defaultHeaders = RestClientTestUtil.randomHeaders(getRandom(), "Header-default");
node = new Node(new HttpHost("localhost", 9200));
failureListener = new HostsTrackingFailureListener();
strictDeprecationMode = randomBoolean();
restClient = new RestClient(httpClient, 10000, defaultHeaders,
singletonList(node), null, failureListener, NodeSelector.ANY, false);
singletonList(node), null, failureListener, NodeSelector.ANY, strictDeprecationMode);
}

/**
Expand Down Expand Up @@ -377,9 +382,54 @@ public void testHeaders() throws IOException {
}
assertThat(esResponse.getStatusLine().getStatusCode(), equalTo(statusCode));
assertHeaders(defaultHeaders, requestHeaders, esResponse.getHeaders(), Collections.<String>emptySet());
assertFalse(esResponse.hasWarnings());
}
}

public void testDeprecationWarnings() throws IOException {
String chars = randomAsciiAlphanumOfLength(5);
assertDeprecationWarnings(singletonList("poorly formatted " + chars), singletonList("poorly formatted " + chars));
assertDeprecationWarnings(singletonList(formatWarning(chars)), singletonList(chars));
assertDeprecationWarnings(
Arrays.asList(formatWarning(chars), "another one", "and another"),
Arrays.asList(chars, "another one", "and another"));

}

private void assertDeprecationWarnings(List<String> warningHeaderTexts, List<String> warningBodyTexts) throws IOException {
String method = randomFrom(getHttpMethods());
Request request = new Request(method, "/200");
RequestOptions.Builder options = request.getOptions().toBuilder();
for (String warningHeaderText : warningHeaderTexts) {
options.addHeader("Warning", warningHeaderText);
}
request.setOptions(options);

Response response;
if (strictDeprecationMode) {
try {
restClient.performRequest(request);
fail("expected ResponseException because strict deprecation mode is enabled");
return;
} catch (ResponseException e) {
assertThat(e.getMessage(), containsString("\nWarnings: " + warningBodyTexts));
response = e.getResponse();
}
} else {
response = restClient.performRequest(request);
}
assertTrue(response.hasWarnings());
assertEquals(warningBodyTexts, response.getWarnings());
}

/**
* Emulates Elasticsearch's DeprecationLogger.formatWarning in simple
* cases. We don't have that available because we're testing against 1.7.
*/
private static String formatWarning(String warningBody) {
return "299 Elasticsearch-1.2.2-SNAPSHOT-eeeeeee \"" + warningBody + "\" \"Mon, 01 Jan 2001 00:00:00 GMT\"";
}

private HttpUriRequest performRandomRequest(String method) throws Exception {
String uriAsString = "/" + randomStatusCode(getRandom());
Request request = new Request(method, uriAsString);
Expand Down

0 comments on commit fd03b2a

Please sign in to comment.