Skip to content

Commit

Permalink
batch trigger: stringify :data before sending
Browse files Browse the repository at this point in the history
  • Loading branch information
jpatel531 committed May 24, 2016
1 parent d574dd7 commit 545bff0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/events.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
exports.trigger = function(pusher, channels, eventName, data, socketId, callback) {
var event = {
"name": eventName,
"data": (typeof data === 'object' ? JSON.stringify(data) : data),
"data": ensureJSON(data),
"channels": channels
};
if (socketId) {
Expand All @@ -11,5 +11,12 @@ exports.trigger = function(pusher, channels, eventName, data, socketId, callback
}

exports.triggerBatch = function(pusher, batch, callback) {
for (var i = 0; i < batch.length; i++) {
batch[i].data = ensureJSON(batch[i].data);
}
pusher.post({ path: '/batch_events', body: { batch: batch } }, callback);
}

function ensureJSON(data) {
return typeof data === 'string' ? data : JSON.stringify(data);
}
29 changes: 29 additions & 0 deletions tests/integration/pusher/trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,5 +321,34 @@ describe("Pusher", function() {
data: "test2"
}], done);
});

it("should stringify data before posting", function(done) {
var mock = nock("http://api.pusherapp.com")
.filteringPath(function(path) {
return path
.replace(/auth_timestamp=[0-9]+/, "auth_timestamp=X")
.replace(/auth_signature=[0-9a-f]{64}/, "auth_signature=Y");
})
.post(
"/apps/1234/batch_events?auth_key=f00d&auth_timestamp=X&auth_version=1.0&body_md5=ade2e9d64d936215c2b2d6a6f4606ef9&auth_signature=Y",
JSON.stringify({"batch":[{"channel":"integration","name":"event","data":"{\"hello\":\"world\"}"},{"channel":"integration2","name":"event2","data":"{\"hello2\":\"another world\"}"}]})
)
.reply(200, "{}");

pusher.triggerBatch([{
channel: "integration",
name: "event",
data: {
hello: "world"
}
},
{
channel: "integration2",
name: "event2",
data: {
hello2: "another world"
}
}], done);
});
})
});

0 comments on commit 545bff0

Please sign in to comment.