From 8e033fbd825f0bdd68ebf4346e824311da365e6d Mon Sep 17 00:00:00 2001 From: Andrea Romanello aka rommel Date: Tue, 6 Feb 2024 16:31:06 +0100 Subject: [PATCH 1/3] add testcase for decodeText module --- test/decode-text.test.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 test/decode-text.test.js diff --git a/test/decode-text.test.js b/test/decode-text.test.js new file mode 100644 index 0000000..fceee9b --- /dev/null +++ b/test/decode-text.test.js @@ -0,0 +1,32 @@ +// FILEPATH: /home/rommel/workspace/contributions/busboy/test/decodeText.test.js + +const { test } = require('tap') +const decodeText = require('../lib/utils/decodeText') + +test('decodeText', t => { + const testCases = [ + { description: 'UTF-8 encoding', text: Buffer.from('Hello, World!', 'utf8'), initialCharset: 'utf8', outputCharset: 'utf8', expected: 'Hello, World!' }, + { description: 'UTF-8 encoding empty', text: Buffer.from('', 'utf8'), initialCharset: 'utf8', outputCharset: 'utf8', expected: '' }, + { description: 'Latin1 encoding', text: Buffer.from('Hello, World!', 'latin1'), initialCharset: 'latin1', outputCharset: 'latin1', expected: 'Hello, World!' }, + { description: 'Latin1 encoding empty', text: Buffer.from('', 'latin1'), initialCharset: 'latin1', outputCharset: 'latin1', expected: '' }, + { description: 'UTF-16LE encoding', text: Buffer.from('Hello, World!', 'utf16le'), initialCharset: 'utf16le', outputCharset: 'utf16le', expected: 'Hello, World!' }, + { description: 'UTF-8 to UTF-16LE encoding', text: 'Hello, World!', initialCharset: 'utf16le', outputCharset: 'utf16le', expected: 'Hello, World!' }, + { description: 'UTF-16LE encoding empty', text: Buffer.from('', 'utf16le'), initialCharset: 'utf16le', outputCharset: 'utf16le', expected: '' }, + { description: 'Base64 encoding', text: Buffer.from('Hello, World!').toString('base64'), initialCharset: 'base64', outputCharset: 'base64', expected: 'SGVsbG8sIFdvcmxkIQ==' }, + { description: 'UTF-8 to Base64 encoding', text: 'Hello, World!', initialCharset: 'utf8', outputCharset: 'base64', expected: 'SGVsbG8sIFdvcmxkIQ==' }, + { description: 'Base64 encoding empty', text: Buffer.from('', 'utf8'), initialCharset: 'utf8', outputCharset: 'base64', expected: '' }, + { description: 'UTF8 to base64', text: Buffer.from('Hello, World!', 'utf-8'), initialCharset: 'utf8', outputCharset: 'base64', expected: 'SGVsbG8sIFdvcmxkIQ==' }, + { description: 'Unknown', text: 'Hello, World!', initialCharset: 'utf8', outputCharset: 'other', expected: 'Hello, World!' }, + { description: 'Unknown empty', text: Buffer.from('', 'utf8'), initialCharset: 'utf8', outputCharset: 'other', expected: '' }, + { description: 'Unknown', text: 'Hello, World!', initialCharset: 'utf8', outputCharset: 'utf-8', expected: 'Hello, World!' } + ] + + t.plan(testCases.length) + + testCases.forEach((c, index) => { + t.test(c.description, t => { + t.plan(1) + t.equal(decodeText(c.text, c.initialCharset, c.outputCharset), c.expected, `Test case ${index + 1}`) + }) + }) +}) From 2ac775d82a0ed59c9d05770d99883b9b627e19ae Mon Sep 17 00:00:00 2001 From: Andrea Romanello aka rommel Date: Tue, 6 Feb 2024 17:23:48 +0100 Subject: [PATCH 2/3] add strict remove and remove some comment --- test/decode-text.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/decode-text.test.js b/test/decode-text.test.js index fceee9b..09ea031 100644 --- a/test/decode-text.test.js +++ b/test/decode-text.test.js @@ -1,4 +1,4 @@ -// FILEPATH: /home/rommel/workspace/contributions/busboy/test/decodeText.test.js +'use strict' const { test } = require('tap') const decodeText = require('../lib/utils/decodeText') From ee08c92ba3bad46d5179b8d8568a73138597797d Mon Sep 17 00:00:00 2001 From: Andrea Romanello aka rommel Date: Tue, 6 Feb 2024 17:31:34 +0100 Subject: [PATCH 3/3] add test case with utf16 char --- test/decode-text.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/decode-text.test.js b/test/decode-text.test.js index 09ea031..5723fe9 100644 --- a/test/decode-text.test.js +++ b/test/decode-text.test.js @@ -10,6 +10,7 @@ test('decodeText', t => { { description: 'Latin1 encoding', text: Buffer.from('Hello, World!', 'latin1'), initialCharset: 'latin1', outputCharset: 'latin1', expected: 'Hello, World!' }, { description: 'Latin1 encoding empty', text: Buffer.from('', 'latin1'), initialCharset: 'latin1', outputCharset: 'latin1', expected: '' }, { description: 'UTF-16LE encoding', text: Buffer.from('Hello, World!', 'utf16le'), initialCharset: 'utf16le', outputCharset: 'utf16le', expected: 'Hello, World!' }, + { description: 'UTF-16LE encoding with special char', text: Buffer.from('Hello, World! 🌍🚀', 'utf16le'), initialCharset: 'utf16le', outputCharset: 'utf16le', expected: 'Hello, World! 🌍🚀' }, { description: 'UTF-8 to UTF-16LE encoding', text: 'Hello, World!', initialCharset: 'utf16le', outputCharset: 'utf16le', expected: 'Hello, World!' }, { description: 'UTF-16LE encoding empty', text: Buffer.from('', 'utf16le'), initialCharset: 'utf16le', outputCharset: 'utf16le', expected: '' }, { description: 'Base64 encoding', text: Buffer.from('Hello, World!').toString('base64'), initialCharset: 'base64', outputCharset: 'base64', expected: 'SGVsbG8sIFdvcmxkIQ==' },