|
12 | 12 | import org.elasticsearch.common.bytes.BytesArray; |
13 | 13 | import org.elasticsearch.common.bytes.BytesReference; |
14 | 14 | import org.elasticsearch.common.compress.CompressedXContent; |
| 15 | +import org.elasticsearch.common.compress.NotXContentException; |
15 | 16 | import org.elasticsearch.common.util.Maps; |
16 | 17 | import org.elasticsearch.common.xcontent.XContentHelper; |
17 | 18 | import org.elasticsearch.test.ESTestCase; |
18 | 19 | import org.elasticsearch.xcontent.ToXContent; |
19 | 20 | import org.elasticsearch.xcontent.ToXContentObject; |
20 | 21 | import org.elasticsearch.xcontent.XContentBuilder; |
| 22 | +import org.elasticsearch.xcontent.XContentParseException; |
21 | 23 | import org.elasticsearch.xcontent.XContentParser; |
22 | 24 | import org.elasticsearch.xcontent.XContentParserConfiguration; |
23 | 25 | import org.elasticsearch.xcontent.XContentType; |
@@ -421,4 +423,19 @@ public void testParseToType() throws IOException { |
421 | 423 |
|
422 | 424 | assertThat(names, equalTo(Set.of("a", "c"))); |
423 | 425 | } |
| 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 | + } |
424 | 441 | } |
0 commit comments