node-red-contrib-aedes
Version:
Node Red MQTT broker node based on aedes.js
246 lines (236 loc) • 6.82 kB
JavaScript
/* eslint-env mocha */
/* eslint no-console: ["error", { allow: ["warn", "error"] }] */
const helper = require('node-red-node-test-helper');
const aedesNode = require('../aedes.js');
const mqttNode = require('../node_modules/node-red/node_modules/@node-red/nodes/core/network/10-mqtt.js');
const mqtt = require('mqtt');
const { logError } = require('./test-utils');
helper.init(require.resolve('node-red'));
describe('Aedes Broker Websocket tests', function () {
beforeEach(function (done) {
helper.startServer(done);
});
afterEach(function (done) {
helper.unload().then(function () {
helper.stopServer(done);
});
});
it('should connect an external ws mqtt client', function (done) {
this.timeout(10000);
const flow = [
{
id: 'n1',
type: 'aedes broker',
mqtt_port: '1883',
mqtt_ws_port: '8080',
name: 'Aedes 1883',
wires: [['n2'], []]
},
{
id: 'n2',
type: 'helper'
}
];
helper.load(aedesNode, flow, function () {
const n1 = helper.getNode('n1');
n1._initPromise.then(function () {
const client = mqtt.connect('ws://localhost:8080', {
clientId: 'ws-client',
resubscribe: false,
reconnectPeriod: -1
});
client.on('error', logError);
const n2 = helper.getNode('n2');
n2.on('input', function (msg) {
if (msg.topic === 'clientReady') {
client.end(function () {
done();
});
}
});
});
});
});
it('should not throw an exception with 2 servers on the same ws port', function (done) {
this.timeout(10000); // have to wait for the inject with delay of two seconds
helper.load([aedesNode, mqttNode], [
{
id: 'n1',
type: 'aedes broker',
mqtt_port: '1883',
name: 'Aedes 1883',
mqtt_ws_port: '8080',
wires: [
[], []
]
}, {
id: 'n11',
type: 'aedes broker',
mqtt_port: '1884',
name: 'Aedes 1884',
mqtt_ws_port: '8080',
wires: [
[], []
]
}
],
function () {
const n1 = helper.getNode('n1');
n1.should.have.property('name', 'Aedes 1883');
const n11 = helper.getNode('n11');
n11.should.have.property('name', 'Aedes 1884');
done();
});
});
it('should set error status when ws port is already in use', function (done) {
this.timeout(10000);
const net = require('net');
const blocker = net.createServer();
blocker.listen(8081, function () {
const flow = [{
id: 'n1',
type: 'aedes broker',
mqtt_port: '1883',
mqtt_ws_port: '8081',
name: 'Aedes WS Error',
wires: [[], []]
}];
helper.load(aedesNode, flow, function () {
const n1 = helper.getNode('n1');
n1.on('call:status', function (call) {
if (call.args[0].fill === 'red') {
call.args[0].should.have.property('fill', 'red');
call.args[0].should.have.property('shape', 'ring');
call.args[0].should.have.property('text', 'aedes-mqtt-broker.status.error');
blocker.close(function () {
done();
});
}
});
});
});
});
it('a subscriber should receive a message from an external ws publisher', function (done) {
this.timeout(10000); // have to wait for the inject with delay of 10 seconds
const flow = [
{
id: 'n1',
type: 'aedes broker',
mqtt_port: '1883',
mqtt_ws_port: '8080',
name: 'Aedes 1883',
wires: [
['n2'], []
]
},
{
id: 'n2',
type: 'helper'
}, {
id: 'n4',
type: 'mqtt in',
name: 'Aedes In 1883',
topic: 'test1883',
broker: 'b1',
wires: [
['n5']
]
}, {
id: 'n5',
type: 'helper'
}, {
id: 'b1',
type: 'mqtt-broker',
name: 'Broker',
broker: 'localhost',
port: '1883'
}
];
helper.load([aedesNode, mqttNode], flow, function () {
const n1 = helper.getNode('n1');
n1._initPromise.then(function () {
const client = mqtt.connect('ws://localhost:8080', { clientId: 'client', resubscribe: false, reconnectPeriod: -1 });
client.on('error', logError);
client.on('connect', function () {
// console.log('External client connected');
});
const n2 = helper.getNode('n2');
const n5 = helper.getNode('n5');
n2.on('input', function (msg) {
if (msg.topic === 'subscribe') {
client.publish('test1883', 'test');
}
});
n5.on('input', function (msg) {
// console.log(msg);
msg.should.have.property('topic', 'test1883');
client.end(function () {
done();
});
});
});
});
});
it('a subscriber should receive a message from an external publisher via ws path', function (done) {
this.timeout(10000); // have to wait for the inject with delay of 10 seconds
const flow = [
{
id: 'n1',
type: 'aedes broker',
mqtt_port: '1883',
mqtt_ws_bind: 'path',
mqtt_ws_path: '/mqtt',
name: 'Aedes 1883',
wires: [
['n2'], []
]
},
{
id: 'n2',
type: 'helper'
}, {
id: 'n4',
type: 'mqtt in',
name: 'Aedes In 1883',
topic: 'test1883',
broker: 'b1',
wires: [
['n5']
]
}, {
id: 'n5',
type: 'helper'
}, {
id: 'b1',
type: 'mqtt-broker',
name: 'Broker',
broker: 'localhost',
port: '1883'
}
];
helper.load([aedesNode, mqttNode], flow, function () {
const n1 = helper.getNode('n1');
n1._initPromise.then(function () {
const client = mqtt.connect(helper.url().replace(/http/, 'ws') + '/mqtt', { clientId: 'client', resubscribe: false, reconnectPeriod: -1 });
client.on('error', logError);
client.on('connect', function () {
// console.log('External client connected');
});
const n2 = helper.getNode('n2');
const n5 = helper.getNode('n5');
n2.on('input', function (msg) {
if (msg.topic === 'subscribe') {
client.publish('test1883', 'test');
}
});
n5.on('input', function (msg) {
// console.log(msg);
msg.should.have.property('topic', 'test1883');
client.end(function () {
done();
});
});
});
});
});
});