From 7d1e3377b862aebb5b3e5a7253de222d3da3f694 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Tue, 4 Oct 2016 19:47:47 -0400 Subject: [PATCH] Clarify wording for the strict REST params message This commit changes the strict REST parameters message to say that unconsumed parameters are unrecognized rather than unused. Additionally, the test is beefed up to include two unused parameters. Relates #20745 --- .../java/org/elasticsearch/rest/BaseRestHandler.java | 8 +++++++- .../org/elasticsearch/rest/BaseRestHandlerTests.java | 11 +++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/rest/BaseRestHandler.java b/core/src/main/java/org/elasticsearch/rest/BaseRestHandler.java index 10f5359803b..fd5ff846529 100644 --- a/core/src/main/java/org/elasticsearch/rest/BaseRestHandler.java +++ b/core/src/main/java/org/elasticsearch/rest/BaseRestHandler.java @@ -30,6 +30,7 @@ import org.elasticsearch.plugins.ActionPlugin; import java.io.IOException; import java.util.Collections; import java.util.List; +import java.util.Locale; import java.util.Set; import java.util.stream.Collectors; @@ -63,7 +64,12 @@ public abstract class BaseRestHandler extends AbstractComponent implements RestH // validate the non-response params if (!unconsumedParams.isEmpty()) { - throw new IllegalArgumentException("request [" + request.path() + "] contains unused params: " + unconsumedParams.toString()); + final String message = String.format( + Locale.ROOT, + "request [%s] contains unrecognized parameters: %s", + request.path(), + unconsumedParams.toString()); + throw new IllegalArgumentException(message); } // execute the action diff --git a/core/src/test/java/org/elasticsearch/rest/BaseRestHandlerTests.java b/core/src/test/java/org/elasticsearch/rest/BaseRestHandlerTests.java index 6019ab8aacf..fc5af192b74 100644 --- a/core/src/test/java/org/elasticsearch/rest/BaseRestHandlerTests.java +++ b/core/src/test/java/org/elasticsearch/rest/BaseRestHandlerTests.java @@ -33,6 +33,7 @@ import java.util.HashMap; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; +import static org.hamcrest.core.AnyOf.anyOf; import static org.hamcrest.core.StringContains.containsString; import static org.hamcrest.object.HasToString.hasToString; import static org.mockito.Mockito.mock; @@ -51,12 +52,18 @@ public class BaseRestHandlerTests extends ESTestCase { final HashMap params = new HashMap<>(); params.put("consumed", randomAsciiOfLength(8)); - params.put("unconsumed", randomAsciiOfLength(8)); + params.put("unconsumed-first", randomAsciiOfLength(8)); + params.put("unconsumed-second", randomAsciiOfLength(8)); RestRequest request = new FakeRestRequest.Builder().withParams(params).build(); RestChannel channel = new FakeRestChannel(request, randomBoolean(), 1); final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> handler.handleRequest(request, channel, mock(NodeClient.class))); - assertThat(e, hasToString(containsString("request [/] contains unused params: [unconsumed]"))); + assertThat( + e, + // we can not rely on ordering of the unconsumed parameters here + anyOf( + hasToString(containsString("request [/] contains unrecognized parameters: [unconsumed-first, unconsumed-second]")), + hasToString(containsString("request [/] contains unrecognized parameters: [unconsumed-second, unconsumed-first]")))); assertFalse(executed.get()); }