Empty payload by default with message.encode

This commit is contained in:
Kagami Hiiragi 2015-02-01 18:15:34 +03:00
parent 25243531e9
commit d2a6528fb3
2 changed files with 9 additions and 0 deletions

View File

@ -86,6 +86,9 @@ var message = exports.message = {
encode: function(command, payload) { encode: function(command, payload) {
assert(command.length <= 12, "Command is too long"); assert(command.length <= 12, "Command is too long");
assert(isAscii(command), "Non-ASCII characters in command"); assert(isAscii(command), "Non-ASCII characters in command");
if (!payload) {
payload = new Buffer(0);
}
var buf = new Buffer(24 + payload.length); var buf = new Buffer(24 + payload.length);
buf.fill(0); buf.fill(0);
buf.writeUInt32BE(message.MAGIC, 0, true); buf.writeUInt32BE(message.MAGIC, 0, true);

View File

@ -138,6 +138,12 @@ describe("Common structures", function() {
it("should encode", function() { it("should encode", function() {
expect(message.encode("test", Buffer("payload")).toString("hex")).to.equal("e9beb4d97465737400000000000000000000000770b33ce97061796c6f6164"); expect(message.encode("test", Buffer("payload")).toString("hex")).to.equal("e9beb4d97465737400000000000000000000000770b33ce97061796c6f6164");
}); });
it("should encode empty payload without second argument", function() {
var res = message.decode(message.encode("ping"));
expect(res.command).to.equal("ping");
expect(res.payload.toString("hex")).to.equal("");
});
}); });
describe("object", function() { describe("object", function() {