Extra tests for html, xml, json and tail nodes

(and some consistent passing of missing payloads)
This commit is contained in:
dceejay 2015-03-24 17:43:47 +00:00
parent 72a9de058d
commit fcc6943f98
8 changed files with 140 additions and 60 deletions

View File

@ -25,35 +25,39 @@ module.exports = function(RED) {
this.as = n.as || "single"; this.as = n.as || "single";
var node = this; var node = this;
this.on("input", function(msg) { this.on("input", function(msg) {
try { if (msg.hasOwnProperty("payload")) {
var $ = cheerio.load(msg.payload); try {
var pay = []; var $ = cheerio.load(msg.payload);
$(node.tag).each(function() { var pay = [];
if (node.as === "multi") { $(node.tag).each(function() {
var pay2 = null; if (node.as === "multi") {
if (node.ret === "html") { pay2 = $(this).html(); } var pay2 = null;
if (node.ret === "text") { pay2 = $(this).text(); } if (node.ret === "html") { pay2 = $(this).html(); }
//if (node.ret === "attr") { pay2 = $(this)[0]["attribs"]; } if (node.ret === "text") { pay2 = $(this).text(); }
//if (node.ret === "val") { pay2 = $(this).val(); } //if (node.ret === "attr") { pay2 = $(this)[0]["attribs"]; }
if (pay2) { //if (node.ret === "val") { pay2 = $(this).val(); }
msg.payload = pay2; /* istanbul ignore else */
node.send(msg); if (pay2) {
msg.payload = pay2;
node.send(msg);
}
} }
if (node.as === "single") {
if (node.ret === "html") { pay.push( $(this).html() ); }
if (node.ret === "text") { pay.push( $(this).text() ); }
//if (node.ret === "attr") { pay.push( $(this)[0]["attribs"] ); }
//if (node.ret === "val") { pay.push( $(this).val() ); }
}
});
if ((node.as === "single") && (pay.length !== 0)) {
msg.payload = pay;
node.send(msg);
} }
if (node.as === "single") { } catch (error) {
if (node.ret === "html") { pay.push( $(this).html() ); } node.error(error.message,msg);
if (node.ret === "text") { pay.push( $(this).text() ); }
//if (node.ret === "attr") { pay.push( $(this)[0]["attribs"] ); }
//if (node.ret === "val") { pay.push( $(this).val() ); }
}
});
if ((node.as === "single") && (pay.length !== 0)) {
msg.payload = pay;
node.send(msg);
} }
} catch (error) {
node.error(error.message,msg);
} }
else { node.send(msg); } // If no payload - just pass it on.
}); });
} }
RED.nodes.registerType("html",CheerioNode); RED.nodes.registerType("html",CheerioNode);

View File

@ -31,15 +31,15 @@ module.exports = function(RED) {
catch(e) { node.error(e.message,msg); } catch(e) { node.error(e.message,msg); }
} }
else if (typeof msg.payload === "object") { else if (typeof msg.payload === "object") {
if (!Buffer.isBuffer(msg.payload) ) { if ((!Buffer.isBuffer(msg.payload)) && (!util.isArray(msg.payload))) {
if (!util.isArray(msg.payload)) { msg.payload = JSON.stringify(msg.payload);
msg.payload = JSON.stringify(msg.payload); node.send(msg);
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); RED.nodes.registerType("json",JSONNode);

View File

@ -42,6 +42,7 @@ module.exports = function(RED) {
} }
else { node.warn("This node only handles xml strings or js objects."); } 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); RED.nodes.registerType("xml",XMLNode);

View File

@ -61,6 +61,7 @@ module.exports = function(RED) {
}); });
this.on("close", function() { this.on("close", function() {
/* istanbul ignore else */
if (tail) { tail.kill(); } if (tail) { tail.kill(); }
}); });
} }

View File

