mqtt.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. 'use strict'
  2. var fs = require('fs')
  3. var path = require('path')
  4. var mqtt = require('../')
  5. describe('mqtt', function () {
  6. describe('#connect', function () {
  7. var sslOpts, sslOpts2
  8. it('should return an MqttClient when connect is called with mqtt:/ url', function () {
  9. var c = mqtt.connect('mqtt://localhost:1883')
  10. c.should.be.instanceOf(mqtt.MqttClient)
  11. c.end()
  12. })
  13. it('should throw an error when called with no protocol specified', function () {
  14. (function () {
  15. var c = mqtt.connect('foo.bar.com')
  16. c.end()
  17. }).should.throw('Missing protocol')
  18. })
  19. it('should throw an error when called with no protocol specified - with options', function () {
  20. (function () {
  21. var c = mqtt.connect('tcp://foo.bar.com', { protocol: null })
  22. c.end()
  23. }).should.throw('Missing protocol')
  24. })
  25. it('should return an MqttClient with username option set', function () {
  26. var c = mqtt.connect('mqtt://user:pass@localhost:1883')
  27. c.should.be.instanceOf(mqtt.MqttClient)
  28. c.options.should.have.property('username', 'user')
  29. c.options.should.have.property('password', 'pass')
  30. c.end()
  31. })
  32. it('should return an MqttClient with username and password options set', function () {
  33. var c = mqtt.connect('mqtt://user@localhost:1883')
  34. c.should.be.instanceOf(mqtt.MqttClient)
  35. c.options.should.have.property('username', 'user')
  36. c.end()
  37. })
  38. it('should return an MqttClient with the clientid with random value', function () {
  39. var c = mqtt.connect('mqtt://user@localhost:1883')
  40. c.should.be.instanceOf(mqtt.MqttClient)
  41. c.options.should.have.property('clientId')
  42. c.end()
  43. })
  44. it('should return an MqttClient with the clientid with empty string', function () {
  45. var c = mqtt.connect('mqtt://user@localhost:1883?clientId=')
  46. c.should.be.instanceOf(mqtt.MqttClient)
  47. c.options.should.have.property('clientId', '')
  48. c.end()
  49. })
  50. it('should return an MqttClient with the clientid option set', function () {
  51. var c = mqtt.connect('mqtt://user@localhost:1883?clientId=123')
  52. c.should.be.instanceOf(mqtt.MqttClient)
  53. c.options.should.have.property('clientId', '123')
  54. c.end()
  55. })
  56. it('should return an MqttClient when connect is called with tcp:/ url', function () {
  57. var c = mqtt.connect('tcp://localhost')
  58. c.should.be.instanceOf(mqtt.MqttClient)
  59. c.end()
  60. })
  61. it('should return an MqttClient with correct host when called with a host and port', function () {
  62. var c = mqtt.connect('tcp://user:pass@localhost:1883')
  63. c.options.should.have.property('hostname', 'localhost')
  64. c.options.should.have.property('port', 1883)
  65. c.end()
  66. })
  67. sslOpts = {
  68. keyPath: path.join(__dirname, 'helpers', 'private-key.pem'),
  69. certPath: path.join(__dirname, 'helpers', 'public-cert.pem'),
  70. caPaths: [path.join(__dirname, 'helpers', 'public-cert.pem')]
  71. }
  72. it('should return an MqttClient when connect is called with mqtts:/ url', function () {
  73. var c = mqtt.connect('mqtts://localhost', sslOpts)
  74. c.options.should.have.property('protocol', 'mqtts')
  75. c.on('error', function () {})
  76. c.should.be.instanceOf(mqtt.MqttClient)
  77. c.end()
  78. })
  79. it('should return an MqttClient when connect is called with ssl:/ url', function () {
  80. var c = mqtt.connect('ssl://localhost', sslOpts)
  81. c.options.should.have.property('protocol', 'ssl')
  82. c.on('error', function () {})
  83. c.should.be.instanceOf(mqtt.MqttClient)
  84. c.end()
  85. })
  86. it('should return an MqttClient when connect is called with ws:/ url', function () {
  87. var c = mqtt.connect('ws://localhost', sslOpts)
  88. c.options.should.have.property('protocol', 'ws')
  89. c.on('error', function () {})
  90. c.should.be.instanceOf(mqtt.MqttClient)
  91. c.end()
  92. })
  93. it('should return an MqttClient when connect is called with wss:/ url', function () {
  94. var c = mqtt.connect('wss://localhost', sslOpts)
  95. c.options.should.have.property('protocol', 'wss')
  96. c.on('error', function () {})
  97. c.should.be.instanceOf(mqtt.MqttClient)
  98. c.end()
  99. })
  100. sslOpts2 = {
  101. key: fs.readFileSync(path.join(__dirname, 'helpers', 'private-key.pem')),
  102. cert: fs.readFileSync(path.join(__dirname, 'helpers', 'public-cert.pem')),
  103. ca: [fs.readFileSync(path.join(__dirname, 'helpers', 'public-cert.pem'))]
  104. }
  105. it('should throw an error when it is called with cert and key set but no protocol specified', function () {
  106. // to do rewrite wrap function
  107. (function () {
  108. var c = mqtt.connect(sslOpts2)
  109. c.end()
  110. }).should.throw('Missing secure protocol key')
  111. })
  112. it('should throw an error when it is called with cert and key set and protocol other than allowed: mqtt,mqtts,ws,wss,wxs', function () {
  113. (function () {
  114. sslOpts2.protocol = 'UNKNOWNPROTOCOL'
  115. var c = mqtt.connect(sslOpts2)
  116. c.end()
  117. }).should.throw()
  118. })
  119. it('should return a MqttClient with mqtts set when connect is called key and cert set and protocol mqtt', function () {
  120. sslOpts2.protocol = 'mqtt'
  121. var c = mqtt.connect(sslOpts2)
  122. c.options.should.have.property('protocol', 'mqtts')
  123. c.on('error', function () {})
  124. c.should.be.instanceOf(mqtt.MqttClient)
  125. })
  126. it('should return a MqttClient with mqtts set when connect is called key and cert set and protocol mqtts', function () {
  127. sslOpts2.protocol = 'mqtts'
  128. var c = mqtt.connect(sslOpts2)
  129. c.options.should.have.property('protocol', 'mqtts')
  130. c.on('error', function () {})
  131. c.should.be.instanceOf(mqtt.MqttClient)
  132. })
  133. it('should return a MqttClient with wss set when connect is called key and cert set and protocol ws', function () {
  134. sslOpts2.protocol = 'ws'
  135. var c = mqtt.connect(sslOpts2)
  136. c.options.should.have.property('protocol', 'wss')
  137. c.on('error', function () {})
  138. c.should.be.instanceOf(mqtt.MqttClient)
  139. })
  140. it('should return a MqttClient with wss set when connect is called key and cert set and protocol wss', function () {
  141. sslOpts2.protocol = 'wss'
  142. var c = mqtt.connect(sslOpts2)
  143. c.options.should.have.property('protocol', 'wss')
  144. c.on('error', function () {})
  145. c.should.be.instanceOf(mqtt.MqttClient)
  146. })
  147. it('should return an MqttClient with the clientid with option of clientId as empty string', function () {
  148. var c = mqtt.connect('mqtt://localhost:1883', {
  149. clientId: ''
  150. })
  151. c.should.be.instanceOf(mqtt.MqttClient)
  152. c.options.should.have.property('clientId', '')
  153. })
  154. it('should return an MqttClient with the clientid with option of clientId empty', function () {
  155. var c = mqtt.connect('mqtt://localhost:1883')
  156. c.should.be.instanceOf(mqtt.MqttClient)
  157. c.options.should.have.property('clientId')
  158. c.end()
  159. })
  160. it('should return an MqttClient with the clientid with option of with specific clientId', function () {
  161. var c = mqtt.connect('mqtt://localhost:1883', {
  162. clientId: '123'
  163. })
  164. c.should.be.instanceOf(mqtt.MqttClient)
  165. c.options.should.have.property('clientId', '123')
  166. c.end()
  167. })
  168. })
  169. })