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) {
|
Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
|
||||||
if (typeof encoding !== 'string')
|
|
||||||
return false;
|
|
||||||
// node::ParseEncoding() requires lower case.
|
// node::ParseEncoding() requires lower case.
|
||||||
encoding = encoding.toLowerCase();
|
if (typeof encoding === 'string')
|
||||||
|
encoding = encoding.toLowerCase();
|
||||||
if (!Buffer.isEncoding(encoding))
|
if (!Buffer.isEncoding(encoding))
|
||||||
return false;
|
throw new TypeError('Unknown encoding: ' + encoding);
|
||||||
this._writableState.defaultEncoding = encoding;
|
this._writableState.defaultEncoding = encoding;
|
||||||
return true;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function decodeChunk(state, chunk, encoding) {
|
function decodeChunk(state, chunk, encoding) {
|
||||||
|
@ -49,28 +49,24 @@ MyWritable.prototype._write = function (chunk, encoding, callback) {
|
|||||||
var m = new MyWritable(function(isBuffer, type, enc) {
|
var m = new MyWritable(function(isBuffer, type, enc) {
|
||||||
assert.equal(enc, 'ascii');
|
assert.equal(enc, 'ascii');
|
||||||
}, { decodeStrings: false });
|
}, { decodeStrings: false });
|
||||||
var status = m.setDefaultEncoding('ascii');
|
m.setDefaultEncoding('ascii');
|
||||||
assert.equal(status, true);
|
|
||||||
m.write('bar');
|
m.write('bar');
|
||||||
m.end();
|
m.end();
|
||||||
}());
|
}());
|
||||||
|
|
||||||
(function changeDefaultEncodingToInvalidValue() {
|
assert.throws(function changeDefaultEncodingToInvalidValue() {
|
||||||
var m = new MyWritable(function(isBuffer, type, enc) {
|
var m = new MyWritable(function(isBuffer, type, enc) {
|
||||||
assert.equal(enc, 'utf8');
|
|
||||||
}, { decodeStrings: false });
|
}, { decodeStrings: false });
|
||||||
var status = m.setDefaultEncoding({});
|
m.setDefaultEncoding({});
|
||||||
assert.equal(status, false);
|
|
||||||
m.write('bar');
|
m.write('bar');
|
||||||
m.end();
|
m.end();
|
||||||
}());
|
}, TypeError);
|
||||||
|
|
||||||
(function checkVairableCaseEncoding() {
|
(function checkVairableCaseEncoding() {
|
||||||
var m = new MyWritable(function(isBuffer, type, enc) {
|
var m = new MyWritable(function(isBuffer, type, enc) {
|
||||||
assert.equal(enc, 'ascii');
|
assert.equal(enc, 'ascii');
|
||||||
}, { decodeStrings: false });
|
}, { decodeStrings: false });
|
||||||
var status = m.setDefaultEncoding('AsCii');
|
m.setDefaultEncoding('AsCii');
|
||||||
assert.equal(status, true);
|
|
||||||
m.write('bar');
|
m.write('bar');
|
||||||
m.end();
|
m.end();
|
||||||
}());
|
}());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user