index.js: replaced return [err] with callback

parser.js: replaced error return expects
This commit is contained in:
Sweet Song
2013-02-22 10:58:07 -05:00
parent dc063040d2
commit b21493325a
2 changed files with 35 additions and 16 deletions

View File

@@ -110,30 +110,31 @@ exports.encodePayload = function (packets) {
*/
exports.decodePayload = function (data, callback) {
var packet;
if (data == '') {
// parser error - ignoring payload
return [err];
callback(packet, true);
}
var length = ''
, n, msg, packet;
, n, msg;
for (var i = 0, l = data.length; i < l; i++) {
var chr = data.charAt(i)
var chr = data.charAt(i);
if (':' != chr) {
length += chr;
} else {
if ('' == length || (length != (n = Number(length)))) {
// parser error - ignoring payload
return [err];
callback(packet, true);
}
msg = data.substr(i + 1, n);
if (length != msg.length) {
// parser error - ignoring payload
return [err];
callback(packet, true);
}
if (msg.length) {
@@ -141,7 +142,7 @@ exports.decodePayload = function (data, callback) {
if (err.type == packet.type && err.data == packet.data) {
// parser error in individual packet - ignoring payload
return [err];
callback(packet, true);
}
callback(packet, i + n == l - 1);
@@ -155,7 +156,7 @@ exports.decodePayload = function (data, callback) {
if (length != '') {
// parser error - ignoring payload
return [err];
callback(packet, true);
}
};

View File

@@ -100,7 +100,9 @@ describe('parser', function () {
it('should decode payloads as arrays', function () {
decPayload(encPayload(['1:x', '2:y']),
function (packet, isLast) {});
function (packet, isLast) {
expect(packet.type).to.eql('error');
});
});
});
@@ -113,7 +115,9 @@ describe('parser', function () {
});
it('should encode/decode empty payloads', function () {
decPayload(encPayload([]), function (packet, isLast) {});
decPayload(encPayload([]), function (packet, isLast) {
expect(packet.type).to.eql('open');
});
});
});
@@ -121,19 +125,33 @@ describe('parser', function () {
var err = [{ type: 'error', data: 'parser error' }];
it('should err on bad payload format', function () {
decPayload('1!', function (packet, isLast) {});
decPayload('', function (packet, isLast) {});
decPayload('))', function (packet, isLast) {});
decPayload('1!', function (packet, isLast) {
expect(packet).to.eql(undefined);
});
decPayload('', function (packet, isLast) {
expect(packet).to.eql(undefined);
});
decPayload('))', function (packet, isLast) {
expect(packet).to.eql(undefined);
});
});
it('should err on bad payload length', function () {
decPayload('1:aa', function (packet, isLast) {});
decPayload('1:', function (packet, isLast) {});
decPayload('1:a2:b', function (packet, isLast) {});
decPayload('1:aa', function (packet, isLast) {
expect(packet.type).to.eql('error');
});
decPayload('1:', function (packet, isLast) {
expect(packet).to.eql(undefined);
});
decPayload('1:a2:b', function (packet, isLast) {
expect(packet.type).to.eql('error');
});
});
it('should err on bad packet format', function () {
decPayload('3:99:', function (packet, isLast) {});
decPayload('3:99:', function (packet, isLast) {
expect(packet.type).to.eql('error');
});
});
});
});