|
67 | 67 | import org.elasticsearch.common.io.stream.NamedWriteableRegistry; |
68 | 68 | import org.elasticsearch.common.settings.Settings; |
69 | 69 | import org.elasticsearch.common.util.concurrent.ThreadContext; |
| 70 | +import org.elasticsearch.index.IndexSettings; |
| 71 | +import org.elasticsearch.index.mapper.DocumentParsingException; |
70 | 72 | import org.elasticsearch.index.query.QueryBuilders; |
71 | 73 | import org.elasticsearch.indices.TestIndexNameExpressionResolver; |
72 | 74 | import org.elasticsearch.plugins.NetworkPlugin; |
|
83 | 85 | import org.elasticsearch.transport.TransportInterceptor; |
84 | 86 | import org.elasticsearch.transport.TransportRequest; |
85 | 87 | import org.elasticsearch.transport.TransportRequestHandler; |
| 88 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 89 | +import org.elasticsearch.xcontent.XContentFactory; |
86 | 90 | import org.junit.After; |
87 | 91 | import org.junit.Before; |
88 | 92 |
|
89 | 93 | import java.util.ArrayList; |
90 | 94 | import java.util.Arrays; |
| 95 | +import java.util.Base64; |
91 | 96 | import java.util.Collection; |
92 | 97 | import java.util.Collections; |
93 | 98 | import java.util.HashMap; |
|
100 | 105 |
|
101 | 106 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
102 | 107 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailuresAndResponse; |
| 108 | +import static org.hamcrest.Matchers.containsString; |
103 | 109 | import static org.hamcrest.Matchers.emptyIterable; |
104 | 110 | import static org.hamcrest.Matchers.equalTo; |
105 | 111 | import static org.hamcrest.Matchers.greaterThan; |
@@ -582,6 +588,27 @@ public void testSearchDfsQueryThenFetch() throws Exception { |
582 | 588 | ); |
583 | 589 | } |
584 | 590 |
|
| 591 | + public void testRejectDocumentWithSourceExceedingSizeLimit() throws Exception { |
| 592 | + String indexName = "source-limit-test"; |
| 593 | + int byteLimit = 10; |
| 594 | + |
| 595 | + assertAcked( |
| 596 | + prepareCreate(indexName).setSettings( |
| 597 | + Settings.builder().put(IndexSettings.MAX_SOURCE_SIZE_BYTES_SETTING.getKey(), byteLimit).build() |
| 598 | + ) |
| 599 | + ); |
| 600 | + |
| 601 | + byte[] data = new byte[80]; |
| 602 | + try (XContentBuilder doc = XContentFactory.jsonBuilder()) { |
| 603 | + doc.startObject(); |
| 604 | + doc.field("value", Base64.getEncoder().encodeToString(data)); |
| 605 | + doc.endObject(); |
| 606 | + |
| 607 | + Exception e = expectThrows(DocumentParsingException.class, () -> { client().prepareIndex(indexName).setSource(doc).get(); }); |
| 608 | + assertThat(e.getMessage(), containsString("failed to parse, document too large. Max allowed is " + byteLimit + " bytes")); |
| 609 | + } |
| 610 | + } |
| 611 | + |
585 | 612 | private static void assertSameIndices(IndicesRequest originalRequest, String... actions) { |
586 | 613 | assertSameIndices(originalRequest, false, actions); |
587 | 614 | } |
|
0 commit comments