Skip to content

Commit fcc6943

Browse files
author
dceejay
committed
Extra tests for html, xml, json and tail nodes
(and some consistent passing of missing payloads)
1 parent 72a9de0 commit fcc6943

8 files changed

Lines changed: 139 additions & 59 deletions

File tree

nodes/core/parsers/70-HTML.js

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,39 @@ module.exports = function(RED) {
2525
this.as = n.as || "single";
2626
var node = this;
2727
this.on("input", function(msg) {
28-
try {
29-
var $ = cheerio.load(msg.payload);
30-
var pay = [];
31-
$(node.tag).each(function() {
32-
if (node.as === "multi") {
33-
var pay2 = null;
34-
if (node.ret === "html") { pay2 = $(this).html(); }
35-
if (node.ret === "text") { pay2 = $(this).text(); }
36-
//if (node.ret === "attr") { pay2 = $(this)[0]["attribs"]; }
37-
//if (node.ret === "val") { pay2 = $(this).val(); }
38-
if (pay2) {
39-
msg.payload = pay2;
40-
node.send(msg);
28+
if (msg.hasOwnProperty("payload")) {
29+
try {
30+
var $ = cheerio.load(msg.payload);
31+
var pay = [];
32+
$(node.tag).each(function() {
33+
if (node.as === "multi") {
34+
var pay2 = null;
35+
if (node.ret === "html") { pay2 = $(this).html(); }
36+
if (node.ret === "text") { pay2 = $(this).text(); }
37+
//if (node.ret === "attr") { pay2 = $(this)[0]["attribs"]; }
38+
//if (node.ret === "val") { pay2 = $(this).val(); }
39+
/* istanbul ignore else */
40+
if (pay2) {
41+
msg.payload = pay2;
42+
node.send(msg);
43+
}
4144
}
45+
if (node.as === "single") {
46+
if (node.ret === "html") { pay.push( $(this).html() ); }
47+
if (node.ret === "text") { pay.push( $(this).text() ); }
48+
//if (node.ret === "attr") { pay.push( $(this)[0]["attribs"] ); }
49+
//if (node.ret === "val") { pay.push( $(this).val() ); }
50+
}
51+
});
52+
if ((node.as === "single") && (pay.length !== 0)) {
53+
msg.payload = pay;
54+
node.send(msg);
4255
}
43-
if (node.as === "single") {
44-
if (node.ret === "html") { pay.push( $(this).html() ); }
45-
if (node.ret === "text") { pay.push( $(this).text() ); }
46-
//if (node.ret === "attr") { pay.push( $(this)[0]["attribs"] ); }
47-
//if (node.ret === "val") { pay.push( $(this).val() ); }
48-
}
49-
});
50-
if ((node.as === "single") && (pay.length !== 0)) {
51-
msg.payload = pay;
52-
node.send(msg);
56+
} catch (error) {
57+
node.error(error.message,msg);
5358
}
54-
} catch (error) {
55-
node.error(error.message,msg);
5659
}
60+
else { node.send(msg); } // If no payload - just pass it on.
5761
});
5862
}
5963
RED.nodes.registerType("html",CheerioNode);

nodes/core/parsers/70-JSON.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ module.exports = function(RED) {
3131
catch(e) { node.error(e.message,msg); }
3232
}
3333
else if (typeof msg.payload === "object") {
34-
if (!Buffer.isBuffer(msg.payload) ) {
35-
if (!util.isArray(msg.payload)) {
36-
msg.payload = JSON.stringify(msg.payload);
37-
node.send(msg);
38-
}
34+
if ((!Buffer.isBuffer(msg.payload)) && (!util.isArray(msg.payload))) {
35+
msg.payload = JSON.stringify(msg.payload);
36+
node.send(msg);
3937
}
38+
else { node.warn("Dropped: "+msg.payload); }
4039
}
41-
else { node.warn("dropped: "+msg.payload); }
40+
else { node.warn("Dropped: "+msg.payload); }
4241
}
42+
else { node.send(msg); } // If no payload - just pass it on.
4343
});
4444
}
4545
RED.nodes.registerType("json",JSONNode);

nodes/core/parsers/70-XML.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module.exports = function(RED) {
4242
}
4343
else { node.warn("This node only handles xml strings or js objects."); }
4444
}
45+
else { node.send(msg); } // If no payload - just pass it on.
4546
});
4647
}
4748
RED.nodes.registerType("xml",XMLNode);

nodes/core/storage/28-tail.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ module.exports = function(RED) {
6161
});
6262

