From d2a6528fb3537f2025eccb5869970e8f1d01e7f2 Mon Sep 17 00:00:00 2001 From: Kagami Hiiragi Date: Sun, 1 Feb 2015 18:15:34 +0300 Subject: [PATCH] Empty payload by default with message.encode --- lib/structs.js | 3 +++ test.js | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/lib/structs.js b/lib/structs.js index a743745..807c975 100644 --- a/lib/structs.js +++ b/lib/structs.js @@ -86,6 +86,9 @@ var message = exports.message = { encode: function(command, payload) { assert(command.length <= 12, "Command is too long"); assert(isAscii(command), "Non-ASCII characters in command"); + if (!payload) { + payload = new Buffer(0); + } var buf = new Buffer(24 + payload.length); buf.fill(0); buf.writeUInt32BE(message.MAGIC, 0, true); diff --git a/test.js b/test.js index d635ba1..fb5bb5c 100644 --- a/test.js +++ b/test.js @@ -138,6 +138,12 @@ describe("Common structures", function() { it("should encode", function() { 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() {