diff --git a/lib/client.js b/lib/client.js index 9853ff69..3656bf7e 100644 --- a/lib/client.js +++ b/lib/client.js @@ -115,6 +115,11 @@ function Client (broker, conn) { that.broker.authorizeForward(that, _packet) if (toForward) { var packet = new QoSPacket(toForward, that) + // Downgrading to client subscription qos if needed + var clientSub = that.subscriptions[packet.topic] + if (clientSub && clientSub.qos && clientSub.qos < packet.qos) { + packet.qos = clientSub.qos + } packet.writeCallback = cb if (that.clean) { writeQoS(null, that, packet) diff --git a/test/qos2.js b/test/qos2.js index 7e9ff5c5..5a943ac1 100644 --- a/test/qos2.js +++ b/test/qos2.js @@ -209,6 +209,39 @@ test('subscribe QoS 0, but publish QoS 2', function (t) { }) }) +test('subscribe QoS 1, but publish QoS 2', function (t) { + var broker = aedes() + var publisher = connect(setup(broker)) + var subscriber = connect(setup(broker)) + var expected = { + cmd: 'publish', + topic: 'hello', + payload: Buffer.from('world'), + qos: 1, + dup: false, + length: 14, + retain: false + } + + subscribe(t, subscriber, 'hello', 1, function () { + subscriber.outStream.once('data', function (packet) { + delete packet.messageId + t.deepEqual(packet, expected, 'packet must match') + t.end() + }) + + publish(t, publisher, { + cmd: 'publish', + topic: 'hello', + payload: Buffer.from('world'), + qos: 2, + retain: false, + messageId: 42, + dup: false + }) + }) +}) + test('restore QoS 2 subscriptions not clean', function (t) { var broker = aedes() var publisher