mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
da8dd1dc30
geohash, smooth, base64, msgpack
116 lines
4.1 KiB
JavaScript
116 lines
4.1 KiB
JavaScript
/**
|
|
* Copyright 2016 IBM Corp.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
**/
|
|
|
|
var should = require("should");
|
|
var helper = require('../../../test/helper.js');
|
|
var testNode = require('../../../parsers/msgpack/70-msgpack.js');
|
|
|
|
describe('msgpack node', function() {
|
|
"use strict";
|
|
|
|
beforeEach(function(done) {
|
|
helper.startServer(done);
|
|
});
|
|
|
|
afterEach(function(done) {
|
|
helper.unload().then(function() {
|
|
helper.stopServer(done);
|
|
});
|
|
});
|
|
|
|
it("should be loaded with correct defaults", function(done) {
|
|
var flow = [{"id":"n1", "type":"msgpack", "name":"msgpack1", "wires":[[]]}];
|
|
helper.load(testNode, flow, function() {
|
|
var n1 = helper.getNode("n1");
|
|
n1.should.have.property("name", "msgpack1");
|
|
done();
|
|
});
|
|
});
|
|
|
|
var buf;
|
|
|
|
it('should pack an object', function(done) {
|
|
var flow = [{"id":"n1", "type":"msgpack", wires:[["n2"]] },
|
|
{id:"n2", type:"helper"} ];
|
|
helper.load(testNode, flow, function() {
|
|
var n1 = helper.getNode("n1");
|
|
var n2 = helper.getNode("n2");
|
|
n2.on("input", function(msg) {
|
|
msg.should.have.a.property("payload");
|
|
msg.payload.should.be.a.Object;
|
|
msg.payload.should.have.length(43);
|
|
buf = msg.payload;
|
|
done();
|
|
});
|
|
n1.emit("input", {payload:{A:1, B:"string", C:true, D:[1,true,"string"], E:{Y:9,Z:"string"}}});
|
|
});
|
|
});
|
|
|
|
it('should unpack a Buffer', function(done) {
|
|
var flow = [{"id":"n1", "type":"msgpack", wires:[["n2"]] },
|
|
{id:"n2", type:"helper"} ];
|
|
helper.load(testNode, flow, function() {
|
|
var n1 = helper.getNode("n1");
|
|
var n2 = helper.getNode("n2");
|
|
n2.on("input", function(msg) {
|
|
msg.should.have.a.property("payload");
|
|
msg.payload.should.have.a.property("A",1);
|
|
msg.payload.should.have.a.property("B",'string');
|
|
msg.payload.should.have.a.property("C",true);
|
|
msg.payload.should.have.a.property("D",[1,true,"string"]);
|
|
msg.payload.should.have.a.property("E");
|
|
msg.payload.E.should.have.a.property("Y",9);
|
|
msg.payload.E.should.have.a.property("Z","string");
|
|
done();
|
|
});
|
|
n1.emit("input", {payload:buf});
|
|
});
|
|
});
|
|
|
|
it('should error if the buffer fails to decode', function(done) {
|
|
var flow = [{"id":"n1", "type":"msgpack", wires:[["n2"]] },
|
|
{id:"n2", type:"helper"} ];
|
|
helper.load(testNode, flow, function() {
|
|
var n1 = helper.getNode("n1");
|
|
var n2 = helper.getNode("n2");
|
|
n2.on("input", function(msg) {
|
|
done("should not get here if there is an error.");
|
|
});
|
|
setTimeout(function() {
|
|
done();
|
|
}, 25);
|
|
n1.emit("input", {payload:Buffer("12345")});
|
|
});
|
|
});
|
|
|
|
it('ignore msg with no payload', function(done) {
|
|
var flow = [{"id":"n1", "type":"msgpack", wires:[["n2"]] },
|
|
{id:"n2", type:"helper"} ];
|
|
helper.load(testNode, flow, function() {
|
|
var n1 = helper.getNode("n1");
|
|
var n2 = helper.getNode("n2");
|
|
n2.on("input", function(msg) {
|
|
done("should not get here with no payload.");
|
|
});
|
|
setTimeout(function() {
|
|
done();
|
|
}, 25);
|
|
n1.emit("input", {topic:1});
|
|
});
|
|
});
|
|
|
|
});
|