@ -149,7 +149,25 @@ 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(){ describe('multiple messages', function(){
var cnt = 0; var cnt = 0;

View File

@ -62,12 +62,11 @@ describe('JSON node', function() {
var jn1 = helper.getNode("jn1"); var jn1 = helper.getNode("jn1");
var jn2 = helper.getNode("jn2"); var jn2 = helper.getNode("jn2");
jn2.on("input", function(msg) { jn2.on("input", function(msg) {
msg.should.have.property('topic', 'bar');
should.equal(msg.payload, '{"employees":[{"firstName":"John","lastName":"Smith"}]}'); should.equal(msg.payload, '{"employees":[{"firstName":"John","lastName":"Smith"}]}');
done(); done();
}); });
var obj = {employees:[{firstName:"John", lastName:"Smith"}]}; var obj = {employees:[{firstName:"John", lastName:"Smith"}]};
jn1.receive({payload:obj,topic: "bar"}); jn1.receive({payload:obj});
}); });
}); });
@ -96,20 +95,48 @@ describe('JSON node', function() {
var flow = [{id:"jn1",type:"json",wires:[["jn2"]],func:"return msg;"}, var flow = [{id:"jn1",type:"json",wires:[["jn2"]],func:"return msg;"},
{id:"jn2", type:"helper"}]; {id:"jn2", type:"helper"}];
helper.load(jsonNode, flow, function() { helper.load(jsonNode, flow, function() {
try { var jn1 = helper.getNode("jn1");
var jn1 = helper.getNode("jn1"); var jn2 = helper.getNode("jn2");
var jn2 = helper.getNode("jn2"); setTimeout(function() {
jn1.receive({payload:1,topic: "bar"}); try {
var logEvents = helper.log().args.filter(function(evt) { var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "json"; return evt[0].type == "json";
}); });
logEvents.should.have.length(1); //console.log(logEvents);
logEvents[0][0].should.have.a.property('msg',"dropped: 1"); logEvents.should.have.length(4);
done(); logEvents[0][0].should.have.a.property('msg');
} catch(err) { logEvents[0][0].msg.toString().should.startWith('Dropped: ');
done(err); 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"});
});
});
}); });

View File

@ -21,7 +21,7 @@ var helper = require("../../helper.js");
describe('XML node', function() { describe('XML node', function() {
before(function(done) { before(function(done) {
helper.startServer(done); helper.startServer(done);
}); });
afterEach(function() { afterEach(function() {
@ -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"});
});
});
}); });

View File

@ -18,12 +18,14 @@ var should = require("should");
var path = require('path'); var path = require('path');
var fs = require('fs-extra'); var fs = require('fs-extra');
var mkdirp = require('mkdirp'); var mkdirp = require('mkdirp');
var sinon = require('sinon');
var tailNode = require("../../../../nodes/core/storage/28-tail.js"); var tailNode = require("../../../../nodes/core/storage/28-tail.js");
var helper = require("../../helper.js"); var helper = require("../../helper.js");
describe('TailNode', function() { describe('tail Node', function() {
var wait = 150;
var resourcesDir = path.join(__dirname,"..","..","..","resources"); var resourcesDir = path.join(__dirname,"..","..","..","resources");
var fileToTail = path.join(resourcesDir,"28-tail-test-file.txt"); 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"]]}, var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}]; {id:"helperNode1", type:"helper", wires:[]}];
helper.load(tailNode, flow, function() { helper.load(tailNode, flow, function() {
@ -66,11 +68,11 @@ describe('TailNode', function() {
setTimeout( function() { setTimeout( function() {
fs.appendFileSync(fileToTail, "Tail message line 3\n"); fs.appendFileSync(fileToTail, "Tail message line 3\n");
fs.appendFileSync(fileToTail, "Tail message line 4\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"]]}, var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":false, "filename":fileToTail, "wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}]; {id:"helperNode1", type:"helper", wires:[]}];
helper.load(tailNode, flow, function() { helper.load(tailNode, flow, function() {
@ -84,11 +86,11 @@ describe('TailNode', function() {
}); });
setTimeout( function() { setTimeout( function() {
fs.appendFileSync(fileToTail, "Tail message line 5\nTail message line 6\n"); 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); fs.unlinkSync(fileToTail);
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]}, var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}]; {id:"helperNode1", type:"helper", wires:[]}];
@ -101,10 +103,22 @@ describe('TailNode', function() {
done(); done();
}); });
setTimeout( function() { setTimeout( function() {
fs.writeFileSync(fileToTail, "Tail message line\n"); fs.writeFile(fileToTail, "Tail message line\n");
},150); },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) { it('tail should handle file truncation', function(done) {
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]}, var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]},
@ -130,7 +144,7 @@ describe('TailNode', function() {
} else { } else {
msg.payload.should.equal("Tail message line append "+inputCounter); msg.payload.should.equal("Tail message line append "+inputCounter);
} }
if (inputCounter === 5) { if (inputCounter === 5) {
setTimeout(function() { setTimeout(function() {
warned.should.be.true; warned.should.be.true;
@ -145,7 +159,7 @@ describe('TailNode', function() {
function() { fs.appendFileSync(fileToTail, "Tail message line append 4\n");}, function() { fs.appendFileSync(fileToTail, "Tail message line append 4\n");},
function() { fs.appendFileSync(fileToTail, "Tail message line append 5\n");} function() { fs.appendFileSync(fileToTail, "Tail message line append 5\n");}
]; ];
function processAction() { function processAction() {
var action = actions.shift(); var action = actions.shift();
action(); action();
@ -157,7 +171,7 @@ describe('TailNode', function() {
} }
setTimeout( function() { setTimeout( function() {
processAction(); processAction();
},150); },wait);
}); });
}); });
*/ */