Skip to content

Commit

Permalink
streams: make setDefaultEncoding() throw
Browse files Browse the repository at this point in the history
PR-URL: nodejs/node-v0.x-archive#8529
Fixes: f04f3a0 "streams: set default encoding for writable streams"
[[email protected]: update tests to check if throws]
Signed-off-by: Trevor Norris <[email protected]>
  • Loading branch information
mscdex authored and trevnorris committed Oct 9, 2014
1 parent db7df57 commit 874dd59
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
8 changes: 3 additions & 5 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 5 additions & 9 deletions test/simple/test-stream-writable-change-default-encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}());

0 comments on commit 874dd59

Please sign in to comment.