mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Extra tests for html, xml, json and tail nodes
(and some consistent passing of missing payloads)
This commit is contained in:
parent
72a9de058d
commit
fcc6943f98
@ -25,6 +25,7 @@ module.exports = function(RED) {
|
||||
this.as = n.as || "single";
|
||||
var node = this;
|
||||
this.on("input", function(msg) {
|
||||
if (msg.hasOwnProperty("payload")) {
|
||||
try {
|
||||
var $ = cheerio.load(msg.payload);
|
||||
var pay = [];
|
||||
@ -35,6 +36,7 @@ module.exports = function(RED) {
|
||||
if (node.ret === "text") { pay2 = $(this).text(); }
|
||||
//if (node.ret === "attr") { pay2 = $(this)[0]["attribs"]; }
|
||||
//if (node.ret === "val") { pay2 = $(this).val(); }
|
||||
/* istanbul ignore else */
|
||||
if (pay2) {
|
||||
msg.payload = pay2;
|
||||
node.send(msg);
|
||||
@ -54,6 +56,8 @@ module.exports = function(RED) {
|
||||
} catch (error) {
|
||||
node.error(error.message,msg);
|
||||
}
|
||||
}
|
||||
else { node.send(msg); } // If no payload - just pass it on.
|
||||
});
|
||||
}
|
||||
RED.nodes.registerType("html",CheerioNode);
|
||||
|
@ -31,15 +31,15 @@ module.exports = function(RED) {
|
||||
catch(e) { node.error(e.message,msg); }
|
||||
}
|
||||
else if (typeof msg.payload === "object") {
|
||||
if (!Buffer.isBuffer(msg.payload) ) {
|
||||
if (!util.isArray(msg.payload)) {
|
||||
if ((!Buffer.isBuffer(msg.payload)) && (!util.isArray(msg.payload))) {
|
||||
msg.payload = JSON.stringify(msg.payload);
|
||||
node.send(msg);
|
||||
}
|
||||
else { node.warn("Dropped: "+msg.payload); }
|
||||
}
|
||||
else { node.warn("Dropped: "+msg.payload); }
|
||||
}
|
||||
else { node.warn("dropped: "+msg.payload); }
|
||||
}
|
||||
else { node.send(msg); } // If no payload - just pass it on.
|
||||
});
|
||||
}
|
||||
RED.nodes.registerType("json",JSONNode);
|
||||
|
@ -42,6 +42,7 @@ module.exports = function(RED) {
|
||||
}
|
||||
else { node.warn("This node only handles xml strings or js objects."); }
|
||||
}
|
||||
else { node.send(msg); } // If no payload - just pass it on.
|
||||
});
|
||||
}
|
||||
RED.nodes.registerType("xml",XMLNode);
|
||||
|
@ -61,6 +61,7 @@ module.exports = function(RED) {
|
||||
});
|
||||
|
||||
this.on("close", function() {
|
||||
/* istanbul ignore else */
|
||||
if (tail) { tail.kill(); }
|
||||
});
|
||||
}
|
||||
|
@ -150,6 +150,24 @@ describe('html node', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('should pass through if payload empty', function(done) {
|
||||
fs.readFile(file, 'utf8', function(err, data) {
|
||||
var flow = [{id:"n1",type:"html",wires:[["n2"]],func:"return msg;"},
|
||||
{id:"n2", type:"helper"}];
|
||||
|
||||
helper.load(htmlNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.property('topic', 'bar');
|
||||
msg.should.not.have.property('payload');
|
||||
done();
|
||||
});
|
||||
n1.receive({topic: "bar"});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('multiple messages', function(){
|
||||
var cnt = 0;
|
||||
|
||||
|
@ -62,12 +62,11 @@ describe('JSON node', function() {
|
||||
var jn1 = helper.getNode("jn1");
|
||||
var jn2 = helper.getNode("jn2");
|
||||
jn2.on("input", function(msg) {
|
||||
msg.should.have.property('topic', 'bar');
|
||||
should.equal(msg.payload, '{"employees":[{"firstName":"John","lastName":"Smith"}]}');
|
||||
done();
|
||||
});
|
||||
var obj = {employees:[{firstName:"John", lastName:"Smith"}]};
|
||||
jn1.receive({payload:obj,topic: "bar"});
|
||||
jn1.receive({payload:obj});
|
||||
});
|
||||
});
|
||||
|
||||
@ -96,19 +95,47 @@ describe('JSON node', function() {
|
||||
var flow = [{id:"jn1",type:"json",wires:[["jn2"]],func:"return msg;"},
|
||||
{id:"jn2", type:"helper"}];
|
||||
helper.load(jsonNode, flow, function() {
|
||||
try {
|
||||
var jn1 = helper.getNode("jn1");
|
||||
var jn2 = helper.getNode("jn2");
|
||||
jn1.receive({payload:1,topic: "bar"});
|
||||
setTimeout(function() {
|
||||
try {
|
||||
var logEvents = helper.log().args.filter(function(evt) {
|
||||
return evt[0].type == "json";
|
||||
});
|
||||
logEvents.should.have.length(1);
|
||||
logEvents[0][0].should.have.a.property('msg',"dropped: 1");
|
||||
//console.log(logEvents);
|
||||
logEvents.should.have.length(4);
|
||||
logEvents[0][0].should.have.a.property('msg');
|
||||
logEvents[0][0].msg.toString().should.startWith('Dropped: ');
|
||||
logEvents[1][0].should.have.a.property('msg');
|
||||
logEvents[1][0].msg.toString().should.startWith('Dropped: ');
|
||||
logEvents[2][0].should.have.a.property('msg');
|
||||
logEvents[2][0].msg.toString().should.startWith('Dropped: ');
|
||||
logEvents[3][0].should.have.a.property('msg');
|
||||
logEvents[3][0].msg.toString().should.startWith('Dropped: ');
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
},150);
|
||||
jn1.receive({payload:true});
|
||||
jn1.receive({payload:1});
|
||||
jn1.receive({payload:["a"]});
|
||||
jn1.receive({payload:new Buffer("a")});
|
||||
});
|
||||
});
|
||||
|
||||
it('should pass straight through if no payload set', function(done) {
|
||||
var flow = [{id:"jn1",type:"json",wires:[["jn2"]],func:"return msg;"},
|
||||
{id:"jn2", type:"helper"}];
|
||||
helper.load(jsonNode, flow, function() {
|
||||
var jn1 = helper.getNode("jn1");
|
||||
var jn2 = helper.getNode("jn2");
|
||||
jn2.on("input", function(msg) {
|
||||
msg.should.have.property('topic', 'bar');
|
||||
msg.should.not.have.property('payload');
|
||||
done();
|
||||
});
|
||||
jn1.receive({topic: "bar"});
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -120,4 +120,19 @@ describe('XML node', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('should just pass through if payload is missing', function(done) {
|
||||
var flow = [{id:"n1",type:"xml",wires:[["n2"]],func:"return msg;"},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(xmlNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.property('topic', 'bar');
|
||||
msg.should.not.have.property('payload');
|
||||
done();
|
||||
});
|
||||
n1.receive({topic: "bar"});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
@ -18,12 +18,14 @@ var should = require("should");
|
||||
var path = require('path');
|
||||
var fs = require('fs-extra');
|
||||
var mkdirp = require('mkdirp');
|
||||
var sinon = require('sinon');
|
||||
|
||||
var tailNode = require("../../../../nodes/core/storage/28-tail.js");
|
||||
var helper = require("../../helper.js");
|
||||
|
||||
describe('TailNode', function() {
|
||||
describe('tail Node', function() {
|
||||
|
||||
var wait = 150;
|
||||
var resourcesDir = path.join(__dirname,"..","..","..","resources");
|
||||
var fileToTail = path.join(resourcesDir,"28-tail-test-file.txt");
|
||||
|
||||
@ -48,7 +50,7 @@ describe('TailNode', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('tail should tail a file', function(done) {
|
||||
it('should tail a file', function(done) {
|
||||
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]},
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
helper.load(tailNode, flow, function() {
|
||||
@ -66,11 +68,11 @@ describe('TailNode', function() {
|
||||
setTimeout( function() {
|
||||
fs.appendFileSync(fileToTail, "Tail message line 3\n");
|
||||
fs.appendFileSync(fileToTail, "Tail message line 4\n");
|
||||
},100);
|
||||
},wait);
|
||||
});
|
||||
});
|
||||
|
||||
it('tail should work in non-split mode', function(done) {
|
||||
it('should work in non-split mode', function(done) {
|
||||
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":false, "filename":fileToTail, "wires":[["helperNode1"]]},
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
helper.load(tailNode, flow, function() {
|
||||
@ -84,11 +86,11 @@ describe('TailNode', function() {
|
||||
});
|
||||
setTimeout( function() {
|
||||
fs.appendFileSync(fileToTail, "Tail message line 5\nTail message line 6\n");
|
||||
},150);
|
||||
},wait);
|
||||
});
|
||||
});
|
||||
|
||||
it('tail should handle a non-existent file', function(done) {
|
||||
it('should handle a non-existent file', function(done) {
|
||||
fs.unlinkSync(fileToTail);
|
||||
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]},
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
@ -101,10 +103,22 @@ describe('TailNode', function() {
|
||||
done();
|
||||
});
|
||||
setTimeout( function() {
|
||||
fs.writeFileSync(fileToTail, "Tail message line\n");
|
||||
},150);
|
||||
fs.writeFile(fileToTail, "Tail message line\n");
|
||||
},wait);
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw an error if run on Windows', function(done) {
|
||||
// Stub os platform so we can make it look like windows
|
||||
var os = require('os');
|
||||
var spy = sinon.stub(os, 'platform', function(arg){ return("windows"); });
|
||||
|
||||
/*jshint immed: false */
|
||||
(function() { tailNode("1234"); }).should.throw();
|
||||
os.platform.restore();
|
||||
done();
|
||||
});
|
||||
|
||||
/*
|
||||
it('tail should handle file truncation', function(done) {
|
||||
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]},
|
||||
@ -157,7 +171,7 @@ describe('TailNode', function() {
|
||||
}
|
||||
setTimeout( function() {
|
||||
processAction();
|
||||
},150);
|
||||
},wait);
|
||||
});
|
||||
});
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user