Skip to content

Commit c32284f

Browse files
committed
testGetParserWithInvalidInput
1 parent 47e0524 commit c32284f

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

server/src/test/java/org/elasticsearch/common/xcontent/support/XContentHelperTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
import org.elasticsearch.common.bytes.BytesArray;
1313
import org.elasticsearch.common.bytes.BytesReference;
1414
import org.elasticsearch.common.compress.CompressedXContent;
15+
import org.elasticsearch.common.compress.NotXContentException;
1516
import org.elasticsearch.common.util.Maps;
1617
import org.elasticsearch.common.xcontent.XContentHelper;
1718
import org.elasticsearch.test.ESTestCase;
1819
import org.elasticsearch.xcontent.ToXContent;
1920
import org.elasticsearch.xcontent.ToXContentObject;
2021
import org.elasticsearch.xcontent.XContentBuilder;
22+
import org.elasticsearch.xcontent.XContentParseException;
2123
import org.elasticsearch.xcontent.XContentParser;
2224
import org.elasticsearch.xcontent.XContentParserConfiguration;
2325
import org.elasticsearch.xcontent.XContentType;
@@ -421,4 +423,19 @@ public void testParseToType() throws IOException {
421423

422424
assertThat(names, equalTo(Set.of("a", "c")));
423425
}
426+
427+
public void testGetParserWithInvalidInput() throws IOException {
428+
assertThrows(
429+
"Should detect bad JSON",
430+
NotXContentException.class,
431+
() -> XContentHelper.createParser(XContentParserConfiguration.EMPTY, new BytesArray("not actually XContent"))
432+
);
433+
XContentParser parser = XContentHelper.createParser(
434+
XContentParserConfiguration.EMPTY,
435+
new BytesArray("not actually XContent"),
436+
XContentType.JSON
437+
);
438+
assertNotNull("Should not detect bad JSON", parser); // This is more like assertNotThrows
439+
assertThrows("Should detect bad JSON at parse time", XContentParseException.class, parser::numberValue);
440+
}
424441
}

0 commit comments

Comments
 (0)