|
@@ -25,6 +25,7 @@ import org.elasticsearch.action.search.SearchPhaseExecutionException;
|
|
|
import org.elasticsearch.action.search.ShardSearchFailure;
|
|
|
import org.elasticsearch.common.ParsingException;
|
|
|
import org.elasticsearch.index.Index;
|
|
|
+import org.elasticsearch.rest.support.RestUtils;
|
|
|
import org.elasticsearch.search.SearchShardTarget;
|
|
|
import org.elasticsearch.test.ESTestCase;
|
|
|
import org.elasticsearch.test.rest.FakeRestRequest;
|
|
@@ -35,8 +36,11 @@ import java.io.IOException;
|
|
|
|
|
|
import static org.hamcrest.Matchers.contains;
|
|
|
import static org.hamcrest.Matchers.containsString;
|
|
|
+import static org.hamcrest.Matchers.equalTo;
|
|
|
import static org.hamcrest.Matchers.not;
|
|
|
import static org.hamcrest.Matchers.notNullValue;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
|
|
|
/**
|
|
|
*
|
|
@@ -147,6 +151,32 @@ public class BytesRestResponseTests extends ESTestCase {
|
|
|
assertTrue(stackTrace.contains("Caused by: ParsingException[foobar]"));
|
|
|
}
|
|
|
|
|
|
+ public void testResponseWhenPathContainsEncodingError() throws IOException {
|
|
|
+ final String path = "%a";
|
|
|
+ final RestRequest request = mock(RestRequest.class);
|
|
|
+ when(request.rawPath()).thenReturn(path);
|
|
|
+ final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> RestUtils.decodeComponent(request.rawPath()));
|
|
|
+ final RestChannel channel = new DetailedExceptionRestChannel(request);
|
|
|
+ // if we try to decode the path, this will throw an IllegalArgumentException again
|
|
|
+ final BytesRestResponse response = new BytesRestResponse(channel, e);
|
|
|
+ assertNotNull(response.content());
|
|
|
+ final String content = response.content().toUtf8();
|
|
|
+ assertThat(content, containsString("\"type\":\"illegal_argument_exception\""));
|
|
|
+ assertThat(content, containsString("\"reason\":\"partial escape sequence at end of string: %a\""));
|
|
|
+ assertThat(content, containsString("\"status\":" + 400));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testResponseWhenInternalServerError() throws IOException {
|
|
|
+ final RestRequest request = new FakeRestRequest();
|
|
|
+ final RestChannel channel = new DetailedExceptionRestChannel(request);
|
|
|
+ final BytesRestResponse response = new BytesRestResponse(channel, new ElasticsearchException("simulated"));
|
|
|
+ assertNotNull(response.content());
|
|
|
+ final String content = response.content().toUtf8();
|
|
|
+ assertThat(content, containsString("\"type\":\"exception\""));
|
|
|
+ assertThat(content, containsString("\"reason\":\"simulated\""));
|
|
|
+ assertThat(content, containsString("\"status\":" + 500));
|
|
|
+ }
|
|
|
+
|
|
|
public static class WithHeadersException extends ElasticsearchException {
|
|
|
|
|
|
WithHeadersException() {
|