This repository has been archived by the owner on Mar 17, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
test.js
202 lines (169 loc) · 4.72 KB
/
test.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* Environment variables
*/
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; // Ignore self-signed certificate errors
/**
* Testing dependencies
*/
var mqttOverWs = require('./')
, mqtt = require('mqtt')
, abstractClientTests = require("mqtt/test/abstract_client")
, testServer = require("./test-server")
, server = testServer.start()
, secureServer = testServer.startSecure()
var secureClientOpts = {
protocol: {
ca: testServer.ssl.cert
}
};
function clientTests(buildClient) {
var client;
beforeEach(function() {
client = buildClient();
});
afterEach(function(done) {
client.once("close", 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");
});
}
describe('MqttClient', function() {
describe("specifying a port", function() {
clientTests(function() {
return mqttOverWs.createClient(testServer.port);
});
});
describe("specifying a port and host", function() {
clientTests(function() {
return mqttOverWs.createClient(testServer.port, 'localhost');
});
});
describe("specifying a URL", function() {
clientTests(function() {
return mqttOverWs.createClient('ws://localhost:' + testServer.port);
});
});
if (process.title === 'browser') {
describe("specifying nothing", function() {
clientTests(function() {
return mqttOverWs.createClient();
});
});
}
if (process.title === 'node') {
describe("specifying a port, secure URL and secure client options", function(){
clientTests(function(){
return mqttOverWs.createClient(testServer.securePort, 'wss://localhost', secureClientOpts);
});
});
describe("specifying a URL with authentication", function() {
var authServer = testServer.startAuth();
clientTests(function() {
return mqttOverWs.createClient('ws://localhost:' + testServer.authPort, {
username: 'test-user',
password: 'p@ssword'
});
});
});
describe("specifying a URL and secure client options", function(){
clientTests(function(){
return mqttOverWs.createClient('wss://localhost:' + testServer.securePort, secureClientOpts);
});
});
}
});
function connectionTests(buildconn) {
var conn;
var connPacket = {
keepalive: 60,
protocolId: 'MQIsdp',
protocolVersion: 3,
reconnectPeriod: 1000,
clean: true,
encoding: 'utf8',
clientId: 'ahahah'
};
var publishPacket = {
topic: "hello",
payload: "world",
qos: 0,
retain: false,
messageId: 42
};
var subscribePacket = {
subscriptions: [{
topic: "hello",
qos: 0
}],
messageId: 42
};
beforeEach(function() {
conn = buildconn();
});
afterEach(function() {
conn.stream.destroy()
});
it("should publish and subscribe", function(done) {
conn.connect(connPacket);
conn.on("connack", function() {
conn.subscribe(subscribePacket);
});
conn.on('suback', function(packet) {
conn.publish(publishPacket);
});
conn.on('publish', function() {
done();
});
});
}
describe('MqttConnection', function() {
describe("specifying a port", function() {
connectionTests(function() {
return mqttOverWs.createConnection(testServer.port);
});
});
describe("specifying a port and host", function() {
connectionTests(function() {
return mqttOverWs.createConnection(testServer.port, 'localhost');
});
});
describe("specifying a URL", function() {
connectionTests(function() {
return mqttOverWs.createConnection('ws://localhost:' + testServer.port);
});
});
if (process.title === 'node') {
describe("specifying a port and secure URL", function() {
connectionTests(function() {
return mqttOverWs.createConnection(testServer.securePort, 'wss://localhost');
});
});
describe("specifying a port, secure URL and secure Client options", function() {
connectionTests(function() {
return mqttOverWs.createConnection(testServer.securePort, 'wss://localhost', secureClientOpts);
});
});
describe("specifying a secure URL", function() {
connectionTests(function() {
return mqttOverWs.createConnection('wss://localhost:' + testServer.securePort);
});
});
}
});
describe('mqttOverWs', function() {
it("should expose MqttConnection", function() {
mqttOverWs.MqttConnection.should.eql(mqtt.MqttConnection);
});
it("should expose MqttClient", function() {
mqttOverWs.MqttClient.should.eql(mqtt.MqttClient);
});
});