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