lwm2m
Version:
Library for developing servers and client of OMA Lightweight M2M
198 lines (163 loc) • 5.38 kB
JavaScript
/*
* Copyright 2017 Alexandre Moreno <alex_moreno@tutk.com>
*
* This file is part of node-lwm2m
*
* node-lwm2m is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* node-lwm2m is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with node-lwm2m.
* If not, seehttp://www.gnu.org/licenses/.
*
* For those usages not covered by the GNU Affero General Public License
* please contact with::[contacto@tid.es]
*/
;
var should = require('should');
var lwm2m = require('../../');
var coap = require('coap');
var Writable = require('readable-stream').Writable;
var Readable = require('readable-stream').Readable;
var Stream = require('stream');
var port = 5683;
var server, client;
var payload = '</1>,</2>,</3>,</4>,</5>';
var ep = 'test';
var schema = lwm2m.Schema({
foo : { id: 5, type: 'String' },
bar : { id: 6, type: 'Integer' },
});
describe('Information Reporting', function() {
beforeEach(function (done) {
server = lwm2m.createServer({ type: 'udp4' });
client = coap.createServer({ type: 'udp4' });
server.on('error', done);
server.on('register', function(params, accept) {
accept();
});
server.listen(port, function() {
var req = coap.request({
host: 'localhost',
port: port,
method: 'POST',
pathname: '/rd',
query: 'ep=' + ep + '<=86400&lwm2m=1.0&b=U',
});
req.on('response', function(res) {
res.code.should.equal('2.01');
client.listen(res.outSocket.port, done);
});
req.end(payload);
});
});
afterEach(function(done) {
server.close(function() {
client.close(done);
});
});
describe('#observe()', function() {
it('should return an stream and pipe client messages', function(done) {
client.on('request', function (req, res) {
req.method.should.equal('GET');
req.headers['Observe'].should.equal(0);
res.should.be.an.instanceof(Writable);
var interval = setInterval(function() {
res.write('test');
}, 50);
res.setOption('Content-Format', 'text/plain');
res.code = '2.05';
setTimeout(function() {
res.end();
done();
}, 200);
res.on('finish', function(err) {
should.not.exist(err);
clearInterval(interval);
});
});
server.observe(ep, '/3/4/5')
.then(function(stream) {
stream.should.be.an.instanceof(Stream);
stream.on('data', function(chunk) {
chunk.should.be.equal('test');
});
stream.on('error', done);
})
.catch(done);
});
it('should return parsed objects when using a schema', function(done) {
client.on('request', function (req, res) {
req.method.should.equal('GET');
req.headers['Observe'].should.equal(0);
res.should.be.an.instanceof(Writable);
res.setOption('Content-Format', 'application/vnd.oma.lwm2m+json');
res.code = '2.05';
setTimeout(function() {
var rs = new Readable();
rs.push('{"e":[');
rs.push('{"n":"5","sv":"test"},');
rs.push('{"n":"6","v":42}');
rs.push(']}');
rs.push(null);
rs.pipe(res);
done();
}, 200);
res.on('finish', function(err) {
should.not.exist(err);
});
});
server.observe(ep, '/3/4', { schema: schema })
.then(function(stream) {
stream.should.be.an.instanceof(Stream);
stream.on('data', function(data) {
should.exist(data);
data.should.have.properties({ foo: 'test', bar: 42 });
});
stream.on('error', done);
})
.catch(done);
});
it('should emit an `end` event when closing the stream', function(done) {
client.on('request', function (req, res) {
req.method.should.equal('GET');
req.headers['Observe'].should.equal(0);
res.should.be.an.instanceof(Writable);
res.setOption('Content-Format', 'text/plain');
res.code = '2.05';
res.write('test');
});
server.observe(ep, '/3/4')
.then(function(stream) {
stream.should.be.an.instanceof(Stream);
stream.on('data', function(chunk) {
chunk.should.be.equal('test');
stream.close();
});
stream.on('end', function() {
done();
});
stream.on('error', done);
})
.catch(done);
});
});
describe('#cancel()', function() {
it('should stop receiving data', function() {
client.on('request', function (req, res) {
req.method.should.equal('GET');
req.headers['Observe'].should.equal(1);
res.end();
});
return server.cancel(ep, '/3/4')
.should.be.fulfilled();
});
});
});