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