diff --git a/x-pack/plugins/lists/server/services/items/buffer_lines.test.ts b/x-pack/plugins/lists/server/services/items/buffer_lines.test.ts index 509233d006b73..7faef72c38907 100644 --- a/x-pack/plugins/lists/server/services/items/buffer_lines.test.ts +++ b/x-pack/plugins/lists/server/services/items/buffer_lines.test.ts @@ -23,9 +23,15 @@ describe('buffer_lines', () => { }).toThrow('bufferSize must be greater than zero'); }); - test('it can read a single line', (done) => { + test('two identical lines are collapsed into just one line without duplicates', (done) => { const input = new TestReadable(); + input.push('--boundary\n'); + input.push('Content-type: text/plain\n'); + input.push('Content-Disposition: form-data; name="fieldName"; filename="filename.text"\n'); + input.push('\n'); + input.push('line one\n'); input.push('line one\n'); + input.push('--boundary--\n'); input.push(null); const bufferedLine = new BufferLines({ bufferSize: IMPORT_BUFFER_SIZE, input }); let linesToTest: string[] = []; @@ -38,25 +44,8 @@ describe('buffer_lines', () => { }); }); - test('it can read a single line using a buffer size of 1', (done) => { + test('it can close out without writing any lines', (done) => { const input = new TestReadable(); - input.push('line one\n'); - input.push(null); - const bufferedLine = new BufferLines({ bufferSize: 1, input }); - let linesToTest: string[] = []; - bufferedLine.on('lines', (lines: string[]) => { - linesToTest = [...linesToTest, ...lines]; - }); - bufferedLine.on('close', () => { - expect(linesToTest).toEqual(['line one']); - done(); - }); - }); - - test('it can read two lines', (done) => { - const input = new TestReadable(); - input.push('line one\n'); - input.push('line two\n'); input.push(null); const bufferedLine = new BufferLines({ bufferSize: IMPORT_BUFFER_SIZE, input }); let linesToTest: string[] = []; @@ -64,74 +53,56 @@ describe('buffer_lines', () => { linesToTest = [...linesToTest, ...lines]; }); bufferedLine.on('close', () => { - expect(linesToTest).toEqual(['line one', 'line two']); - done(); - }); - }); - - test('it can read two lines using a buffer size of 1', (done) => { - const input = new TestReadable(); - input.push('line one\n'); - input.push('line two\n'); - input.push(null); - const bufferedLine = new BufferLines({ bufferSize: 1, input }); - let linesToTest: string[] = []; - bufferedLine.on('lines', (lines: string[]) => { - linesToTest = [...linesToTest, ...lines]; - }); - bufferedLine.on('close', () => { - expect(linesToTest).toEqual(['line one', 'line two']); + expect(linesToTest).toEqual([]); done(); }); }); - test('two identical lines are collapsed into just one line without duplicates', (done) => { + test('it can read 200 lines', (done) => { const input = new TestReadable(); - input.push('line one\n'); - input.push('line one\n'); - input.push(null); const bufferedLine = new BufferLines({ bufferSize: IMPORT_BUFFER_SIZE, input }); + input.push('--boundary\n'); + input.push('Content-type: text/plain\n'); + input.push('Content-Disposition: form-data; name="fieldName"; filename="filename.text"\n'); + input.push('\n'); let linesToTest: string[] = []; - bufferedLine.on('lines', (lines: string[]) => { - linesToTest = [...linesToTest, ...lines]; - }); - bufferedLine.on('close', () => { - expect(linesToTest).toEqual(['line one']); - done(); - }); - }); - - test('it can close out without writing any lines', (done) => { - const input = new TestReadable(); + const size200: string[] = new Array(200).fill(null).map((_, index) => `${index}\n`); + size200.forEach((element) => input.push(element)); + input.push('--boundary--\n'); input.push(null); - const bufferedLine = new BufferLines({ bufferSize: IMPORT_BUFFER_SIZE, input }); - let linesToTest: string[] = []; bufferedLine.on('lines', (lines: string[]) => { linesToTest = [...linesToTest, ...lines]; }); bufferedLine.on('close', () => { - expect(linesToTest).toEqual([]); + expect(linesToTest.length).toEqual(200); done(); }); }); - test('it can read 200 lines', (done) => { + test('it can read an example multi-part message', (done) => { const input = new TestReadable(); + input.push('--boundary\n'); + input.push('Content-type: text/plain\n'); + input.push('Content-Disposition: form-data; name="fieldName"; filename="filename.text"\n'); + input.push('\n'); + input.push('127.0.0.1\n'); + input.push('127.0.0.2\n'); + input.push('127.0.0.3\n'); + input.push('\n'); + input.push('--boundary--\n'); + input.push(null); const bufferedLine = new BufferLines({ bufferSize: IMPORT_BUFFER_SIZE, input }); let linesToTest: string[] = []; - const size200: string[] = new Array(200).fill(null).map((_, index) => `${index}\n`); - size200.forEach((element) => input.push(element)); - input.push(null); bufferedLine.on('lines', (lines: string[]) => { linesToTest = [...linesToTest, ...lines]; }); bufferedLine.on('close', () => { - expect(linesToTest.length).toEqual(200); + expect(linesToTest).toEqual(['127.0.0.1', '127.0.0.2', '127.0.0.3']); done(); }); }); - test('it can read an example multi-part message', (done) => { + test('it does not create empty values for lines after the end boundary', (done) => { const input = new TestReadable(); input.push('--boundary\n'); input.push('Content-type: text/plain\n'); @@ -142,6 +113,7 @@ describe('buffer_lines', () => { input.push('127.0.0.3\n'); input.push('\n'); input.push('--boundary--\n'); + input.push('\n'); input.push(null); const bufferedLine = new BufferLines({ bufferSize: IMPORT_BUFFER_SIZE, input }); let linesToTest: string[] = []; diff --git a/x-pack/plugins/lists/server/services/items/buffer_lines.ts b/x-pack/plugins/lists/server/services/items/buffer_lines.ts index cb6d073010127..e179fc4358468 100644 --- a/x-pack/plugins/lists/server/services/items/buffer_lines.ts +++ b/x-pack/plugins/lists/server/services/items/buffer_lines.ts @@ -48,7 +48,7 @@ export class BufferLines extends Readable { // we are at the end of the stream this.boundary = null; this.readableText = false; - } else { + } else if (this.readableText) { // we have actual content to push this.push(line); } diff --git a/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.test.ts b/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.test.ts index 78098fde59827..89c3d89fe631e 100644 --- a/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.test.ts +++ b/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.test.ts @@ -49,6 +49,12 @@ describe('write_lines_to_bulk_list_items', () => { test('It imports a set of items to a write buffer by calling "getListItemByValues" with a single value given', async () => { const options = getImportListItemsToStreamOptionsMock(); const promise = importListItemsToStream(options); + options.stream.push('--boundary\n'); + options.stream.push('Content-type: text/plain\n'); + options.stream.push( + 'Content-Disposition: form-data; name="fieldName"; filename="filename.text"\n' + ); + options.stream.push('\n'); options.stream.push('127.0.0.1\n'); options.stream.push(null); await promise; @@ -58,6 +64,12 @@ describe('write_lines_to_bulk_list_items', () => { test('It imports a set of items to a write buffer by calling "getListItemByValues" with two values given', async () => { const options = getImportListItemsToStreamOptionsMock(); const promise = importListItemsToStream(options); + options.stream.push('--boundary\n'); + options.stream.push('Content-type: text/plain\n'); + options.stream.push( + 'Content-Disposition: form-data; name="fieldName"; filename="filename.text"\n' + ); + options.stream.push('\n'); options.stream.push('127.0.0.1\n'); options.stream.push('127.0.0.2\n'); options.stream.push(null);