mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Merge pull request #3905 from node-red/mqtt-followups
Fix birth topic handling in MQTT node
This commit is contained in:
commit
f06c53f1f1
@ -459,7 +459,6 @@ module.exports = function(RED) {
|
|||||||
if(!opts || typeof opts !== "object") {
|
if(!opts || typeof opts !== "object") {
|
||||||
return; //nothing to change, simply return
|
return; //nothing to change, simply return
|
||||||
}
|
}
|
||||||
const originalBrokerURL = node.brokerurl;
|
|
||||||
|
|
||||||
//apply property changes (only if the property exists in the opts object)
|
//apply property changes (only if the property exists in the opts object)
|
||||||
setIfHasProperty(opts, node, "url", init);
|
setIfHasProperty(opts, node, "url", init);
|
||||||
@ -468,7 +467,6 @@ module.exports = function(RED) {
|
|||||||
setIfHasProperty(opts, node, "clientid", init);
|
setIfHasProperty(opts, node, "clientid", init);
|
||||||
setIfHasProperty(opts, node, "autoConnect", init);
|
setIfHasProperty(opts, node, "autoConnect", init);
|
||||||
setIfHasProperty(opts, node, "usetls", init);
|
setIfHasProperty(opts, node, "usetls", init);
|
||||||
setIfHasProperty(opts, node, "usews", init);
|
|
||||||
setIfHasProperty(opts, node, "verifyservercert", init);
|
setIfHasProperty(opts, node, "verifyservercert", init);
|
||||||
setIfHasProperty(opts, node, "compatmode", init);
|
setIfHasProperty(opts, node, "compatmode", init);
|
||||||
setIfHasProperty(opts, node, "protocolVersion", init);
|
setIfHasProperty(opts, node, "protocolVersion", init);
|
||||||
@ -571,9 +569,6 @@ module.exports = function(RED) {
|
|||||||
if (typeof node.usetls === 'undefined') {
|
if (typeof node.usetls === 'undefined') {
|
||||||
node.usetls = false;
|
node.usetls = false;
|
||||||
}
|
}
|
||||||
if (typeof node.usews === 'undefined') {
|
|
||||||
node.usews = false;
|
|
||||||
}
|
|
||||||
if (typeof node.verifyservercert === 'undefined') {
|
if (typeof node.verifyservercert === 'undefined') {
|
||||||
node.verifyservercert = false;
|
node.verifyservercert = false;
|
||||||
}
|
}
|
||||||
@ -1000,7 +995,7 @@ module.exports = function(RED) {
|
|||||||
node.client.publish(msg.topic, msg.payload, options, function (err) {
|
node.client.publish(msg.topic, msg.payload, options, function (err) {
|
||||||
if (done) {
|
if (done) {
|
||||||
done(err)
|
done(err)
|
||||||
} else {
|
} else if(err) {
|
||||||
node.error(err, msg)
|
node.error(err, msg)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -489,6 +489,39 @@ describe('MQTT Nodes', function () {
|
|||||||
}
|
}
|
||||||
testSendRecv(brokerOptions, { topic: brokerOptions.birthTopic }, {}, options, hooks);
|
testSendRecv(brokerOptions, { topic: brokerOptions.birthTopic }, {}, options, hooks);
|
||||||
});
|
});
|
||||||
|
itConditional('should safely discard bad birth topic', function (done) {
|
||||||
|
if (skipTests) { return this.skip() }
|
||||||
|
this.timeout = 2000;
|
||||||
|
const baseTopic = nextTopic();
|
||||||
|
const brokerOptions = {
|
||||||
|
protocolVersion: 4,
|
||||||
|
birthTopic: baseTopic + "#", // a publish topic should never have a wildcard
|
||||||
|
birthPayload: "broker connected",
|
||||||
|
birthQos: 2,
|
||||||
|
}
|
||||||
|
const options = {};
|
||||||
|
const hooks = { done: null, beforeLoad: null, afterLoad: null, afterConnect: null };
|
||||||
|
hooks.afterLoad = (helperNode, mqttBroker, mqttIn, mqttOut) => {
|
||||||
|
helperNode.on("input", function (msg) {
|
||||||
|
try {
|
||||||
|
msg.should.have.a.property("error").type("object");
|
||||||
|
msg.error.should.have.a.property("source").type("object");
|
||||||
|
msg.error.source.should.have.a.property("id", mqttIn.id);
|
||||||
|
done();
|
||||||
|
} catch (err) {
|
||||||
|
done(err)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return true; //handled
|
||||||
|
}
|
||||||
|
options.expectMsg = null;
|
||||||
|
try {
|
||||||
|
testSendRecv(brokerOptions, { topic: brokerOptions.birthTopic }, {}, options, hooks);
|
||||||
|
done()
|
||||||
|
} catch(err) {
|
||||||
|
done(e)
|
||||||
|
}
|
||||||
|
});
|
||||||
itConditional('should publish close message', function (done) {
|
itConditional('should publish close message', function (done) {
|
||||||
if (skipTests) { return this.skip() }
|
if (skipTests) { return this.skip() }
|
||||||
this.timeout = 2000;
|
this.timeout = 2000;
|
||||||
@ -646,12 +679,13 @@ function testSendRecv(brokerOptions, inNodeOptions, outNodeOptions, options, hoo
|
|||||||
const mqttBroker = helper.getNode(brokerOptions.id);
|
const mqttBroker = helper.getNode(brokerOptions.id);
|
||||||
const mqttIn = helper.getNode(nodes.mqtt_in.id);
|
const mqttIn = helper.getNode(nodes.mqtt_in.id);
|
||||||
const mqttOut = helper.getNode(nodes.mqtt_out.id);
|
const mqttOut = helper.getNode(nodes.mqtt_out.id);
|
||||||
let afterLoadHandled = false;
|
let afterLoadHandled = false, finished = false;
|
||||||
if (hooks.afterLoad) {
|
if (hooks.afterLoad) {
|
||||||
afterLoadHandled = hooks.afterLoad(helperNode, mqttBroker, mqttIn, mqttOut)
|
afterLoadHandled = hooks.afterLoad(helperNode, mqttBroker, mqttIn, mqttOut)
|
||||||
}
|
}
|
||||||
if (!afterLoadHandled) {
|
if (!afterLoadHandled) {
|
||||||
helperNode.on("input", function (msg) {
|
helperNode.on("input", function (msg) {
|
||||||
|
finished = true
|
||||||
try {
|
try {
|
||||||
compareMsgToExpected(msg, expectMsg);
|
compareMsgToExpected(msg, expectMsg);
|
||||||
if (hooks.done) { hooks.done(); }
|
if (hooks.done) { hooks.done(); }
|
||||||
@ -676,10 +710,12 @@ function testSendRecv(brokerOptions, inNodeOptions, outNodeOptions, options, hoo
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
|
if(finished) { return }
|
||||||
if (hooks.done) { hooks.done(e); }
|
if (hooks.done) { hooks.done(e); }
|
||||||
else { throw e; }
|
else { throw e; }
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
if(finished) { return }
|
||||||
if (hooks.done) { hooks.done(err); }
|
if (hooks.done) { hooks.done(err); }
|
||||||
else { throw err; }
|
else { throw err; }
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user