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 debugNode = require("../../../../nodes/core/core/58-debug.js");
19 var helper = require("../../helper.js");
20 var WebSocket = require('ws');
22 describe('debug node', function() {
24 before(function(done) {
25 helper.startServer(done);
28 afterEach(function() {
33 it('should be loaded', function(done) {
34 var flow = [{id:"n1", type:"debug", name: "Debug" }];
35 helper.load(debugNode, flow, function() {
36 var n1 = helper.getNode("n1");
37 n1.should.have.property('name', 'Debug');
42 it('should publish on input', function(done) {
43 var flow = [{id:"n1", type:"debug", name: "Debug" }];
44 helper.load(debugNode, flow, function() {
45 var n1 = helper.getNode("n1");
46 websocket_test(function() {
47 n1.emit("input", {payload:"test"});
49 JSON.parse(msg).should.eql({
50 topic:"debug",data:{id:"n1",name:"Debug",msg:"test"}
56 it('should publish to console', function(done) {
57 var flow = [{id:"n1", type:"debug", console: "true" }];
58 helper.load(debugNode, flow, function() {
59 var n1 = helper.getNode("n1");
61 n1.on('log', function(msg) {
62 msg.should.eql({level:'log',id:'n1',type:'debug',msg:'test'});
68 websocket_test(function() {
69 n1.emit("input", {payload:"test"});
71 JSON.parse(msg).should.eql({
72 topic:"debug",data:{id:"n1",msg:"test"}
83 it('should publish complete message', function(done) {
84 var flow = [{id:"n1", type:"debug", complete: "true" }];
85 helper.load(debugNode, flow, function() {
86 var n1 = helper.getNode("n1");
87 websocket_test(function() {
88 n1.emit("input", {payload:"test"});
90 JSON.parse(msg).should.eql({
92 data:{id:"n1",msg:'(Object) {\n "payload": "test"\n}'}
98 it('should publish an Error', function(done) {
99 var flow = [{id:"n1", type:"debug" }];
100 helper.load(debugNode, flow, function() {
101 var n1 = helper.getNode("n1");
102 websocket_test(function() {
103 n1.emit("input", {payload: new Error("oops")});
105 JSON.parse(msg).should.eql({
106 topic:"debug",data:{id:"n1",msg:"Error: oops"}
112 it('should publish a boolean', function(done) {
113 var flow = [{id:"n1", type:"debug" }];
114 helper.load(debugNode, flow, function() {
115 var n1 = helper.getNode("n1");
116 websocket_test(function() {
117 n1.emit("input", {payload: true});
119 JSON.parse(msg).should.eql({
120 topic:"debug",data:{id:"n1",msg: '(boolean) true'}
126 it('should publish with no payload', function(done) {
127 var flow = [{id:"n1", type:"debug" }];
128 helper.load(debugNode, flow, function() {
129 var n1 = helper.getNode("n1");
130 websocket_test(function() {
131 n1.emit("input", {});
133 JSON.parse(msg).should.eql({
134 topic:"debug",data:{id:"n1",msg: '(undefined)'}
140 it('should publish an object', function(done) {
141 var flow = [{id:"n1", type:"debug" }];
142 helper.load(debugNode, flow, function() {
143 var n1 = helper.getNode("n1");
144 websocket_test(function() {
145 n1.emit("input", {payload: {type:'foo'}});
147 JSON.parse(msg).should.eql({
149 data:{id:"n1",msg:'(Object) {\n "type": "foo"\n}'}
155 it('should publish an array', function(done) {
156 var flow = [{id:"n1", type:"debug" }];
157 helper.load(debugNode, flow, function() {
158 var n1 = helper.getNode("n1");
159 websocket_test(function() {
160 n1.emit("input", {payload: [0,1,2,3]});
162 JSON.parse(msg).should.eql({
164 data:{id:"n1",msg: '(Array) [\n 0,\n 1,\n 2,\n 3\n]'}
170 it('should publish an object with circular references', function(done) {
171 var flow = [{id:"n1", type:"debug" }];
172 helper.load(debugNode, flow, function() {
173 var n1 = helper.getNode("n1");
174 websocket_test(function() {
175 var o = { name: 'bar' };
177 n1.emit("input", {payload: o});
179 JSON.parse(msg).should.eql({
183 msg:'(Object) {\n "name": "bar",\n "o": "[circular]"\n}'
190 it('should truncated a long message', function(done) {
191 var flow = [{id:"n1", type:"debug" }];
192 helper.load(debugNode, flow, function() {
193 var n1 = helper.getNode("n1");
194 websocket_test(function() {
195 n1.emit("input", {payload: Array(1002).join("X")});
197 JSON.parse(msg).should.eql({
201 msg: Array(1001).join("X")+' ....'
208 it('should convert Buffer to hex', function(done) {
209 var flow = [{id:"n1", type:"debug" }];
210 helper.load(debugNode, flow, function() {
211 var n1 = helper.getNode("n1");
212 websocket_test(function() {
213 n1.emit("input", {payload: new Buffer('HELLO', 'utf8')});
215 JSON.parse(msg).should.eql({
219 msg: '(Buffer) 48454c4c4f',
226 it('should publish when active', function(done) {
227 var flow = [{id:"n1", type:"debug", active: false }];
228 helper.load(debugNode, flow, function() {
229 var n1 = helper.getNode("n1");
230 websocket_test(function() {
231 n1.emit("input", {payload:"message 1"});
233 .post('/debug/n1/enable')
234 .expect(200).end(function(err) {
235 if (err) { return done(err); }
236 n1.emit("input", {payload:"message 2"});
239 JSON.parse(msg).should.eql({
240 topic:"debug",data:{id:"n1",msg:"message 2"}
246 it('should not publish when inactive', function(done) {
247 var flow = [{id:"n1", type:"debug", active: true }];
248 helper.load(debugNode, flow, function() {
249 var n1 = helper.getNode("n1");
250 websocket_test(function(close) {
252 .post('/debug/n1/disable')
253 .expect(201).end(function(err) {
258 n1.emit("input", {payload:"message"});
259 setTimeout(function() {
265 should.fail(null,null,"unexpected message");
270 describe('post', function() {
271 it('should return 404 on invalid state', function(done) {
272 var flow = [{id:"n1", type:"debug", active: true }];
273 helper.load(debugNode, flow, function() {
275 .post('/debug/n1/foobar')
276 .expect(404).end(done);
280 it('should return 404 on invalid node', function(done) {
282 .post('/debug/n99/enable')
283 .expect(404).end(done);
289 function websocket_test(open_callback, message_callback, done_callback) {
290 var ws = new WebSocket(helper.url() + "/comms");
291 var close_callback = function() { ws.close(); };
292 ws.on('open', function() { open_callback(close_callback); });
293 ws.on('message', function(msg) {
294 message_callback(msg, close_callback);