diff --git a/test/parallel/test-buffer-bytelength.js b/test/parallel/test-buffer-bytelength.js index b14047893370f2..09e59743e40208 100644 --- a/test/parallel/test-buffer-bytelength.js +++ b/test/parallel/test-buffer-bytelength.js @@ -106,3 +106,12 @@ assert.strictEqual(Buffer.byteLength('Il était tué', 'utf8'), 14); // Test that ArrayBuffer from a different context is detected correctly const arrayBuf = vm.runInNewContext('new ArrayBuffer()'); assert.strictEqual(Buffer.byteLength(arrayBuf), 0); + +// Verify that invalid encodings are treated as utf8 +for (let i = 1; i < 10; i++) { + const encoding = String(i).repeat(i); + + assert.ok(!Buffer.isEncoding(encoding)); + assert.strictEqual(Buffer.byteLength('foo', encoding), + Buffer.byteLength('foo', 'utf8')); +} diff --git a/test/parallel/test-buffer-tostring.js b/test/parallel/test-buffer-tostring.js index 776b7c089224ed..214d1013e9896c 100644 --- a/test/parallel/test-buffer-tostring.js +++ b/test/parallel/test-buffer-tostring.js @@ -4,8 +4,8 @@ require('../common'); const assert = require('assert'); // utf8, ucs2, ascii, latin1, utf16le -const encodings = ['utf8', 'ucs2', 'ucs-2', 'ascii', 'latin1', 'binary', - 'utf16le', 'utf-16le']; +const encodings = ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1', + 'binary', 'utf16le', 'utf-16le']; encodings .reduce((es, e) => es.concat(e, e.toUpperCase()), []) @@ -23,3 +23,12 @@ encodings assert.strictEqual(Buffer.from('666f6f', encoding).toString(encoding), '666f6f'); }); + +// Invalid encodings +for (let i = 1; i < 10; i++) { + const encoding = String(i).repeat(i); + const error = new RegExp(`^TypeError: Unknown encoding: ${encoding}$`); + + assert.ok(!Buffer.isEncoding(encoding)); + assert.throws(() => Buffer.from('foo').toString(encoding), error); +} diff --git a/test/parallel/test-buffer-write.js b/test/parallel/test-buffer-write.js index 80c95dda4471e7..f278b92cce957c 100644 --- a/test/parallel/test-buffer-write.js +++ b/test/parallel/test-buffer-write.js @@ -53,3 +53,12 @@ encodings assert.strictEqual(buf.write('666f6f', 0, len, encoding), len); assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase())); }); + +// Invalid encodings +for (let i = 1; i < 10; i++) { + const encoding = String(i).repeat(i); + const error = new RegExp(`^TypeError: Unknown encoding: ${encoding}$`); + + assert.ok(!Buffer.isEncoding(encoding)); + assert.throws(() => Buffer.alloc(9).write('foo', encoding), error); +}