wx.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict'
  2. var mqtt = require('../../lib/connect')
  3. var _URL = require('url')
  4. var xtend = require('xtend')
  5. var parsed = _URL.parse(document.URL)
  6. var isHttps = parsed.protocol === 'https:'
  7. var port = parsed.port || (isHttps ? 443 : 80)
  8. var host = parsed.hostname
  9. var protocol = isHttps ? 'wxs' : 'wx'
  10. require('../helpers/wx')
  11. function clientTests (buildClient) {
  12. var client
  13. beforeEach(function () {
  14. client = buildClient()
  15. client.on('offline', function () {
  16. console.log('client offline')
  17. })
  18. client.on('connect', function () {
  19. console.log('client connect')
  20. })
  21. client.on('reconnect', function () {
  22. console.log('client reconnect')
  23. })
  24. })
  25. afterEach(function (done) {
  26. client.once('close', function () {
  27. done()
  28. })
  29. client.end()
  30. })
  31. it('should connect', function (done) {
  32. client.on('connect', function () {
  33. done()
  34. })
  35. })
  36. it('should publish and subscribe', function (done) {
  37. client.subscribe('hello', function () {
  38. done()
  39. }).publish('hello', 'world')
  40. })
  41. }
  42. function suiteFactory (configName, opts) {
  43. function setVersion (base) {
  44. return xtend(base || {}, opts)
  45. }
  46. var suiteName = 'MqttClient(' + configName + '=' + JSON.stringify(opts) + ')'
  47. describe(suiteName, function () {
  48. this.timeout(10000)
  49. describe('specifying nothing', function () {
  50. clientTests(function () {
  51. return mqtt.connect(setVersion())
  52. })
  53. })
  54. if (parsed.hostname === 'localhost') {
  55. describe('specifying a port', function () {
  56. clientTests(function () {
  57. return mqtt.connect(setVersion({ protocol: protocol, port: port }))
  58. })
  59. })
  60. }
  61. describe('specifying a port and host', function () {
  62. clientTests(function () {
  63. return mqtt.connect(setVersion({ protocol: protocol, port: port, host: host }))
  64. })
  65. })
  66. describe('specifying a URL', function () {
  67. clientTests(function () {
  68. return mqtt.connect(protocol + '://' + host + ':' + port, setVersion())
  69. })
  70. })
  71. describe('specifying a URL with a path', function () {
  72. clientTests(function () {
  73. return mqtt.connect(protocol + '://' + host + ':' + port + '/mqtt', setVersion())
  74. })
  75. })
  76. })
  77. }
  78. suiteFactory('v3', {protocolId: 'MQIsdp', protocolVersion: 3})
  79. suiteFactory('default', {})