Skip to content

Commit

Permalink
Adding more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
farhadi committed Jun 27, 2016
1 parent 6d2dd0a commit 44c16ef
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
5 changes: 5 additions & 0 deletions lib/smpp.js
Expand Up @@ -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);
};
Expand Down
38 changes: 34 additions & 4 deletions test/pdu.js
@@ -1,4 +1,5 @@
var assert = require('assert'),
stream = require('stream'),
PDU = require('../lib/pdu').PDU;

describe('PDU', function() {
Expand Down Expand Up @@ -30,33 +31,52 @@ 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;
var pdu = new PDU(b);
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);
}, /PDU length was too large/);
});
});

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);
Expand Down Expand Up @@ -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);
});
});
});
23 changes: 21 additions & 2 deletions test/smpp.js
Expand Up @@ -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;
});
Expand All @@ -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;
});
Expand Down Expand Up @@ -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));
});
});
});

0 comments on commit 44c16ef

Please sign in to comment.