6363
this.on("close", function() {
64+
/* istanbul ignore else */
6465
if (tail) { tail.kill(); }
6566
});
6667
}

test/nodes/core/parsers/70-HTML_spec.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,25 @@ describe('html node', function() {
149149
});
150150
});
151151
});
152-
152+
153+
it('should pass through if payload empty', function(done) {
154+
fs.readFile(file, 'utf8', function(err, data) {
155+
var flow = [{id:"n1",type:"html",wires:[["n2"]],func:"return msg;"},
156+
{id:"n2", type:"helper"}];
157+
158+
helper.load(htmlNode, flow, function() {
159+
var n1 = helper.getNode("n1");
160+
var n2 = helper.getNode("n2");
161+
n2.on("input", function(msg) {
162+
msg.should.have.property('topic', 'bar');
163+
msg.should.not.have.property('payload');
164+
done();
165+
});
166+
n1.receive({topic: "bar"});
167+
});
168+
});
169+
});
170+
153171
describe('multiple messages', function(){
154172
var cnt = 0;
155173

test/nodes/core/parsers/70-JSON_spec.js

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,11 @@ describe('JSON node', function() {
6262
var jn1 = helper.getNode("jn1");
6363
var jn2 = helper.getNode("jn2");
6464
jn2.on("input", function(msg) {
65-
msg.should.have.property('topic', 'bar');
6665
should.equal(msg.payload, '{"employees":[{"firstName":"John","lastName":"Smith"}]}');
6766
done();
6867
});
6968
var obj = {employees:[{firstName:"John", lastName:"Smith"}]};
70-
jn1.receive({payload:obj,topic: "bar"});
69+
jn1.receive({payload:obj});
7170
});
7271
});
7372

@@ -96,20 +95,48 @@ describe('JSON node', function() {
9695
var flow = [{id:"jn1",type:"json",wires:[["jn2"]],func:"return msg;"},
9796
{id:"jn2", type:"helper"}];
9897
helper.load(jsonNode, flow, function() {
99-
try {
100-
var jn1 = helper.getNode("jn1");
101-
var jn2 = helper.getNode("jn2");
102-
jn1.receive({payload:1,topic: "bar"});
103-
var logEvents = helper.log().args.filter(function(evt) {
104-
return evt[0].type == "json";
105-
});
106-
logEvents.should.have.length(1);
107-
logEvents[0][0].should.have.a.property('msg',"dropped: 1");
98+
var jn1 = helper.getNode("jn1");
99+
var jn2 = helper.getNode("jn2");
100+
setTimeout(function() {
101+
try {
102+
var logEvents = helper.log().args.filter(function(evt) {
103+
return evt[0].type == "json";
104+
});
105+
//console.log(logEvents);
106+
logEvents.should.have.length(4);
107+
logEvents[0][0].should.have.a.property('msg');
108+
logEvents[0][0].msg.toString().should.startWith('Dropped: ');
109+
logEvents[1][0].should.have.a.property('msg');
110+
logEvents[1][0].msg.toString().should.startWith('Dropped: ');
111+
logEvents[2][0].should.have.a.property('msg');
112+
logEvents[2][0].msg.toString().should.startWith('Dropped: ');
113+
logEvents[3][0].should.have.a.property('msg');
114+
logEvents[3][0].msg.toString().should.startWith('Dropped: ');
115+
done();
116+
} catch(err) {
117+
done(err);
118+
}
119+
},150);
120+
jn1.receive({payload:true});
121+
jn1.receive({payload:1});
122+
jn1.receive({payload:["a"]});
123+
jn1.receive({payload:new Buffer("a")});
124+
});
125+
});
126+
127+
it('should pass straight through if no payload set', function(done) {
128+
var flow = [{id:"jn1",type:"json",wires:[["jn2"]],func:"return msg;"},
129+
{id:"jn2", type:"helper"}];
130+
helper.load(jsonNode, flow, function() {
131+
var jn1 = helper.getNode("jn1");
132+
var jn2 = helper.getNode("jn2");
133+
jn2.on("input", function(msg) {
134+
msg.should.have.property('topic', 'bar');
135+
msg.should.not.have.property('payload');
108136
done();
109-
} catch(err) {
110-
done(err);
111-
}
137+
});
138+
jn1.receive({topic: "bar"});
112139
});
113140
});
114-
141+
115142
});

