99
1010package org .elasticsearch .http ;
1111
12+ import org .apache .http .entity .ByteArrayEntity ;
13+ import org .apache .http .entity .ContentType ;
1214import org .elasticsearch .action .bulk .IncrementalBulkService ;
1315import org .elasticsearch .client .Request ;
1416import org .elasticsearch .client .Response ;
1921import org .elasticsearch .xcontent .json .JsonXContent ;
2022
2123import java .io .IOException ;
24+ import java .nio .charset .StandardCharsets ;
2225import java .util .List ;
2326import java .util .Map ;
2427
28+ import static org .elasticsearch .rest .RestStatus .BAD_REQUEST ;
2529import static org .elasticsearch .rest .RestStatus .OK ;
2630import static org .hamcrest .CoreMatchers .containsString ;
2731import static org .hamcrest .Matchers .equalTo ;
2832
2933@ ESIntegTestCase .ClusterScope (scope = ESIntegTestCase .Scope .SUITE , supportsDedicatedMasters = false , numDataNodes = 2 , numClientNodes = 0 )
30- public class IncrementalBulkRestIT extends HttpSmokeTestCase {
34+ public class BulkRestIT extends HttpSmokeTestCase {
3135
3236 @ Override
3337 protected Settings nodeSettings (int nodeOrdinal , Settings otherSettings ) {
3438 return Settings .builder ()
3539 .put (super .nodeSettings (nodeOrdinal , otherSettings ))
36- .put (IncrementalBulkService .INCREMENTAL_BULK .getKey (), true )
40+ .put (IncrementalBulkService .INCREMENTAL_BULK .getKey (), seventyFivePercentOfTheTime () )
3741 .build ();
3842 }
3943
44+ private static boolean seventyFivePercentOfTheTime () {
45+ return (randomBoolean () && randomBoolean ()) == false ;
46+ }
47+
4048 public void testBulkUriMatchingDoesNotMatchBulkCapabilitiesApi () throws IOException {
4149 Request request = new Request ("GET" , "/_capabilities?method=GET&path=%2F_bulk&capabilities=failure_store_status&pretty" );
4250 Response response = getRestClient ().performRequest (request );
@@ -51,6 +59,26 @@ public void testBulkMissingBody() throws IOException {
5159 assertThat (responseException .getMessage (), containsString ("request body is required" ));
5260 }
5361
62+ public void testBulkInvalidIndexNameString () throws IOException {
63+ Request request = new Request ("POST" , "/_bulk" );
64+
65+ byte [] bytes1 = "{\" create\" :{\" _index\" :\" " .getBytes (StandardCharsets .UTF_8 );
66+ byte [] bytes2 = new byte [] { (byte ) 0xfe , (byte ) 0xfe , (byte ) 0xff , (byte ) 0xff };
67+ byte [] bytes3 = "\" ,\" _id\" :\" 1\" }}\n {\" field\" :1}\n \r \n " .getBytes (StandardCharsets .UTF_8 );
68+ byte [] bulkBody = new byte [bytes1 .length + bytes2 .length + bytes3 .length ];
69+ System .arraycopy (bytes1 , 0 , bulkBody , 0 , bytes1 .length );
70+ System .arraycopy (bytes2 , 0 , bulkBody , bytes1 .length , bytes2 .length );
71+ System .arraycopy (bytes3 , 0 , bulkBody , bytes1 .length + bytes2 .length , bytes3 .length );
72+
73+ request .setEntity (new ByteArrayEntity (bulkBody , ContentType .APPLICATION_JSON ));
74+
75+ ResponseException responseException = expectThrows (ResponseException .class , () -> getRestClient ().performRequest (request ));
76+ assertThat (responseException .getResponse ().getStatusLine ().getStatusCode (), equalTo (BAD_REQUEST .getStatus ()));
77+ assertThat (responseException .getMessage (), containsString ("could not parse bulk request body" ));
78+ assertThat (responseException .getMessage (), containsString ("json_parse_exception" ));
79+ assertThat (responseException .getMessage (), containsString ("Invalid UTF-8" ));
80+ }
81+
5482 public void testBulkRequestBodyImproperlyTerminated () throws IOException {
5583 Request request = new Request (randomBoolean () ? "POST" : "PUT" , "/_bulk" );
5684 // missing final line of the bulk body. cannot process
@@ -61,10 +89,10 @@ public void testBulkRequestBodyImproperlyTerminated() throws IOException {
6189 );
6290 ResponseException responseException = expectThrows (ResponseException .class , () -> getRestClient ().performRequest (request ));
6391 assertEquals (400 , responseException .getResponse ().getStatusLine ().getStatusCode ());
64- assertThat (responseException .getMessage (), containsString ("could not parse bulk request body " ));
92+ assertThat (responseException .getMessage (), containsString ("The bulk request must be terminated by a newline " ));
6593 }
6694
67- public void testIncrementalBulk () throws IOException {
95+ public void testBulkRequest () throws IOException {
6896 Request createRequest = new Request ("PUT" , "/index_name" );
6997 createRequest .setJsonEntity ("""
7098 {
@@ -81,7 +109,6 @@ public void testIncrementalBulk() throws IOException {
81109
82110 Request firstBulkRequest = new Request ("POST" , "/index_name/_bulk" );
83111
84- // index documents for the rollup job
85112 String bulkBody = "{\" index\" :{\" _index\" :\" index_name\" ,\" _id\" :\" 1\" }}\n "
86113 + "{\" field\" :1}\n "
87114 + "{\" index\" :{\" _index\" :\" index_name\" ,\" _id\" :\" 2\" }}\n "
@@ -113,7 +140,6 @@ public void testBulkWithIncrementalDisabled() throws IOException {
113140
114141 Request firstBulkRequest = new Request ("POST" , "/index_name/_bulk" );
115142
116- // index documents for the rollup job
117143 String bulkBody = "{\" index\" :{\" _index\" :\" index_name\" ,\" _id\" :\" 1\" }}\n "
118144 + "{\" field\" :1}\n "
119145 + "{\" index\" :{\" _index\" :\" index_name\" ,\" _id\" :\" 2\" }}\n "
@@ -137,7 +163,7 @@ public void testBulkWithIncrementalDisabled() throws IOException {
137163 }
138164 }
139165
140- public void testIncrementalMalformed () throws IOException {
166+ public void testMalformedActionLineBulk () throws IOException {
141167 Request createRequest = new Request ("PUT" , "/index_name" );
142168 createRequest .setJsonEntity ("""
143169 {
@@ -154,7 +180,6 @@ public void testIncrementalMalformed() throws IOException {
154180
155181 Request bulkRequest = new Request ("POST" , "/index_name/_bulk" );
156182
157- // index documents for the rollup job
158183 final StringBuilder bulk = new StringBuilder ();
159184 bulk .append ("{\" index\" :{\" _index\" :\" index_name\" }}\n " );
160185 bulk .append ("{\" field\" :1}\n " );
@@ -170,7 +195,6 @@ public void testIncrementalMalformed() throws IOException {
170195 private static void sendLargeBulk () throws IOException {
171196 Request bulkRequest = new Request ("POST" , "/index_name/_bulk" );
172197
173- // index documents for the rollup job
174198 final StringBuilder bulk = new StringBuilder ();
175199 bulk .append ("{\" delete\" :{\" _index\" :\" index_name\" ,\" _id\" :\" 1\" }}\n " );
176200 int updates = 0 ;
0 commit comments