Comparing version 0.0.5 to 0.0.8
@@ -10,32 +10,32 @@ "use strict"; | ||
EventEmitter.call( this ); | ||
EventEmitter.call( this ); | ||
config = config || {}; | ||
config = config || {}; | ||
this._config = config; | ||
this._transports = []; | ||
this._config = config; | ||
this._transports = []; | ||
if ( !this._config.baseDir ) { | ||
throw new Error( 'config field baseDir is required' ); | ||
} | ||
if ( !this._config.baseDir ) { | ||
throw new Error( 'config field baseDir is required' ); | ||
} | ||
if ( !fs.existsSync( this._config.baseDir ) ) { | ||
throw new Error( 'config field baseDir identifies a missing directory: ' + this._config.baseDir ); | ||
} | ||
if ( !fs.existsSync( this._config.baseDir ) ) { | ||
throw new Error( 'config field baseDir identifies a missing directory: ' + this._config.baseDir ); | ||
} | ||
var baseDirStat = fs.statSync( this._config.baseDir ); | ||
if ( !baseDirStat.isDirectory() ) { | ||
throw new Error( 'config field baseDir must identify a directory: ' + this._config.baseDir ); | ||
} | ||
var baseDirStat = fs.statSync( this._config.baseDir ); | ||
if ( !baseDirStat.isDirectory() ) { | ||
throw new Error( 'config field baseDir must identify a directory: ' + this._config.baseDir ); | ||
} | ||
if ( this._config.hasOwnProperty( 'authorize' ) && typeof this._config.authorize !== 'function' ) { | ||
throw new Error( 'config field authorize must be a function if supplied' ); | ||
} | ||
if ( this._config.hasOwnProperty( 'authorize' ) && typeof this._config.authorize !== 'function' ) { | ||
throw new Error( 'config field authorize must be a function if supplied' ); | ||
} | ||
// normalize path and slashes | ||
this._config.baseDir = ('/' + fs.realpathSync( this._config.baseDir ) + '/').replace( /\/\/+/g, '/' ); | ||
// normalize path and slashes | ||
this._config.baseDir = ('/' + fs.realpathSync( this._config.baseDir ) + '/').replace( /\/\/+/g, '/' ); | ||
this._moduleCache = {}; | ||
this._moduleCache = {}; | ||
this._require = null; | ||
this._require = null; | ||
@@ -46,27 +46,27 @@ }; | ||
FlingReceiver.prototype.addTransport = function ( transport ) { | ||
var self = this; | ||
var existingEntry = self._selectTransportEntry( transport ); | ||
var self = this; | ||
var existingEntry = self._selectTransportEntry( transport ); | ||
// don't add twice | ||
if ( existingEntry === null ) { | ||
// don't add twice | ||
if ( existingEntry === null ) { | ||
var handler = self._generateRequestHandler(); | ||
var handler = self._generateRequestHandler(); | ||
try { | ||
try { | ||
transport.on( 'request', handler ); | ||
transport.on( 'request', handler ); | ||
self._transports.push( { | ||
transport: transport, | ||
handler: handler | ||
} ); | ||
self._transports.push( { | ||
transport: transport, | ||
handler: handler | ||
} ); | ||
} catch ( e ) { | ||
// NO-OP | ||
} | ||
} catch ( e ) { | ||
// NO-OP | ||
} | ||
self.emit( 'addTransport', transport ); | ||
self.emit( 'addTransport', transport ); | ||
} | ||
} | ||
@@ -77,19 +77,19 @@ }; | ||
for ( var i = 0; i < this._transports.length; i++ ) { | ||
if ( this._transports[i].transport === transport ) { | ||
return this._transports[i]; | ||
} | ||
} | ||
for ( var i = 0; i < this._transports.length; i++ ) { | ||
if ( this._transports[i].transport === transport ) { | ||
return this._transports[i]; | ||
} | ||
} | ||
return null; | ||
return null; | ||
}; | ||
FlingReceiver.prototype._generateRequestHandler = function () { | ||
var self = this; | ||
var self = this; | ||
// preserve context | ||
return function ( request ) { | ||
// preserve context | ||
return function ( request ) { | ||
self._handleRequest( request ); | ||
}; | ||
self._handleRequest( request ); | ||
}; | ||
@@ -100,124 +100,127 @@ }; | ||
var self = this; | ||
var self = this; | ||
var timer = new Timer(); | ||
var method = null; | ||
var timer = new Timer(); | ||
var method = null; | ||
var tasks = { | ||
validateRequestSchema: function ( done ) { | ||
var tasks = { | ||
validateRequestSchema: function ( done ) { | ||
self._validateRequestSchema( request, done ); | ||
self._validateRequestSchema( request, done ); | ||
}, | ||
authorizeRequest: function ( done ) { | ||
}, | ||
authorizeRequest: function ( done ) { | ||
self._authorize( request, function ( authorized ) { | ||
self._authorize( request, function ( authorized ) { | ||
if ( authorized ) { | ||
done( null, true ); | ||
} else { | ||
done( { | ||
code: 401, | ||
message: 'Not authorized for specified method', | ||
data: request.payload.method | ||
} ); | ||
} | ||
if ( authorized ) { | ||
done( null, true ); | ||
} else { | ||
done( { | ||
code: 401, | ||
message: 'Not authorized for specified method', | ||
data: request.payload.method | ||
} ); | ||
} | ||
} ); | ||
} ); | ||
}, | ||
parseRequestMethod: function ( done ) { | ||
}, | ||
parseRequestMethod: function ( done ) { | ||
method = { | ||
module: request.payload.method.replace( /\.[^\.]*$/, '' ).trim().replace( /^\./, '' ).replace( /\.$/, '' ), | ||
action: request.payload.method.replace( /^.*\./, '' ).trim() | ||
}; | ||
method = { | ||
module: request.payload.method.replace( /\.[^\.]*$/, '' ).trim().replace( /^\./, '' ).replace( /\.$/, '' ), | ||
action: request.payload.method.replace( /^.*\./, '' ).trim() | ||
}; | ||
done( null, true ); | ||
done( null, true ); | ||
}, | ||
getRequestModule: function ( done ) { | ||
}, | ||
getRequestModule: function ( done ) { | ||
self._getModule( method.module, function ( err, module ) { | ||
self._getModule( method.module, function ( err, module ) { | ||
if ( err ) { | ||
done( { | ||
code: 500, | ||
message: 'Module failed to load', | ||
data: (err + '') | ||
} ); | ||
} else if ( module ) { | ||
method.module = module; | ||
done( null, true ); | ||
} else { | ||
done( { | ||
code: 404, | ||
message: 'Module not found', | ||
data: null | ||
} ); | ||
} | ||
} ); | ||
if ( err ) { | ||
done( { | ||
code: 500, | ||
message: 'Module failed to load', | ||
data: (err + '') | ||
} ); | ||
} else if ( module ) { | ||
method.module = module; | ||
done( null, true ); | ||
} else { | ||
done( { | ||
code: 404, | ||
message: 'Module not found', | ||
data: null | ||
} ); | ||
} | ||
} ); | ||
}, | ||
executeRequest: function ( done ) { | ||
if ( typeof method.module[method.action] === 'function' ) { | ||
try { | ||
method.module[method.action]( request.payload.params, function ( code, data ) { | ||
setImmediate( function () { | ||
if ( code === 200 ) { | ||
done( null, data ); | ||
} else { | ||
}, | ||
executeRequest: function ( done ) { | ||
if ( typeof method.module[method.action] === 'function' ) { | ||
try { | ||
if ( typeof data === 'string' ) { | ||
data = { | ||
message: data, | ||
data: null | ||
}; | ||
} | ||
var params = self._config.authorize ? { agentId: request.agentId, params: request.payload.params } : request.payload.params; | ||
done( { | ||
code: code, | ||
message: data.message, | ||
data: data.data | ||
} ); | ||
method.module[method.action]( params, function ( code, data ) { | ||
setImmediate( function () { | ||
if ( code === 200 ) { | ||
done( null, data ); | ||
} else { | ||
} | ||
if ( typeof data === 'string' ) { | ||
data = { | ||
message: data, | ||
data: null | ||
}; | ||
} | ||
} ); | ||
} ); | ||
} catch ( e ) { | ||
done( { | ||
code: 500, | ||
message: 'Failed to execute method', | ||
data: (e + '') | ||
} ); | ||
} | ||
} else { | ||
done( { | ||
code: 404, | ||
message: 'Method action not found', | ||
data: null | ||
} ); | ||
} | ||
} | ||
}; | ||
done( { | ||
code: code, | ||
message: data.message, | ||
data: data.data | ||
} ); | ||
timer.start(); | ||
async.series( tasks, function ( err, result ) { | ||
} | ||
var duration = timer.stop(); | ||
} ); | ||
} ); | ||
} catch ( e ) { | ||
done( { | ||
code: 500, | ||
message: ('Failed to execute method: ' + e.message), | ||
data: e.stack | ||
} ); | ||
} | ||
} else { | ||
done( { | ||
code: 404, | ||
message: 'Method action not found', | ||
data: null | ||
} ); | ||
} | ||
} | ||
}; | ||
if ( !result || !result.executeRequest ) { | ||
result = { | ||
executeRequest: null | ||
}; | ||
} | ||
timer.start(); | ||
async.series( tasks, function ( err, result ) { | ||
request.response( createResponsePayload( | ||
request.payload.id, | ||
err, | ||
result.executeRequest | ||
) ); | ||
} ); | ||
var duration = timer.stop(); | ||
if ( !result || !result.executeRequest ) { | ||
result = { | ||
executeRequest: null | ||
}; | ||
} | ||
request.response( createResponsePayload( | ||
request.payload.id, | ||
err, | ||
result.executeRequest | ||
) ); | ||
} ); | ||
}; | ||
@@ -227,75 +230,75 @@ | ||
if ( !request || typeof request !== 'object' ) { | ||
done( { | ||
code: 500, | ||
message: 'Internal Error: transport failed to return a request object', | ||
data: (typeof request) | ||
} ); | ||
return; | ||
} | ||
if ( !request || typeof request !== 'object' ) { | ||
done( { | ||
code: 500, | ||
message: 'Internal Error: transport failed to return a request object', | ||
data: (typeof request) | ||
} ); | ||
return; | ||
} | ||
if ( !request.payload || typeof request.payload !== 'object' ) { | ||
done( { | ||
code: 500, | ||
message: 'Internal Error: transport failed to return a request.payload object', | ||
data: (typeof request) | ||
} ); | ||
return; | ||
} | ||
if ( !request.payload || typeof request.payload !== 'object' ) { | ||
done( { | ||
code: 500, | ||
message: 'Internal Error: transport failed to return a request.payload object', | ||
data: (typeof request) | ||
} ); | ||
return; | ||
} | ||
if ( !request.payload.hasOwnProperty( 'jsonrpc' ) ) { | ||
done( { | ||
code: 400, | ||
message: 'Required Field: jsonrpc', | ||
data: (typeof request) | ||
} ); | ||
return; | ||
} | ||
if ( !request.payload.hasOwnProperty( 'jsonrpc' ) ) { | ||
done( { | ||
code: 400, | ||
message: 'Required Field: jsonrpc', | ||
data: (typeof request) | ||
} ); | ||
return; | ||
} | ||
if ( request.payload.jsonrpc !== '2.0' ) { | ||
done( { | ||
code: 400, | ||
message: 'Unsupported Protocol Version: JSON RPC ' + request.payload.jsonrpc, | ||
data: (typeof request) | ||
} ); | ||
return; | ||
} | ||
if ( request.payload.jsonrpc !== '2.0' ) { | ||
done( { | ||
code: 400, | ||
message: 'Unsupported Protocol Version: JSON RPC ' + request.payload.jsonrpc, | ||
data: (typeof request) | ||
} ); | ||
return; | ||
} | ||
if ( !request.payload.hasOwnProperty( 'id' ) ) { | ||
done( { | ||
code: 400, | ||
message: 'Required Field: id', | ||
data: (typeof request) | ||
} ); | ||
return; | ||
} | ||
if ( !request.payload.hasOwnProperty( 'id' ) ) { | ||
done( { | ||
code: 400, | ||
message: 'Required Field: id', | ||
data: (typeof request) | ||
} ); | ||
return; | ||
} | ||
if ( !request.payload.hasOwnProperty( 'method' ) ) { | ||
done( { | ||
code: 400, | ||
message: 'Required Field: method', | ||
data: (typeof request) | ||
} ); | ||
return; | ||
} | ||
if ( !request.payload.hasOwnProperty( 'method' ) ) { | ||
done( { | ||
code: 400, | ||
message: 'Required Field: method', | ||
data: (typeof request) | ||
} ); | ||
return; | ||
} | ||
if ( !request.payload.method || request.payload.method.length < 1 ) { | ||
done( { | ||
code: 400, | ||
message: 'Required Field: method can not be an empty string', | ||
data: (typeof request) | ||
} ); | ||
return; | ||
} | ||
if ( !request.payload.method || request.payload.method.length < 1 ) { | ||
done( { | ||
code: 400, | ||
message: 'Required Field: method can not be an empty string', | ||
data: (typeof request) | ||
} ); | ||
return; | ||
} | ||
if ( !request.payload.hasOwnProperty( 'params' ) ) { | ||
done( { | ||
code: 400, | ||
message: 'Required Field: params must be supplied', | ||
data: (typeof request) | ||
} ); | ||
return; | ||
} | ||
if ( !request.payload.hasOwnProperty( 'params' ) ) { | ||
done( { | ||
code: 400, | ||
message: 'Required Field: params must be supplied', | ||
data: (typeof request) | ||
} ); | ||
return; | ||
} | ||
done( null, true ); | ||
done( null, true ); | ||
@@ -306,60 +309,60 @@ }; | ||
var self = this; | ||
var self = this; | ||
var _require = self._require ? self._require : require; | ||
var modulePath = self._config.baseDir + path.replace( /\./g, '/' ); | ||
var _require = self._require ? self._require : require; | ||
var modulePath = self._config.baseDir + path.replace( /\./g, '/' ); | ||
var _done = function () { | ||
if ( self._moduleCache[modulePath].error ) { | ||
done( self._moduleCache[modulePath].error ); | ||
} else { | ||
done( null, self._moduleCache[modulePath].module ); | ||
} | ||
}; | ||
var _done = function () { | ||
if ( self._moduleCache[modulePath].error ) { | ||
done( self._moduleCache[modulePath].error ); | ||
} else { | ||
done( null, self._moduleCache[modulePath].module ); | ||
} | ||
}; | ||
if ( self._moduleCache.hasOwnProperty( modulePath ) ) { | ||
if ( self._moduleCache.hasOwnProperty( modulePath ) ) { | ||
_done(); | ||
_done(); | ||
} else { | ||
} else { | ||
var moduleExists = function () { | ||
try { | ||
var moduleExists = function () { | ||
try { | ||
self._moduleCache[modulePath] = { | ||
module: _require( modulePath ) | ||
}; | ||
self._moduleCache[modulePath] = { | ||
module: _require( modulePath ) | ||
}; | ||
} catch ( e ) { | ||
} catch ( e ) { | ||
self._moduleCache[modulePath] = { | ||
error: e | ||
}; | ||
self._moduleCache[modulePath] = { | ||
error: e | ||
}; | ||
} | ||
}; | ||
} | ||
}; | ||
fs.exists( modulePath, function ( exists ) { | ||
if ( exists ) { | ||
moduleExists(); | ||
_done(); | ||
fs.exists( modulePath, function ( exists ) { | ||
if ( exists ) { | ||
moduleExists(); | ||
_done(); | ||
} else { | ||
fs.exists( modulePath + '.js', function ( exists ) { | ||
} else { | ||
fs.exists( modulePath + '.js', function ( exists ) { | ||
if ( exists ) { | ||
moduleExists(); | ||
} else { | ||
self._moduleCache[modulePath] = { | ||
module: null | ||
}; | ||
} | ||
if ( exists ) { | ||
moduleExists(); | ||
} else { | ||
self._moduleCache[modulePath] = { | ||
module: null | ||
}; | ||
} | ||
_done(); | ||
_done(); | ||
} ); | ||
} | ||
} ); | ||
} ); | ||
} | ||
} ); | ||
} | ||
} | ||
@@ -369,13 +372,13 @@ }; | ||
FlingReceiver.prototype._authorize = function ( payload, done ) { | ||
if ( this._config.authorize ) { | ||
try { | ||
this._config.authorize( payload.agentId, payload.method, function ( authorized ) { | ||
setImmediate( done, authorized ); | ||
} ); | ||
} catch ( e ) { | ||
done( false ); | ||
} | ||
} else { | ||
done( true ); | ||
} | ||
if ( this._config.authorize ) { | ||
try { | ||
this._config.authorize( payload.agentId, payload.method, function ( authorized ) { | ||
setImmediate( done, authorized ); | ||
} ); | ||
} catch ( e ) { | ||
done( false ); | ||
} | ||
} else { | ||
done( true ); | ||
} | ||
}; | ||
@@ -385,7 +388,7 @@ | ||
this.emit( 'log', { | ||
level: level, | ||
message: message, | ||
data: data | ||
} ); | ||
this.emit( 'log', { | ||
level: level, | ||
message: message, | ||
data: data | ||
} ); | ||
@@ -396,14 +399,14 @@ }; | ||
var payload = { | ||
jsonrpc: '2.0', | ||
id: (id || 0) | ||
}; | ||
var payload = { | ||
jsonrpc: '2.0', | ||
id: (id || 0) | ||
}; | ||
if ( error ) { | ||
payload.error = error; | ||
} else { | ||
payload.result = data; | ||
} | ||
if ( error ) { | ||
payload.error = error; | ||
} else { | ||
payload.result = data; | ||
} | ||
return payload; | ||
return payload; | ||
@@ -410,0 +413,0 @@ } |
@@ -6,8 +6,8 @@ "use strict"; | ||
var AbstractReceiverTransport = function ( config ) { | ||
EventEmitter.call( this ); | ||
this._config = config; | ||
EventEmitter.call( this ); | ||
this._config = config; | ||
if ( this._config.hasOwnProperty( 'authenticate' ) && typeof this._config.authenticate !== 'function' ) { | ||
throw new Error( 'config field authenticate must be a function if supplied' ); | ||
} | ||
if ( this._config.hasOwnProperty( 'authenticate' ) && typeof this._config.authenticate !== 'function' ) { | ||
throw new Error( 'config field authenticate must be a function if supplied' ); | ||
} | ||
@@ -18,7 +18,7 @@ }; | ||
AbstractReceiverTransport.prototype.init = function ( done ) { | ||
setImmediate( done, 400, 'NOT IMPLEMENTED' ); | ||
setImmediate( done, 400, 'NOT IMPLEMENTED' ); | ||
}; | ||
AbstractReceiverTransport.prototype.dinit = function ( done ) { | ||
setImmediate( done, 400, 'NOT IMPLEMENTED' ); | ||
setImmediate( done, 400, 'NOT IMPLEMENTED' ); | ||
}; | ||
@@ -28,21 +28,21 @@ | ||
if ( this._config._authenticate ) { | ||
if ( typeof this._config.authenticate === 'function' ) { | ||
try { | ||
try { | ||
this._config._authenticate( payload, function ( id ) { | ||
setImmediate( done, id ); | ||
} ); | ||
this._config.authenticate( payload, function ( id ) { | ||
setImmediate( done, id ); | ||
} ); | ||
} catch ( e ) { | ||
} catch ( e ) { | ||
setImmediate( done, null ); | ||
setImmediate( done, null ); | ||
} | ||
} | ||
} else { | ||
} else { | ||
setImmediate( done, null ); | ||
setImmediate( done, null ); | ||
} | ||
} | ||
@@ -49,0 +49,0 @@ }; |
@@ -7,6 +7,6 @@ "use strict"; | ||
var ExpressReceiverTransport = function ( config ) { | ||
AbstractReceiverTransport.apply( this, arguments ); | ||
AbstractReceiverTransport.apply( this, arguments ); | ||
this._server = this._config.express; | ||
this._url = this._config.url; | ||
this._server = this._config.express; | ||
this._url = this._config.url; | ||
@@ -18,20 +18,18 @@ }; | ||
var self = this; | ||
self._server.post( self._url, express.json(), function ( request, response ) { | ||
var self = this; | ||
self._server.post( self._url, express.json(), function ( request, response ) { | ||
self._authenticate( { | ||
headers: request.headers, | ||
body: request.body | ||
}, | ||
function ( id ) { | ||
self.emit( 'request', { | ||
agentId: id, | ||
payload: request.body, | ||
response: generateCallback( response ) | ||
} ); | ||
} ); | ||
self._authenticate( | ||
request, | ||
function ( agentId ) { | ||
self.emit( 'request', { | ||
agentId: agentId, | ||
payload: request.body, | ||
response: generateCallback( response ) | ||
} ); | ||
} ); | ||
} ); | ||
} ); | ||
setImmediate( done, null ); | ||
setImmediate( done, null ); | ||
@@ -42,12 +40,12 @@ }; | ||
var routes = this._server.routes; | ||
var routes = this._server.routes; | ||
for ( var i = 0; i < routes.length; i++ ) { | ||
if ( routes[i].path === this._url ) { | ||
delete routes[i]; | ||
break; | ||
} | ||
} | ||
for ( var i = 0; i < routes.length; i++ ) { | ||
if ( routes[i].path === this._url ) { | ||
delete routes[i]; | ||
break; | ||
} | ||
} | ||
setImmediate( done, null ); | ||
setImmediate( done, null ); | ||
@@ -58,30 +56,30 @@ }; | ||
var notified = false; | ||
var _notify = function () { | ||
if ( notified ) { | ||
return; | ||
} | ||
notified = true; | ||
var notified = false; | ||
var _notify = function () { | ||
if ( notified ) { | ||
return; | ||
} | ||
notified = true; | ||
setImmediate( notify ); | ||
}; | ||
setImmediate( notify ); | ||
}; | ||
return function ( responseObject ) { | ||
return function ( responseObject ) { | ||
_notify(); | ||
_notify(); | ||
var payload = JSON.stringify( responseObject ); | ||
var payload = JSON.stringify( responseObject ); | ||
// Note: HTTP status is always 200 even if the RPC status is not. This | ||
// is because the transport layer status and RPC layer status codes have | ||
// nothing to do with each other. | ||
httpResponse.writeHead( 200, { | ||
'Content-Length': payload.length, | ||
'Content-Type': 'application/json' } ); | ||
// Note: HTTP status is always 200 even if the RPC status is not. This | ||
// is because the transport layer status and RPC layer status codes have | ||
// nothing to do with each other. | ||
httpResponse.writeHead( 200, { | ||
'Content-Length': payload.length, | ||
'Content-Type': 'application/json' } ); | ||
httpResponse.end( payload ); | ||
httpResponse.end( payload ); | ||
}; | ||
}; | ||
} | ||
module.exports = ExpressReceiverTransport; |
{ | ||
"name": "fling", | ||
"description": "JSON Fling is a simple JSON-RPC framework for NodeJS with built-in permissions and support for different transports.", | ||
"version": "0.0.5", | ||
"version": "0.0.8", | ||
"author": "Anthony Hildoer <anthony@bluerival.com>", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -6,3 +6,3 @@ "use strict"; | ||
describe( 'transports.receiver', function () { | ||
generateIntegrationTests( 'transport.receiver.express' ); | ||
generateIntegrationTests( 'transport.receiver.express' ); | ||
} ); | ||
@@ -23,123 +23,211 @@ | ||
describe( name, function () { | ||
describe( name, function () { | ||
var params = require( './' + name ); | ||
var params = require( './' + name ); | ||
var Transport = params.constructor; | ||
var config = params.config; | ||
var requestEmitter = params.requestEmitter; | ||
var Transport = params.constructor; | ||
var config = params.config; | ||
var requestEmitter = params.requestEmitter; | ||
var transport = null; | ||
var transport = null; | ||
before( function ( done ) { | ||
if ( typeof params.init === 'function' ) { | ||
params.init( done ); | ||
} else { | ||
done(); | ||
} | ||
} ); | ||
before( function ( done ) { | ||
if ( typeof params.init === 'function' ) { | ||
params.init( done ); | ||
} else { | ||
done(); | ||
} | ||
} ); | ||
after( function ( done ) { | ||
if ( typeof params.dinit === 'function' ) { | ||
params.dinit( done ); | ||
} else { | ||
done(); | ||
} | ||
} ); | ||
after( function ( done ) { | ||
if ( typeof params.dinit === 'function' ) { | ||
params.dinit( done ); | ||
} else { | ||
done(); | ||
} | ||
} ); | ||
it( 'should take an object for a config', function () { | ||
assert.strictEqual( typeof config, 'object' ); | ||
} ); | ||
it( 'should take an object for a config', function () { | ||
assert.strictEqual( typeof config, 'object' ); | ||
} ); | ||
it( 'should have a requestEmitter function to simulate input on the transport', function () { | ||
assert.strictEqual( typeof requestEmitter, 'function' ); | ||
} ); | ||
it( 'should have a requestEmitter function to simulate input on the transport', function () { | ||
assert.strictEqual( typeof requestEmitter, 'function' ); | ||
} ); | ||
it( 'should be a constructor', function () { | ||
assert.strictEqual( typeof Transport, 'function' ); | ||
} ); | ||
it( 'should be a constructor', function () { | ||
assert.strictEqual( typeof Transport, 'function' ); | ||
} ); | ||
it( 'should instantiate', function () { | ||
transport = new Transport( config ); | ||
} ); | ||
it( 'should instantiate w/ defaults', function () { | ||
transport = new Transport( config ); | ||
} ); | ||
it( 'should init', function ( done ) { | ||
transport.init( function ( err ) { | ||
try { | ||
assert.ifError( err ); | ||
done(); | ||
} catch ( e ) { | ||
done( e ); | ||
} | ||
} ); | ||
} ); | ||
it( 'should init w/ defaults', function ( done ) { | ||
transport.init( function ( err ) { | ||
try { | ||
assert.ifError( err ); | ||
done(); | ||
} catch ( e ) { | ||
done( e ); | ||
} | ||
} ); | ||
} ); | ||
it( 'should dinit', function ( done ) { | ||
transport.dinit( function ( err ) { | ||
try { | ||
assert.ifError( err ); | ||
done(); | ||
} catch ( e ) { | ||
done( e ); | ||
} | ||
} ); | ||
} ); | ||
it( 'should emit a properly formatted request w/ defaults', function ( done ) { | ||
it( 'should emit a properly formatted request', function ( done ) { | ||
try { | ||
try { | ||
// simulate adding the transport instance to a FlingerReceiver instance | ||
transport.on( 'request', fakeReceiver( done ) ); | ||
// simulate adding the transport instance to a FlingerReceiver instance | ||
transport.on( 'request', fakeReceiver( done ) ); | ||
// simulate a request payload delivered to the transport, and the response | ||
requestEmitter( { | ||
jsonrpc: '2.0', | ||
id: 100, | ||
method: 'test.module1.module2.action', | ||
params: { | ||
some: 'string', | ||
an: [ 'array' ], | ||
anInteger: 1, | ||
another: { | ||
object: 'here' | ||
} | ||
} | ||
}, | ||
function ( response ) { | ||
// simulate a request payload delivered to the transport, and the response | ||
requestEmitter( { | ||
jsonrpc: '2.0', | ||
id: 100, | ||
method: 'test.module1.module2.action', | ||
params: { | ||
some: 'string', | ||
an: [ 'array' ], | ||
anInteger: 1, | ||
another: { | ||
object: 'here' | ||
} | ||
} | ||
}, | ||
function ( response ) { | ||
try { | ||
try { | ||
assert.strictEqual( typeof response, 'object' ); | ||
assert.strictEqual( response.id, 100 ); | ||
assert.strictEqual( response.method, undefined ); | ||
assert.strictEqual( response.error, undefined ); | ||
assert.deepEqual( objToString( response.result ), objToString( { | ||
some: 'string', | ||
an: [ 'array' ], | ||
anInteger: 1, | ||
another: { | ||
object: 'here' | ||
}, | ||
injectedData: { | ||
you: ['should'], | ||
see: { | ||
this: 'data ' | ||
} | ||
} | ||
} ) ); | ||
assert.strictEqual( typeof response, 'object' ); | ||
assert.strictEqual( response.id, 100 ); | ||
assert.strictEqual( response.method, undefined ); | ||
assert.strictEqual( response.error, undefined ); | ||
assert.deepEqual( objToString( response.result ), objToString( { | ||
some: 'string', | ||
an: [ 'array' ], | ||
anInteger: 1, | ||
another: { | ||
object: 'here' | ||
}, | ||
injectedData: { | ||
you: ['should'], | ||
see: { | ||
this: 'data ' | ||
} | ||
} | ||
} ) ); | ||
done(); | ||
} catch ( e ) { | ||
done( e ); | ||
} | ||
done(); | ||
} catch ( e ) { | ||
done( e ); | ||
} | ||
} ); | ||
} ); | ||
} catch ( e ) { | ||
done( e ); | ||
} | ||
} ); | ||
} catch ( e ) { | ||
done( e ); | ||
} | ||
} ); | ||
it( 'should dinit w/ defaults', function ( done ) { | ||
transport.dinit( function ( err ) { | ||
try { | ||
assert.ifError( err ); | ||
done(); | ||
} catch ( e ) { | ||
done( e ); | ||
} | ||
} ); | ||
} ); | ||
} ); | ||
var authenticationCount = 0; | ||
it( 'should instantiate w/ authentication', function () { | ||
config.authenticate = function ( payload, done ) { | ||
done( '100' ); | ||
authenticationCount++; | ||
}; | ||
transport = new Transport( config ); | ||
} ); | ||
it( 'should init w/ authentication', function ( done ) { | ||
transport.init( function ( err ) { | ||
try { | ||
assert.ifError( err ); | ||
done(); | ||
} catch ( e ) { | ||
done( e ); | ||
} | ||
} ); | ||
} ); | ||
it( 'should emit a properly formatted request w/ authentication', function ( done ) { | ||
try { | ||
// simulate adding the transport instance to a FlingerReceiver instance | ||
transport.on( 'request', fakeReceiver( done ) ); | ||
// simulate a request payload delivered to the transport, and the response | ||
requestEmitter( { | ||
jsonrpc: '2.0', | ||
id: 100, | ||
method: 'test.module1.module2.action', | ||
params: { | ||
some: 'string', | ||
an: [ 'array' ], | ||
anInteger: 1, | ||
another: { | ||
object: 'here' | ||
} | ||
} | ||
}, | ||
function ( response ) { | ||
try { | ||
assert.strictEqual( authenticationCount, 1 ); | ||
assert.strictEqual( typeof response, 'object' ); | ||
assert.strictEqual( response.id, 100 ); | ||
assert.strictEqual( response.method, undefined ); | ||
assert.strictEqual( response.error, undefined ); | ||
assert.deepEqual( objToString( response.result ), objToString( { | ||
some: 'string', | ||
an: [ 'array' ], | ||
anInteger: 1, | ||
another: { | ||
object: 'here' | ||
}, | ||
injectedData: { | ||
you: ['should'], | ||
see: { | ||
this: 'data ' | ||
} | ||
} | ||
} ) ); | ||
done(); | ||
} catch ( e ) { | ||
done( e ); | ||
} | ||
} ); | ||
} catch ( e ) { | ||
done( e ); | ||
} | ||
} ); | ||
it( 'should dinit w/ authentication', function ( done ) { | ||
transport.dinit( function ( err ) { | ||
try { | ||
assert.ifError( err ); | ||
done(); | ||
} catch ( e ) { | ||
done( e ); | ||
} | ||
} ); | ||
} ); | ||
} ); | ||
} | ||
@@ -149,33 +237,33 @@ | ||
return function ( request ) { | ||
try { | ||
return function ( request ) { | ||
try { | ||
assert.strictEqual( typeof request, 'object' ); | ||
assert.strictEqual( typeof request, 'object' ); | ||
assert.strictEqual( typeof request.response, 'function' ); | ||
assert.strictEqual( typeof request.response, 'function' ); | ||
assert.strictEqual( typeof request.payload, 'object' ); | ||
assert.strictEqual( typeof request.payload.method, 'string' ); | ||
assert.ok( request.payload.hasOwnProperty( 'id' ) ); | ||
assert.ok( request.payload.hasOwnProperty( 'params' ) ); | ||
assert.strictEqual( typeof request.payload, 'object' ); | ||
assert.strictEqual( typeof request.payload.method, 'string' ); | ||
assert.ok( request.payload.hasOwnProperty( 'id' ) ); | ||
assert.ok( request.payload.hasOwnProperty( 'params' ) ); | ||
request.payload.params.injectedData = { | ||
you: ['should'], | ||
see: { | ||
this: 'data ' | ||
} | ||
}; | ||
request.payload.params.injectedData = { | ||
you: ['should'], | ||
see: { | ||
this: 'data ' | ||
} | ||
}; | ||
setImmediate( function () { | ||
request.response( { | ||
jsonrpc: '2.0', | ||
id: request.payload.id, | ||
result: request.payload.params | ||
} ); | ||
} ); | ||
setImmediate( function () { | ||
request.response( { | ||
jsonrpc: '2.0', | ||
id: request.payload.id, | ||
result: request.payload.params | ||
} ); | ||
} ); | ||
} catch ( e ) { | ||
done( e ); | ||
} | ||
}; | ||
} catch ( e ) { | ||
done( e ); | ||
} | ||
}; | ||
@@ -185,3 +273,3 @@ } | ||
function objToString( obj ) { | ||
return JSON.stringify( obj, null, 2 ); | ||
return JSON.stringify( obj, null, 2 ); | ||
} |
Sorry, the diff of this file is not supported yet
1151
37245
20