test/nodes/core/parsers/70-XML_spec.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var helper = require("../../helper.js");
2121
describe('XML node', function() {
2222

2323
before(function(done) {
24-
helper.startServer(done);
24+
helper.startServer(done);
2525
});
2626

2727
afterEach(function() {
@@ -120,4 +120,19 @@ describe('XML node', function() {
120120
});
121121
});
122122

123+
it('should just pass through if payload is missing', function(done) {
124+
var flow = [{id:"n1",type:"xml",wires:[["n2"]],func:"return msg;"},
125+
{id:"n2", type:"helper"}];
126+
helper.load(xmlNode, flow, function() {
127+
var n1 = helper.getNode("n1");
128+
var n2 = helper.getNode("n2");
129+
n2.on("input", function(msg) {
130+
msg.should.have.property('topic', 'bar');
131+
msg.should.not.have.property('payload');
132+
done();
133+
});
134+
n1.receive({topic: "bar"});
135+
});
136+
});
137+
123138
});

test/nodes/core/storage/28-tail_spec.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ var should = require("should");
1818
var path = require('path');
1919
var fs = require('fs-extra');
2020
var mkdirp = require('mkdirp');
21+
var sinon = require('sinon');
2122

2223
var tailNode = require("../../../../nodes/core/storage/28-tail.js");
2324
var helper = require("../../helper.js");
2425

25-
describe('TailNode', function() {
26+
describe('tail Node', function() {
2627

28+
var wait = 150;
2729
var resourcesDir = path.join(__dirname,"..","..","..","resources");
2830
var fileToTail = path.join(resourcesDir,"28-tail-test-file.txt");
2931

@@ -48,7 +50,7 @@ describe('TailNode', function() {
4850
});
4951
});
5052

51-
it('tail should tail a file', function(done) {
53+
it('should tail a file', function(done) {
5254
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]},
5355
{id:"helperNode1", type:"helper", wires:[]}];
5456
helper.load(tailNode, flow, function() {
@@ -66,11 +68,11 @@ describe('TailNode', function() {
6668
setTimeout( function() {
6769
fs.appendFileSync(fileToTail, "Tail message line 3\n");
6870
fs.appendFileSync(fileToTail, "Tail message line 4\n");
69-
},100);
71+
},wait);
7072
});
7173
});
7274

73-
it('tail should work in non-split mode', function(done) {
75+
it('should work in non-split mode', function(done) {
7476
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":false, "filename":fileToTail, "wires":[["helperNode1"]]},
7577
{id:"helperNode1", type:"helper", wires:[]}];
7678
helper.load(tailNode, flow, function() {
@@ -84,11 +86,11 @@ describe('TailNode', function() {
8486
});
8587
setTimeout( function() {
8688
fs.appendFileSync(fileToTail, "Tail message line 5\nTail message line 6\n");
87-
},150);
89+
},wait);
8890
});
8991
});
9092

91-
it('tail should handle a non-existent file', function(done) {
93+
it('should handle a non-existent file', function(done) {
9294
fs.unlinkSync(fileToTail);
9395
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]},
9496
{id:"helperNode1", type:"helper", wires:[]}];
@@ -101,10 +103,22 @@ describe('TailNode', function() {
101103
done();
102104
});
103105
setTimeout( function() {
104-
fs.writeFileSync(fileToTail, "Tail message line\n");
105-
},150);
106+
fs.writeFile(fileToTail, "Tail message line\n");
107+
},wait);
106108
});
107109
});
110+
111+
it('should throw an error if run on Windows', function(done) {
112+
// Stub os platform so we can make it look like windows
113+
var os = require('os');
114+
var spy = sinon.stub(os, 'platform', function(arg){ return("windows"); });
115+
116+
/*jshint immed: false */
117+
(function() { tailNode("1234"); }).should.throw();
118+
os.platform.restore();
119+
done();
120+
});
121+
108122
/*
109123
it('tail should handle file truncation', function(done) {
110124
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]},
@@ -130,7 +144,7 @@ describe('TailNode', function() {
130144
} else {
131145
msg.payload.should.equal("Tail message line append "+inputCounter);
132146
}
133-
147+
134148
if (inputCounter === 5) {
135149
setTimeout(function() {
136150
warned.should.be.true;
@@ -145,7 +159,7 @@ describe('TailNode', function() {
145159
function() { fs.appendFileSync(fileToTail, "Tail message line append 4\n");},
146160
function() { fs.appendFileSync(fileToTail, "Tail message line append 5\n");}
147161
];
148-
162+
149163
function processAction() {
150164
var action = actions.shift();
151165
action();
@@ -157,7 +171,7 @@ describe('TailNode', function() {
157171
}
158172
setTimeout( function() {
159173
processAction();
160-
},150);
174+
},wait);
161175
});
162176
});
163177
*/

0 commit comments

Comments
 (0)