Comparing version 0.0.1 to 0.0.2
var ami = { | ||
Client: require(__dirname + '/client') | ||
} | ||
var EventEmitter = require('events').EventEmitter; | ||
var util = require('util'); | ||
var Client = function(socket) { | ||
EventEmitter.call(this); | ||
this.socket = socket || new require('net').Socket(); | ||
var self = this; | ||
this.socket.on('error', function errorEmitProxy(err){ | ||
self.emit('error', err); | ||
}); | ||
} | ||
util.inherits(Client, EventEmitter); | ||
Client.prototype.connect = function(port, host, connectListener) { | ||
this.socket.connect(port, host, connectListener); | ||
} | ||
var actionid = 1; | ||
Client.prototype.login = function(username, secret, onLogin) { | ||
this.socket.write([ | ||
'ActionID: '+(actionid++), | ||
'Action: login', | ||
'Username: ' + username, | ||
'Secret: ' + secret, | ||
'', | ||
'' | ||
].join('\r\n')) | ||
} | ||
ami.Client = Client; | ||
module.exports = ami; | ||
return; |
@@ -5,3 +5,3 @@ { | ||
"description": "asterisk ami client", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"repository": { | ||
@@ -13,9 +13,11 @@ "type": "git", | ||
"scripts": { | ||
"test": "mocha -r should" | ||
"test": "mocha -r should -R tap -t 100" | ||
}, | ||
"engines": { | ||
"node": "~0.6.3" | ||
"node": "~0.6.0" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
"devDependencies": { | ||
"memory-socket" : "*" | ||
} | ||
} |
@@ -1,31 +0,13 @@ | ||
var ami = require(__dirname + '/../lib'); | ||
var EventEmitter = require('events').EventEmitter; | ||
var util = require('util'); | ||
var MockSocket = function() { | ||
EventEmitter.call(this); | ||
this.data = []; | ||
} | ||
util.inherits(MockSocket, EventEmitter); | ||
var MemorySocket = require('memory-socket') | ||
MockSocket.prototype.connect = function(port, host, connectListener) { | ||
this.port = port; | ||
if('function' === typeof host) { | ||
connectListener = host; | ||
} | ||
else if('function' === typeof connectListener) { | ||
this.on('connect', connectListener); | ||
} | ||
this.host = host; | ||
} | ||
var ami = require(__dirname + '/../lib'); | ||
var Action = require(__dirname + '/../lib/action'); | ||
MockSocket.prototype.write = function(data) { | ||
this.data.push(data); | ||
} | ||
describe('ami.Client', function() { | ||
describe('Client', function() { | ||
describe('connection', function() { | ||
describe('success', function() { | ||
var socket = new MockSocket(); | ||
var socket = new MemorySocket(); | ||
var client = new ami.Client(socket); | ||
@@ -36,6 +18,4 @@ var port = 5038; | ||
before(function(done) { | ||
socket.emitSoon('connect'); | ||
client.connect(port, host, done); | ||
process.nextTick(function() { | ||
socket.emit('connect'); | ||
}) | ||
}) | ||
@@ -47,7 +27,6 @@ | ||
}) | ||
}) | ||
describe('error', function() { | ||
var socket = new MockSocket(); | ||
var socket = new MemorySocket(); | ||
var client = new ami.Client(socket); | ||
@@ -62,26 +41,99 @@ var port = 5038; | ||
}); | ||
process.nextTick(function() { | ||
socket.emit('error', new Error("fake error")); | ||
}) | ||
socket.emitSoon('error', new Error("fake error")); | ||
}) | ||
}) | ||
}) | ||
describe('login', function() { | ||
describe('sent packet', function() { | ||
var socket = new MockSocket(); | ||
describe('success', function() { | ||
var socket = new MemorySocket(); | ||
var client = new ami.Client(socket); | ||
client.login('user', 'pass', function() { | ||
}); | ||
it('has correct information', function() { | ||
socket.data[0].should.equal([ | ||
'ActionID: 1', | ||
'Action: login', | ||
'Username: user', | ||
'Secret: pass','',''].join('\r\n')) | ||
var loginAction = { | ||
action: 'login', | ||
username: 'user', | ||
secret: 'pass' | ||
} | ||
beforeEach(function() { | ||
socket = new MemorySocket(); | ||
client = new ami.Client(socket); | ||
//enqueue a successful login message | ||
var packet = 'Response: Success\r\nActionID: ' + Action.lastActionID + '\r\nMessage: Authentication accepted\r\n\r\n' | ||
socket.emitSoon('data', Buffer(packet,'utf8')) | ||
}) | ||
it('sends correct login message', function() { | ||
client.send(loginAction); | ||
var expectedData = 'Action: login\r\nUsername: user\r\nSecret: pass\r\nActionID: ' + (Action.lastActionID-1) + '\r\n\r\n'; | ||
socket.data[0].should.equal(expectedData); | ||
}) | ||
it('calls callback with no error', function(done) { | ||
client.send(loginAction, done); | ||
}) | ||
it('emits a login success message', function(done) { | ||
client.send(loginAction); | ||
client.on('message', function(msg) { | ||
msg.actionID.should.equal((Action.lastActionID-1).toString()); | ||
msg.response.should.equal('Success'); | ||
msg.message.should.equal('Authentication accepted'); | ||
done(); | ||
}) | ||
}) | ||
it('removes completed action from action queue', function() { | ||
client._pendingActions.length.should.equal(0); | ||
client.send(loginAction, function(err) { | ||
process.nextTick(function() { | ||
client._pendingActions.length.should.equal(0); | ||
}) | ||
}) | ||
client._pendingActions.length.should.equal(1); | ||
}) | ||
}) | ||
}) | ||
}) | ||
describe('Client', function() { | ||
describe('incomming message parsing', function() { | ||
it('doesn\'t choke on startup greeting message', function(done) { | ||
var socket = new MemorySocket(); | ||
var client = new ami.Client(socket); | ||
socket.emitSoon('data', Buffer('Asterisk Call Manager/1.1\r\n', 'utf8')); | ||
client.on('message', function(msg) { | ||
msg.should.equal('asterisk Call Manager/1.1'); | ||
done(); | ||
}) | ||
}) | ||
}) | ||
describe('split packets', function(done) { | ||
return; | ||
var socket = new MemorySocket(); | ||
var client = new ami.Client(socket); | ||
client.socket.on('data', function() { | ||
console.log('msg: \n"%s"', arguments[0].toString('utf8')); | ||
}) | ||
before(function() { | ||
//socket.emitSoon('data', Buffer('Asterisk Call Man')) | ||
//socket.emitSoon('data', Buffer('ager/1.1\r\n')) | ||
var firstPartOfResponse = '' | ||
socket.emitSoon('data', Buffer('Response: Success\r\nActionID: ', 'utf8')) | ||
socket.emitSoon('data', Buffer(Action.lastActionID + '\r\nMessage: test\r\n\r\n')) | ||
}) | ||
it('parse correctly', function(done) { | ||
client.login('name', 'pw'); | ||
client.on('message', function(msg) { | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
12706
10
374
1
16
1
8
1