diff --git a/lib/handlers/subscribe.js b/lib/handlers/subscribe.js index 4100d058..8d97fb8b 100644 --- a/lib/handlers/subscribe.js +++ b/lib/handlers/subscribe.js @@ -16,6 +16,7 @@ function SubscribeState (client, packet, finish, granted) { this.packet = packet this.finish = finish this.granted = granted + this.subsIndex = 0 } function handleSubscribe (client, packet, done) { @@ -67,12 +68,10 @@ function storeSubscriptions (sub, done) { if (!sub) { this.granted.push(128) - return done(null, sub) } - if (sub && packet.subscriptions[0].topic !== sub.topic) { - // don't call addSubscriptions per topic, - // TODO change aedes subscribe handle, but this is a fast & dirty fix for now + if (packet.subscriptions.length > 0 && ++this.subsIndex < packet.subscriptions.length) { + // TODO change aedes subscribe handle, but this fix bugs for now. return done(null, sub) } diff --git a/test/auth.js b/test/auth.js index 1d1faecb..6167a393 100644 --- a/test/auth.js +++ b/test/auth.js @@ -566,6 +566,40 @@ test('negate multiple subscriptions', function (t) { }) }) +test('negate subscription with correct persistence', function (t) { + t.plan(7) + + var broker = aedes() + broker.authorizeSubscribe = function (client, sub, cb) { + t.ok(client, 'client exists') + if (sub.topic === 'hello') { + sub = null + } + cb(null, sub) + } + + var s = connect(setup(broker), { clean: false, clientId: 'abcde' }) + s.outStream.once('data', function (packet) { + t.equal(packet.cmd, 'suback') + t.deepEqual(packet.granted, [128, 0]) + t.notEqual(broker.persistence._subscriptions.get('abcde'), undefined) + t.deepEqual(broker.persistence._subscriptions.get('abcde').size, 2) + t.equal(packet.messageId, 24) + }) + + s.inStream.write({ + cmd: 'subscribe', + messageId: 24, + subscriptions: [{ + topic: 'hello', + qos: 0 + }, { + topic: 'world', + qos: 0 + }] + }) +}) + test('negate multiple subscriptions random times', function (t) { t.plan(5)