meta.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. 'use strict'
  2. const { test } = require('tap')
  3. const { setup, connect, subscribe, noError } = require('./helper')
  4. const aedes = require('../')
  5. test('count connected clients', function (t) {
  6. t.plan(4)
  7. const broker = aedes()
  8. t.teardown(broker.close.bind(broker))
  9. t.equal(broker.connectedClients, 0, 'no connected clients')
  10. connect(setup(broker), {
  11. }, function () {
  12. t.equal(broker.connectedClients, 1, 'one connected clients')
  13. const last = connect(setup(broker), {
  14. }, function () {
  15. t.equal(broker.connectedClients, 2, 'two connected clients')
  16. last.conn.destroy()
  17. // needed because destroy() will do the trick before
  18. // the next tick
  19. setImmediate(function () {
  20. t.equal(broker.connectedClients, 1, 'one connected clients')
  21. })
  22. })
  23. })
  24. })
  25. test('call published method', function (t) {
  26. t.plan(4)
  27. const broker = aedes()
  28. t.teardown(broker.close.bind(broker))
  29. broker.published = function (packet, client, done) {
  30. t.equal(packet.topic, 'hello', 'topic matches')
  31. t.equal(packet.payload.toString(), 'world', 'payload matches')
  32. t.equal(client, null, 'no client')
  33. done()
  34. }
  35. broker.publish({
  36. topic: 'hello',
  37. payload: Buffer.from('world')
  38. }, function (err) {
  39. t.error(err, 'no error')
  40. })
  41. })
  42. test('call published method with client', function (t) {
  43. t.plan(4)
  44. const broker = aedes()
  45. t.teardown(broker.close.bind(broker))
  46. broker.published = function (packet, client, done) {
  47. // for internal messages, client will be null
  48. if (client) {
  49. t.equal(packet.topic, 'hello', 'topic matches')
  50. t.equal(packet.payload.toString(), 'world', 'payload matches')
  51. t.equal(packet.qos, 1)
  52. t.equal(packet.messageId, 42)
  53. done()
  54. }
  55. }
  56. const s = connect(setup(broker))
  57. s.inStream.write({
  58. cmd: 'publish',
  59. topic: 'hello',
  60. payload: Buffer.from('world'),
  61. qos: 1,
  62. messageId: 42
  63. })
  64. })
  65. test('emit publish event with client - QoS 0', function (t) {
  66. t.plan(3)
  67. const broker = aedes()
  68. t.teardown(broker.close.bind(broker))
  69. broker.on('publish', function (packet, client) {
  70. // for internal messages, client will be null
  71. if (client) {
  72. t.equal(packet.qos, 0)
  73. t.equal(packet.topic, 'hello', 'topic matches')
  74. t.equal(packet.payload.toString(), 'world', 'payload matches')
  75. }
  76. })
  77. const s = connect(setup(broker))
  78. s.inStream.write({
  79. cmd: 'publish',
  80. topic: 'hello',
  81. payload: Buffer.from('world'),
  82. qos: 0
  83. })
  84. })
  85. test('emit publish event with client - QoS 1', function (t) {
  86. t.plan(4)
  87. const broker = aedes()
  88. t.teardown(broker.close.bind(broker))
  89. broker.on('publish', function (packet, client) {
  90. // for internal messages, client will be null
  91. if (client) {
  92. t.equal(packet.qos, 1)
  93. t.equal(packet.messageId, 42)
  94. t.equal(packet.topic, 'hello', 'topic matches')
  95. t.equal(packet.payload.toString(), 'world', 'payload matches')
  96. }
  97. })
  98. const s = connect(setup(broker))
  99. s.inStream.write({
  100. cmd: 'publish',
  101. topic: 'hello',
  102. payload: Buffer.from('world'),
  103. qos: 1,
  104. messageId: 42
  105. })
  106. })
  107. test('emit subscribe event', function (t) {
  108. t.plan(6)
  109. const broker = aedes()
  110. t.teardown(broker.close.bind(broker))
  111. const s = connect(setup(broker), { clientId: 'abcde' })
  112. broker.on('subscribe', function (subscriptions, client) {
  113. t.same(subscriptions, [{
  114. topic: 'hello',
  115. qos: 0
  116. }], 'topic matches')
  117. t.equal(client.id, 'abcde', 'client matches')
  118. })
  119. subscribe(t, s, 'hello', 0, function () {
  120. t.pass('subscribe completed')
  121. })
  122. })
  123. test('emit subscribe event if unrecognized params in subscribe packet structure', function (t) {
  124. t.plan(3)
  125. const broker = aedes()
  126. t.teardown(broker.close.bind(broker))
  127. const s = noError(connect(setup(broker)))
  128. const subs = [{ topic: 'hello', qos: 0 }]
  129. broker.on('subscribe', function (subscriptions, client) {
  130. t.equal(subscriptions, subs)
  131. t.same(client, s.client)
  132. })
  133. s.client.subscribe({
  134. subscriptions: subs,
  135. restore: true
  136. }, function (err) {
  137. t.error(err)
  138. })
  139. })
  140. test('emit unsubscribe event', function (t) {
  141. t.plan(6)
  142. const broker = aedes()
  143. t.teardown(broker.close.bind(broker))
  144. const s = connect(setup(broker), { clean: true, clientId: 'abcde' })
  145. broker.on('unsubscribe', function (unsubscriptions, client) {
  146. t.same(unsubscriptions, [
  147. 'hello'
  148. ], 'unsubscription matches')
  149. t.equal(client.id, 'abcde', 'client matches')
  150. })
  151. subscribe(t, s, 'hello', 0, function () {
  152. s.inStream.write({
  153. cmd: 'unsubscribe',
  154. messageId: 43,
  155. unsubscriptions: ['hello']
  156. })
  157. s.outStream.once('data', function (packet) {
  158. t.pass('subscribe completed')
  159. })
  160. })
  161. })
  162. test('emit unsubscribe event if unrecognized params in unsubscribe packet structure', function (t) {
  163. t.plan(3)
  164. const broker = aedes()
  165. t.teardown(broker.close.bind(broker))
  166. const s = noError(connect(setup(broker)))
  167. const unsubs = [{ topic: 'hello', qos: 0 }]
  168. broker.on('unsubscribe', function (unsubscriptions, client) {
  169. t.equal(unsubscriptions, unsubs)
  170. t.same(client, s.client)
  171. })
  172. s.client.unsubscribe({
  173. unsubscriptions: unsubs,
  174. close: true
  175. }, function (err) {
  176. t.error(err)
  177. })
  178. })
  179. test('dont emit unsubscribe event on client close', function (t) {
  180. t.plan(3)
  181. const broker = aedes()
  182. t.teardown(broker.close.bind(broker))
  183. const s = noError(connect(setup(broker), { clientId: 'abcde' }), t)
  184. broker.on('unsubscribe', function (unsubscriptions, client) {
  185. t.error('unsubscribe should not be emitted')
  186. })
  187. subscribe(t, s, 'hello', 0, function () {
  188. s.inStream.end({
  189. cmd: 'disconnect'
  190. })
  191. s.outStream.once('data', function (packet) {
  192. t.pass('unsubscribe completed')
  193. })
  194. })
  195. })
  196. test('emit clientDisconnect event', function (t) {
  197. t.plan(1)
  198. const broker = aedes()
  199. t.teardown(broker.close.bind(broker))
  200. broker.on('clientDisconnect', function (client) {
  201. t.equal(client.id, 'abcde', 'client matches')
  202. })
  203. const s = noError(connect(setup(broker), { clientId: 'abcde' }), t)
  204. s.inStream.end({
  205. cmd: 'disconnect'
  206. })
  207. s.outStream.resume()
  208. })
  209. test('emits client', function (t) {
  210. t.plan(1)
  211. const broker = aedes()
  212. t.teardown(broker.close.bind(broker))
  213. broker.on('client', function (client) {
  214. t.equal(client.id, 'abcde', 'clientId matches')
  215. })
  216. connect(setup(broker), {
  217. clientId: 'abcde'
  218. })
  219. })
  220. test('get aedes version', function (t) {
  221. t.plan(1)
  222. const broker = aedes()
  223. t.teardown(broker.close.bind(broker))
  224. t.equal(broker.version, require('../package.json').version)
  225. })
  226. test('connect and connackSent event', { timeout: 50 }, function (t) {
  227. t.plan(3)
  228. const s = setup()
  229. t.teardown(s.broker.close.bind(s.broker))
  230. const clientId = 'my-client'
  231. s.broker.on('connackSent', function (packet, client) {
  232. t.equal(packet.returnCode, 0)
  233. t.equal(client.id, clientId, 'connackSent event and clientId matches')
  234. })
  235. s.inStream.write({
  236. cmd: 'connect',
  237. protocolId: 'MQTT',
  238. protocolVersion: 4,
  239. clean: true,
  240. clientId,
  241. keepalive: 0
  242. })
  243. s.outStream.on('data', function (packet) {
  244. t.same(packet, {
  245. cmd: 'connack',
  246. returnCode: 0,
  247. length: 2,
  248. qos: 0,
  249. retain: false,
  250. dup: false,
  251. topic: null,
  252. payload: null,
  253. sessionPresent: false
  254. }, 'successful connack')
  255. })
  256. })