From 44c16ef8895b5457a67b2986c5437e921a6862c7 Mon Sep 17 00:00:00 2001 From: Ali Farhadi Date: Mon, 27 Jun 2016 18:23:47 +0430 Subject: [PATCH] Adding more tests --- lib/smpp.js | 5 +++++ test/pdu.js | 38 ++++++++++++++++++++++++++++++++++---- test/smpp.js | 23 +++++++++++++++++++++-- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/lib/smpp.js b/lib/smpp.js index 7739b59..220ee4a 100644 --- a/lib/smpp.js +++ b/lib/smpp.js @@ -121,6 +121,11 @@ Session.prototype.close = function(callback) { var createShortcut = function(command) { return function(options, responseCallback, sendCallback) { + if (typeof options == 'function') { + sendCallback = responseCallback; + responseCallback = options; + options = {}; + } var pdu = new PDU(command, options); return this.send(pdu, responseCallback, sendCallback); }; diff --git a/test/pdu.js b/test/pdu.js index 43b9e96..def86dd 100644 --- a/test/pdu.js +++ b/test/pdu.js @@ -1,4 +1,5 @@ var assert = require('assert'), + stream = require('stream'), PDU = require('../lib/pdu').PDU; describe('PDU', function() { @@ -30,18 +31,27 @@ describe('PDU', function() { replace_if_present_flag: 0, data_coding: 1, sm_default_msg_id: 0, - short_message: { message: 'test' } + short_message: { message: 'test' } }; }); describe('#construct', function() { - it('it should construct a pdu from buffer', function() { + it('should construct a pdu from buffer', function() { var pdu = new PDU(buffer); assert.equal(buffer.length, pdu.command_length); assert.deepEqual(pdu, expected); }); - it('it should not fail with a malformed pdu', function() { + it('should extract tlv parameters if available', function() { + var b = Buffer.concat([buffer, Buffer('0424000474657374', 'hex')]); + b[3] = 67; + expected.command_length = 67; + expected.message_payload = { message: 'test' }; + var pdu = new PDU(b); + assert.deepEqual(pdu, expected); + }); + + it('should not fail with a malformed pdu', function() { var b = Buffer.concat([buffer, Buffer([0])]); b[3] = 0x3c; expected.command_length = 60; @@ -49,7 +59,7 @@ describe('PDU', function() { assert.deepEqual(pdu, expected); }); - it('it should throw error when PDU length is larger than PDU.maxLength', function() { + it('should throw error when PDU length is larger than PDU.maxLength', function() { buffer[0] = 1; assert.throws(function() { var pdu = new PDU(buffer); @@ -57,6 +67,16 @@ describe('PDU', function() { }); }); + describe('#fromStream()', function() { + it('should extract pdu from stream', function() { + var readable = new stream.Readable(); + readable._read = function() {} ; + readable.push(buffer); + var pdu = PDU.fromStream(readable); + assert.deepEqual(pdu, expected); + }); + }); + describe('#fromBuffer()', function() { it('should return false if buffer size is less than PDU length', function() { var b = buffer.slice(0, buffer.length - 1); @@ -85,4 +105,14 @@ describe('PDU', function() { assert.deepEqual(pdu.toBuffer(), buffer); }); }); + + describe('#response()', function() { + it('should generate a response pdu from the given pdu', function() { + var pdu = new PDU(buffer); + var response = pdu.response({ message_id: '123456' }); + assert.equal(response.command, 'submit_sm_resp'); + assert.equal(response.message_id, '123456'); + assert.equal(response.sequence_number, 2); + }); + }); }); diff --git a/test/smpp.js b/test/smpp.js index 76fc6c6..2cf2e3d 100644 --- a/test/smpp.js +++ b/test/smpp.js @@ -36,9 +36,14 @@ describe('Server', function() { describe('Session', function() { var server, port, secure = {}; + var sessionHandler = function(session) { + session.on('pdu', function(pdu) { + session.send(pdu.response()); + }); + }; before(function(done) { - server = smpp.createServer(); + server = smpp.createServer(sessionHandler); server.listen(0, done); port = server.address().port; }); @@ -47,7 +52,7 @@ describe('Session', function() { secure.server = smpp.createServer({ key: fs.readFileSync(__dirname + '/fixtures/server.key'), cert: fs.readFileSync(__dirname + '/fixtures/server.crt') - }); + }, sessionHandler); secure.server.listen(0, done); secure.port = secure.server.address().port; }); @@ -111,4 +116,18 @@ describe('Session', function() { }, done); }); }); + + describe('#send()', function() { + it('should successfully send a pdu and receive its response', function(done) { + var session = smpp.connect({ port: port }); + var pdu = new smpp.PDU('enquire_link'); + session.send(pdu, done.bind(this, null)); + }); + + it('should successfully send a pdu using shorthand methods', function(done) { + var session = smpp.connect({ port: port }); + var pdu = new smpp.PDU('enquire_link'); + session.enquire_link(done.bind(this, null)); + }); + }); });