bridge.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict'
  2. const { test } = require('tap')
  3. const { setup, connect, subscribe } = require('./helper')
  4. for (const qos of [0, 1, 2]) {
  5. const packet = {
  6. qos,
  7. cmd: 'publish',
  8. topic: 'hello',
  9. payload: 'world'
  10. }
  11. if (qos > 0) packet.messageId = 42
  12. test('normal client sends a publish message and shall receive it back, qos = ' + qos, function (t) {
  13. const s = connect(setup())
  14. t.teardown(s.broker.close.bind(s.broker))
  15. const handle = setTimeout(() => {
  16. t.fail('did not receive packet back')
  17. t.end()
  18. }, 1000)
  19. subscribe(t, s, 'hello', qos, function () {
  20. s.outStream.on('data', (packet) => {
  21. if (packet.cmd === 'publish') {
  22. clearTimeout(handle)
  23. t.end()
  24. } else if (packet.cmd === 'pubrec') {
  25. s.inStream.write({ cmd: 'pubrel', messageId: 42 })
  26. }
  27. })
  28. s.inStream.write(packet)
  29. })
  30. })
  31. test('bridge client sends a publish message but shall not receive it back, qos = ' + qos, function (t) {
  32. // protocolVersion 128 + 4 means mqtt 3.1.1 with bridgeMode enabled
  33. // https://github.com/mqttjs/mqtt-packet/blob/7f7c2ed8bcb4b2c582851d120a94e0b4a731f661/parser.js#L171
  34. const s = connect(setup(), { clientId: 'my-client-bridge-1', protocolVersion: 128 + 4 })
  35. t.teardown(s.broker.close.bind(s.broker))
  36. const handle = setTimeout(() => t.end(), 1000)
  37. subscribe(t, s, 'hello', qos, function () {
  38. s.outStream.on('data', function () {
  39. clearTimeout(handle)
  40. t.fail('should not receive packet back')
  41. t.end()
  42. })
  43. s.inStream.write(packet)
  44. })
  45. })
  46. }