Skip to content

Commit 545bff0

Browse files
committed
batch trigger: stringify :data before sending
1 parent d574dd7 commit 545bff0

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/events.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
exports.trigger = function(pusher, channels, eventName, data, socketId, callback) {
22
var event = {
33
"name": eventName,
4-
"data": (typeof data === 'object' ? JSON.stringify(data) : data),
4+
"data": ensureJSON(data),
55
"channels": channels
66
};
77
if (socketId) {
@@ -11,5 +11,12 @@ exports.trigger = function(pusher, channels, eventName, data, socketId, callback
1111
}
1212

1313
exports.triggerBatch = function(pusher, batch, callback) {
14+
for (var i = 0; i < batch.length; i++) {
15+
batch[i].data = ensureJSON(batch[i].data);
16+
}
1417
pusher.post({ path: '/batch_events', body: { batch: batch } }, callback);
1518
}
19+
20+
function ensureJSON(data) {
21+
return typeof data === 'string' ? data : JSON.stringify(data);
22+
}

tests/integration/pusher/trigger.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,5 +321,34 @@ describe("Pusher", function() {
321321
data: "test2"
322322
}], done);
323323
});
324+
325+
it("should stringify data before posting", function(done) {
326+
var mock = nock("http://api.pusherapp.com")
327+
.filteringPath(function(path) {
328+
return path
329+
.replace(/auth_timestamp=[0-9]+/, "auth_timestamp=X")
330+
.replace(/auth_signature=[0-9a-f]{64}/, "auth_signature=Y");
331+
})
332+
.post(
333+
"/apps/1234/batch_events?auth_key=f00d&auth_timestamp=X&auth_version=1.0&body_md5=ade2e9d64d936215c2b2d6a6f4606ef9&auth_signature=Y",
334+
JSON.stringify({"batch":[{"channel":"integration","name":"event","data":"{\"hello\":\"world\"}"},{"channel":"integration2","name":"event2","data":"{\"hello2\":\"another world\"}"}]})
335+
)
336+
.reply(200, "{}");
337+
338+
pusher.triggerBatch([{
339+
channel: "integration",
340+
name: "event",
341+
data: {
342+
hello: "world"
343+
}
344+
},
345+
{
346+
channel: "integration2",
347+
name: "event2",
348+
data: {
349+
hello2: "another world"
350+
}
351+
}], done);
352+
});
324353
})
325354
});

0 commit comments

Comments
 (0)