2 * Copyright 2014 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 var should = require("should");
18 var sinon = require("sinon");
19 var path = require("path");
20 var fs = require('fs-extra');
22 var htmlNode = require("../../../../nodes/core/parsers/70-HTML.js");
23 var helper = require("../../helper.js");
25 describe('html node', function() {
27 var resourcesDir = __dirname+ path.sep + ".." + path.sep + ".." + path.sep + ".." + path.sep + "resources" + path.sep;
28 var file = path.join(resourcesDir, "70-HTML-test-file.html");
30 before(function(done) {
31 helper.startServer(done);
34 beforeEach(function() {
35 fs.existsSync(file).should.be.true;
38 afterEach(function() {
42 it('should be loaded', function(done) {
43 var flow = [{id:"htmlNode1", type:"html", name: "htmlNode" }];
44 helper.load(htmlNode, flow, function() {
45 var htmlNode1 = helper.getNode("htmlNode1");
46 htmlNode1.should.have.property('name', 'htmlNode');
51 it('should retrieve header contents as default', function(done) {
52 fs.readFile(file, 'utf8', function(err, data) {
53 var flow = [{id:"n1",type:"html",wires:[["n2"]],func:"return msg;"},
54 {id:"n2", type:"helper"}];
56 helper.load(htmlNode, flow, function() {
57 var n1 = helper.getNode("n1");
58 var n2 = helper.getNode("n2");
59 n2.on("input", function(msg) {
60 msg.should.have.property('topic', 'bar');
61 should.equal(msg.payload, 'This is a test page for node 70-HTML');
64 n1.receive({payload:data,topic: "bar"});
69 it('should retrieve paragraph contents when specified', function(done) {
70 fs.readFile(file, 'utf8', function(err, data) {
71 var flow = [{id:"n1",type:"html",wires:[["n2"]],ret:"text",tag:"p"},
72 {id:"n2", type:"helper"}];
74 helper.load(htmlNode, flow, function() {
75 var n1 = helper.getNode("n1");
76 var n2 = helper.getNode("n2");
77 n2.on("input", function(msg) {
78 msg.should.have.property('topic', 'bar');
79 should.equal(msg.payload, 'There\'s nothing to read here.');
82 n1.receive({payload:data,topic: "bar"});
87 it('should retrieve list contents as an array of html as default', function(done) {
88 fs.readFile(file, 'utf8', function(err, data) {
89 var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"ol"},
90 {id:"n2", type:"helper"}];
92 helper.load(htmlNode, flow, function() {
93 var n1 = helper.getNode("n1");
94 var n2 = helper.getNode("n2");
95 n2.on("input", function(msg) {
96 msg.should.have.property('topic', 'bar');
97 msg.payload[0].indexOf("<li>Blue</li>").should.be.above(-1);
98 msg.payload[0].indexOf("<li>Red</li>").should.be.above(-1);
101 n1.receive({payload:data,topic: "bar"});
106 it('should retrieve list contents as an array of text', function(done) {
107 fs.readFile(file, 'utf8', function(err, data) {
108 var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"ol",ret:"text"},
109 {id:"n2", type:"helper"}];
111 helper.load(htmlNode, flow, function() {
112 var n1 = helper.getNode("n1");
113 var n2 = helper.getNode("n2");
114 n2.on("input", function(msg) {
115 msg.should.have.property('topic', 'bar');
116 msg.payload.should.be.instanceof(Array).and.have.lengthOf(1);
117 msg.payload[0].indexOf("Blue").should.be.above(-1);
118 msg.payload[0].indexOf("Red").should.be.above(-1);
121 n1.receive({payload:data,topic: "bar"});
126 it('should log on error', function(done) {
127 fs.readFile(file,function(err, data) {
128 var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"p"},
129 {id:"n2", type:"helper"}];
131 helper.load(htmlNode, flow, function() {
132 var n1 = helper.getNode("n1");
133 var n2 = helper.getNode("n2");
134 n1.on("log", function(msg) {
135 msg.should.have.property('msg');
136 msg.msg.indexOf("Error:").should.be.above(-1);
137 msg.msg.should.startWith("Error:");
140 n1.receive({payload:null,topic: "bar"});
145 describe('multiple messages', function(){
148 afterEach(function() {
149 cnt.should.be.exactly(2);
153 it('should retrieve list contents as html as default with output as multiple msgs ', function(done) {
154 fs.readFile(file, 'utf8', function(err, data) {
155 var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"ul",as:"multi"},
156 {id:"n2", type:"helper"}];
158 helper.load(htmlNode, flow, function() {
159 var n1 = helper.getNode("n1");
160 var n2 = helper.getNode("n2");
161 n2.on("input", function(msg) {
163 msg.should.have.property('topic', 'bar');
164 if (cnt !== 1 && cnt !== 2) {
168 msg.payload.indexOf("<li>Apple</li>").should.be.above(-1);
169 msg.payload.indexOf("<li>Pear</li>").should.be.above(-1);
170 } else if (cnt === 2) {
171 msg.payload.indexOf("<li>Potato</li>").should.be.above(-1);
172 msg.payload.indexOf("<li>Parsnip</li>").should.be.above(-1);
176 n1.receive({payload:data,topic: "bar"});
181 it('should retrieve list contents as text with output as multiple msgs ', function(done) {
182 fs.readFile(file, 'utf8', function(err, data) {
183 var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"ul",ret:"text",as:"multi"},
184 {id:"n2", type:"helper"}];
186 helper.load(htmlNode, flow, function() {
187 var n1 = helper.getNode("n1");
188 var n2 = helper.getNode("n2");
189 n2.on("input", function(msg) {
191 msg.should.have.property('topic', 'bar');
192 if (cnt !== 1 && cnt !== 2) {
196 msg.payload.indexOf("Apple").should.be.above(-1);
197 msg.payload.indexOf("Pear").should.be.above(-1);
198 } else if (cnt === 2) {
199 msg.payload.indexOf("Potato").should.be.above(-1);
200 msg.payload.indexOf("Parsnip").should.be.above(-1);
204 n1.receive({payload:data,topic: "bar"});