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,35 +25,39 @@ module.exports = function(RED) {
|
||||
this.as = n.as || "single";
|
||||
var node = this;
|
||||
this.on("input", function(msg) {
|
||||
try {
|
||||
var $ = cheerio.load(msg.payload);
|
||||
var pay = [];
|
||||
$(node.tag).each(function() {
|
||||
if (node.as === "multi") {
|
||||
var pay2 = null;
|
||||
if (node.ret === "html") { pay2 = $(this).html(); }
|
||||
if (node.ret === "text") { pay2 = $(this).text(); }
|
||||
//if (node.ret === "attr") { pay2 = $(this)[0]["attribs"]; }
|
||||
//if (node.ret === "val") { pay2 = $(this).val(); }
|
||||
if (pay2) {
|
||||
msg.payload = pay2;
|
||||
node.send(msg);
|
||||
if (msg.hasOwnProperty("payload")) {
|
||||
try {
|
||||
var $ = cheerio.load(msg.payload);
|
||||
var pay = [];
|
||||
$(node.tag).each(function() {
|
||||
if (node.as === "multi") {
|
||||
var pay2 = null;
|
||||
if (node.ret === "html") { pay2 = $(this).html(); }
|
||||
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);
|
||||
}
|
||||
}
|
||||
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") {
|
||||
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);
|
||||
} catch (error) {
|
||||
node.error(error.message,msg);
|
||||
}
|
||||
} 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)) {
|
||||
msg.payload = JSON.stringify(msg.payload);
|
||||
node.send(msg);
|
||||
}
|
||||
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(); }
|
||||
});
|
||||
}
|
||||
|
@ -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(){
|
||||
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,20 +95,48 @@ 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"});
|
||||
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");
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
var jn1 = helper.getNode("jn1");
|
||||
var jn2 = helper.getNode("jn2");
|
||||
setTimeout(function() {
|
||||
try {
|
||||
var logEvents = helper.log().args.filter(function(evt) {
|
||||
return evt[0].type == "json";
|
||||
});
|
||||
//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"});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
@ -21,7 +21,7 @@ var helper = require("../../helper.js");
|
||||
describe('XML node', function() {
|
||||
|
||||
before(function(done) {
|
||||
helper.startServer(done);
|
||||
helper.startServer(done);
|
||||
});
|
||||
|
||||
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"});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
@ -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"]]},
|
||||
@ -130,7 +144,7 @@ describe('TailNode', function() {
|
||||
} else {
|
||||
msg.payload.should.equal("Tail message line append "+inputCounter);
|
||||
}
|
||||
|
||||
|
||||
if (inputCounter === 5) {
|
||||
setTimeout(function() {
|
||||
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 5\n");}
|
||||
];
|
||||
|
||||
|
||||
function processAction() {
|
||||
var action = actions.shift();
|
||||
action();
|
||||
@ -157,7 +171,7 @@ describe('TailNode', function() {
|
||||
}
|
||||
setTimeout( function() {
|
||||
processAction();
|
||||
},150);
|
||||
},wait);
|
||||
});
|
||||
});
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user