streams: make setDefaultEncoding() throw
PR-URL: https://github.com/joyent/node/pull/8529 Fixes: f04f3a0 "streams: set default encoding for writable streams" [trev.norris@gmail.com: update tests to check if throws] Signed-off-by: Trevor Norris <trev.norris@gmail.com>
This commit is contained in:
parent
db7df57e03
commit
874dd590cf
@ -222,14 +222,12 @@ Writable.prototype.uncork = function() {
|
||||
};
|
||||
|
||||
Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
|
||||
if (typeof encoding !== 'string')
|
||||
return false;
|
||||
// node::ParseEncoding() requires lower case.
|
||||
encoding = encoding.toLowerCase();
|
||||
if (typeof encoding === 'string')
|
||||
encoding = encoding.toLowerCase();
|
||||
if (!Buffer.isEncoding(encoding))
|
||||
return false;
|
||||
throw new TypeError('Unknown encoding: ' + encoding);
|
||||
this._writableState.defaultEncoding = encoding;
|
||||
return true;
|
||||
};
|
||||
|
||||
function decodeChunk(state, chunk, encoding) {
|
||||
|
@ -49,28 +49,24 @@ MyWritable.prototype._write = function (chunk, encoding, callback) {
|
||||
var m = new MyWritable(function(isBuffer, type, enc) {
|
||||
assert.equal(enc, 'ascii');
|
||||
}, { decodeStrings: false });
|
||||
var status = m.setDefaultEncoding('ascii');
|
||||
assert.equal(status, true);
|
||||
m.setDefaultEncoding('ascii');
|
||||
m.write('bar');
|
||||
m.end();
|
||||
}());
|
||||
|
||||
(function changeDefaultEncodingToInvalidValue() {
|
||||
assert.throws(function changeDefaultEncodingToInvalidValue() {
|
||||
var m = new MyWritable(function(isBuffer, type, enc) {
|
||||
assert.equal(enc, 'utf8');
|
||||
}, { decodeStrings: false });
|
||||
var status = m.setDefaultEncoding({});
|
||||
assert.equal(status, false);
|
||||
m.setDefaultEncoding({});
|
||||
m.write('bar');
|
||||
m.end();
|
||||
}());
|
||||
}, TypeError);
|
||||
|
||||
(function checkVairableCaseEncoding() {
|
||||
var m = new MyWritable(function(isBuffer, type, enc) {
|
||||
assert.equal(enc, 'ascii');
|
||||
}, { decodeStrings: false });
|
||||
var status = m.setDefaultEncoding('AsCii');
|
||||
assert.equal(status, true);
|
||||
m.setDefaultEncoding('AsCii');
|
||||
m.write('bar');
|
||||
m.end();
|
||||
}());
|
||||
|
Loading…
x
Reference in New Issue
Block a user