2017-04-28 05:07:55 +02:00
|
|
|
'use strict';
|
|
|
|
|
2017-06-29 05:58:39 +02:00
|
|
|
const common = require('../common');
|
2017-04-28 05:07:55 +02:00
|
|
|
const assert = require('assert');
|
|
|
|
|
|
|
|
// utf8, ucs2, ascii, latin1, utf16le
|
2017-05-05 09:08:59 +02:00
|
|
|
const encodings = ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1',
|
|
|
|
'binary', 'utf16le', 'utf-16le'];
|
2017-04-28 05:07:55 +02:00
|
|
|
|
|
|
|
encodings
|
|
|
|
.reduce((es, e) => es.concat(e, e.toUpperCase()), [])
|
|
|
|
.forEach((encoding) => {
|
|
|
|
assert.strictEqual(Buffer.from('foo', encoding).toString(encoding), 'foo');
|
|
|
|
});
|
|
|
|
|
|
|
|
// base64
|
|
|
|
['base64', 'BASE64'].forEach((encoding) => {
|
|
|
|
assert.strictEqual(Buffer.from('Zm9v', encoding).toString(encoding), 'Zm9v');
|
|
|
|
});
|
|
|
|
|
|
|
|
// hex
|
|
|
|
['hex', 'HEX'].forEach((encoding) => {
|
|
|
|
assert.strictEqual(Buffer.from('666f6f', encoding).toString(encoding),
|
|
|
|
'666f6f');
|
|
|
|
});
|
2017-05-05 09:08:59 +02:00
|
|
|
|
|
|
|
// Invalid encodings
|
|
|
|
for (let i = 1; i < 10; i++) {
|
|
|
|
const encoding = String(i).repeat(i);
|
2017-06-29 05:58:39 +02:00
|
|
|
const error = common.expectsError({
|
|
|
|
code: 'ERR_UNKNOWN_ENCODING',
|
|
|
|
type: TypeError,
|
|
|
|
message: `Unknown encoding: ${encoding}`
|
|
|
|
});
|
2017-05-05 09:08:59 +02:00
|
|
|
assert.ok(!Buffer.isEncoding(encoding));
|
|
|
|
assert.throws(() => Buffer.from('foo').toString(encoding), error);
|
|
|
|
}
|