|
19 | 19 |
|
20 | 20 | package org.elasticsearch.rest.action; |
21 | 21 |
|
| 22 | +import com.fasterxml.jackson.core.io.JsonEOFException; |
| 23 | +import java.util.Arrays; |
22 | 24 | import org.elasticsearch.common.ParsingException; |
23 | 25 | import org.elasticsearch.common.settings.Settings; |
24 | 26 | import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
@@ -59,10 +61,32 @@ public void testParseTopLevelBuilder() throws IOException { |
59 | 61 | } |
60 | 62 |
|
61 | 63 | public void testParseTopLevelBuilderEmptyObject() throws IOException { |
62 | | - String requestBody = "{}"; |
63 | | - try (XContentParser parser = createParser(JsonXContent.jsonXContent, requestBody)) { |
64 | | - QueryBuilder query = RestActions.getQueryContent(parser); |
65 | | - assertNull(query); |
| 64 | + for (String requestBody : Arrays.asList("{}", "")) { |
| 65 | + try (XContentParser parser = createParser(JsonXContent.jsonXContent, requestBody)) { |
| 66 | + QueryBuilder query = RestActions.getQueryContent(parser); |
| 67 | + assertNull(query); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public void testParseTopLevelBuilderMalformedJson() throws IOException { |
| 73 | + for (String requestBody : Arrays.asList("\"\"", "\"someString\"", "\"{\"")) { |
| 74 | + try (XContentParser parser = createParser(JsonXContent.jsonXContent, requestBody)) { |
| 75 | + ParsingException exception = |
| 76 | + expectThrows(ParsingException.class, () -> RestActions.getQueryContent(parser)); |
| 77 | + assertEquals("Expected [START_OBJECT] but found [VALUE_STRING]", exception.getMessage()); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public void testParseTopLevelBuilderIncompleteJson() throws IOException { |
| 83 | + for (String requestBody : Arrays.asList("{", "{ \"query\" :")) { |
| 84 | + try (XContentParser parser = createParser(JsonXContent.jsonXContent, requestBody)) { |
| 85 | + ParsingException exception = |
| 86 | + expectThrows(ParsingException.class, () -> RestActions.getQueryContent(parser)); |
| 87 | + assertEquals("Failed to parse", exception.getMessage()); |
| 88 | + assertEquals(JsonEOFException.class, exception.getRootCause().getClass()); |
| 89 | + } |
66 | 90 | } |
67 | 91 | } |
68 | 92 |
|
|
0 commit comments