Skip to content

Commit

Permalink
Merge branch 'master' of github.com:keithnlarsen/nodification
Browse files Browse the repository at this point in the history
  • Loading branch information
Michelle Schuster committed Dec 14, 2011
2 parents ac296c8 + 46d94eb commit 834894e
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 84 deletions.
1 change: 0 additions & 1 deletion .travis.yml
@@ -1,4 +1,3 @@
language: node_js
node_js:
- 0.4
- 0.6
58 changes: 58 additions & 0 deletions gateways/notificationRegistrationGateway.js
@@ -0,0 +1,58 @@
// This is an implementation of the Revealing Module Pattern
// ref: http://addyosmani.com/resources/essentialjsdesignpatterns/book/#revealingmodulepatternjavascript
//
// ---- Example of how to call ----
// ---- You only need to call init if you want to overide the default options ----
// var options = { client: someClient, requestHandler: someHandler, eventNotificationUrl: 'someUrl' };
// gateway.init( options );
// gateway.register( registrationData, voiceMailType, function ( err, success) { ... });
//
module.exports = (function() {
var client;
var requestHandler = require( '../libs/requestHandler' );
var eventNotificationUrl = 'http://localhost:3000/events';

function init ( options ) {
client = options.client || client;
requestHandler = options.requestHandler || requestHandler;
eventNotificationUrl = options.eventNotificationUrl || eventNotificationUrl;
}

function createRequest ( registration, notificationType ) {
var url = require( 'url' ).parse( notificationType.registrationUrl );
var http = (url.protocol == 'http') ? require( 'http' ) : require( 'https' );

client = client || http.createClient( url.port, url.hostname );

var requestOptions = {
'Host': url.hostname,
'Accept': 'application/json',
'Content-Type': 'application/json'
};

var message = {
'registrationKey': registration.key,
'eventNotificationUrl': eventNotificationUrl,
'notificationType': notificationType.name
};

var request = client.request( 'GET', url.path, requestOptions );
request.write( JSON.stringify( message ) );
return request;
}

function register ( registration, notificationType, callBack ) {
requestHandler.handle( createRequest( registration, notificationType ), function( err, response ) {
if ( response.statusCode >= 200 && response.statusCode < 300 ) {
callBack( null, true );
} else {
callBack( new Error( 'Error registering notification at:' + notificationType.registrationUrl + '. StatusCode ' + response.statusCode + ': ' + response.body ), false );
}
} );
}

return {
init: init,
register: register
};
}());
12 changes: 11 additions & 1 deletion libs/baseRestController.js
Expand Up @@ -72,7 +72,17 @@ var baseRestController = baseObject.extend( {
next( new Error( 'Internal Server Error: see logs for details: ' + err ), req, res );
}
} else {
res.send( instance.toObject(), 201 );
req.params = req.params || {};
req.params.id = instance._id;
self.getQuery( req ).exec( function( err, instance ) {
if ( err ) {
next( new Error( 'Internal Server Error: see logs for details: ' + err ), req, res );
} else if ( !instance ) {
next( self.restErrors.notFound.create( self.name + ' Id: "' + req.params.id + '" was not found.' ), req, res );
} else {
res.send( instance.toObject(), 201 );
}
} );
}
} );
},
Expand Down
25 changes: 25 additions & 0 deletions libs/requestHandler.js
@@ -0,0 +1,25 @@
module.exports = ( function() {

function handle( request, callBack ) {
request.on( 'response', function ( response ) {
var responseBody = "";
response.setEncoding( 'utf8' );

response.addListener( "data", function( chunk ) {
responseBody += chunk;
} );

response.on( 'end', function() {
response.body = responseBody;
callBack( null, response );
} );
} );

request.end();
}

return {
handle : handle
}
}());

6 changes: 3 additions & 3 deletions libs/resterrors.js
Expand Up @@ -45,7 +45,7 @@ module.exports = ( function() {
title: 'Unsupported Media Type',
description: 'The server is refusing to service the request beacuse the entity of the request is in a format not supported by the requested resource.',
httpStatus: 415
})
} )
};

var errorMapper = {
Expand All @@ -67,12 +67,12 @@ module.exports = ( function() {
}
};

var errorHandler = function( error, request, response ) {
function errorHandler ( error, request, response ) {
var constructorName = error.prototype ? error.prototype.constructor.name : 'default';
var errorHandler = errorMapper[error.name] || errorMapper[constructorName];

errorHandler( error, request, response );
};
}

return {
baseRestError: baseRestError,
Expand Down
36 changes: 10 additions & 26 deletions tests/integration/controllers/notificationTypeControllerTest.js
Expand Up @@ -9,23 +9,7 @@ describe( 'Nodification.Tests.Integration.Controllers.NotificationTypeController
var app;

var localhost = http.createClient( 3000, 'localhost' );
var requestHelper = function( request, fn ) {
request.end();

request.on( 'response', function ( response ) {
var responseBody = "";
response.setEncoding( 'utf8' );

response.addListener( "data", function( chunk ) {
responseBody += chunk;
} );

response.on( 'end', function() {
response.body = responseBody;
fn( response );
} );
} );
};
var requestHandler = require('../../../libs/requestHandler');

before( function( done ) {
var app = require( '../../../app' );
Expand Down Expand Up @@ -59,7 +43,7 @@ describe( 'Nodification.Tests.Integration.Controllers.NotificationTypeController
var request = localhost.request( 'PUT', '/notificationTypes', {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'} );
request.write( JSON.stringify( createJSON ) );

requestHelper( request, function( response ) {
requestHandler.handle( request, function( err, response ) {
var actual = JSON.parse( response.body );
var expected = createJSON;

Expand All @@ -77,7 +61,7 @@ describe( 'Nodification.Tests.Integration.Controllers.NotificationTypeController
var request = localhost.request( 'PUT', '/notificationTypes', {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'} );
request.write( JSON.stringify( createJSON ) );

requestHelper( request, function( response ) {
requestHandler.handle( request, function( err, response ) {
response.statusCode.should.equal( 409 );
response.body.should.match( /duplicate key error/ );

Expand All @@ -90,7 +74,7 @@ describe( 'Nodification.Tests.Integration.Controllers.NotificationTypeController
it( 'should return a single NotificationType when given an existing Id', function( done ) {
var request = localhost.request( 'GET', '/notificationTypes/' + newId, {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'} );

requestHelper( request, function( response ) {
requestHandler.handle( request, function( err, response ) {
var actual = JSON.parse( response.body );
var expected = createJSON;

Expand All @@ -106,7 +90,7 @@ describe( 'Nodification.Tests.Integration.Controllers.NotificationTypeController
it( 'should return 404 NotFound when given a non-existing Id', function( done ) {
var request = localhost.request( 'GET', '/notificationTypes/111111111111111111111111', {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'} );

requestHelper( request, function( response ) {
requestHandler.handle( request, function( err, response ) {
response.statusCode.should.equal( 404 );

done();
Expand All @@ -119,7 +103,7 @@ describe( 'Nodification.Tests.Integration.Controllers.NotificationTypeController
var request = localhost.request( 'POST', '/notificationTypes/' + newId, {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'} );
request.write( JSON.stringify( updateJSON ) );

requestHelper( request, function( response ) {
requestHandler.handle( request, function( err, response ) {
var actual = JSON.parse( response.body );
var expected = updateJSON;

Expand All @@ -137,7 +121,7 @@ describe( 'Nodification.Tests.Integration.Controllers.NotificationTypeController
var request = localhost.request( 'POST', '/notificationTypes/111111111111111111111111', {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'} );
request.write( JSON.stringify( updateJSON ) );

requestHelper( request, function( response ) {
requestHandler.handle( request, function( err, response ) {
response.statusCode.should.equal( 404 );

done();
Expand All @@ -149,7 +133,7 @@ describe( 'Nodification.Tests.Integration.Controllers.NotificationTypeController
it( 'should return all NotificationTypes', function( done ) {
var request = localhost.request( 'GET', '/notificationTypes', {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'} );

requestHelper( request, function( response ) {
requestHandler.handle( request, function( err, response ) {
var actual = JSON.parse( response.body );
var expected = updateJSON;

Expand All @@ -169,7 +153,7 @@ describe( 'Nodification.Tests.Integration.Controllers.NotificationTypeController
var request = localhost.request( 'DELETE', '/notificationTypes/' + newId, {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'} );
request.write('{}');

requestHelper( request, function( response ) {
requestHandler.handle( request, function( err, response ) {
response.statusCode.should.equal( 200 );

done();
Expand All @@ -178,7 +162,7 @@ describe( 'Nodification.Tests.Integration.Controllers.NotificationTypeController
it( 'should return 404 when called with an Id that doesn\'t exist', function( done ) {
var request = localhost.request( 'DELETE', '/notificationTypes/' + newId, {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'} );
request.write('{}');
requestHelper( request, function( response ) {
requestHandler.handle( request, function( err, response ) {
response.statusCode.should.equal( 404 );

done();
Expand Down
34 changes: 9 additions & 25 deletions tests/integration/controllers/registrationControllerTest.js
Expand Up @@ -13,23 +13,7 @@ describe( 'Nodification.Tests.Integration.Controllers.RegistrationController', f
var app;

var localhost = http.createClient( 3000, 'localhost' );
var requestHelper = function( request, fn ) {
request.end();

request.on( 'response', function ( response ) {
var responseBody = "";
response.setEncoding( 'utf8' );

response.addListener( "data", function( chunk ) {
responseBody += chunk;
} );

response.on( 'end', function() {
response.body = responseBody;
fn( response );
} );
} );
};
var requestHandler = require('../../../libs/requestHandler');

before( function( done ) {
var app = require( '../../../app' );
Expand Down Expand Up @@ -75,12 +59,12 @@ describe( 'Nodification.Tests.Integration.Controllers.RegistrationController', f
var request = localhost.request( 'PUT', '/registrations', {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'} );
request.write( JSON.stringify( createJSON ) );

requestHelper( request, function( response ) {
requestHandler.handle( request, function( err, response ) {
var actual = JSON.parse( response.body );
var expected = createJSON;

newId = actual._id.toString();
actual.notificationType.toString().should.equal( voiceMailId );
actual.notificationType._id.toString().should.equal( voiceMailId );
actual.key.should.equal( expected.key );
actual.registrationConfirmed.should.equal( expected.registrationConfirmed );
actual.devices[0].name.should.equal( expected.devices[0].name );
Expand All @@ -97,7 +81,7 @@ describe( 'Nodification.Tests.Integration.Controllers.RegistrationController', f
it( 'should return a single Registration when given an existing Id', function( done ) {
var request = localhost.request( 'GET', '/registrations/' + newId, {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'} );

requestHelper( request, function( response ) {
requestHandler.handle( request, function( err, response ) {
var actual = JSON.parse( response.body );
var expected = createJSON;

Expand All @@ -116,7 +100,7 @@ describe( 'Nodification.Tests.Integration.Controllers.RegistrationController', f
it( 'should return 404 NotFound when given a non-existing Id', function( done ) {
var request = localhost.request( 'GET', '/registrations/111111111111111111111111', {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'} );

requestHelper( request, function( response ) {
requestHandler.handle( request, function( err, response ) {
response.statusCode.should.equal( 404 );

done();
Expand All @@ -129,7 +113,7 @@ describe( 'Nodification.Tests.Integration.Controllers.RegistrationController', f
var request = localhost.request( 'POST', '/registrations/' + newId, {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'} );
request.write( JSON.stringify( updateJSON ) );

requestHelper( request, function( response ) {
requestHandler.handle( request, function( err, response ) {
var actual = JSON.parse( response.body );
var expected = updateJSON;

Expand All @@ -152,7 +136,7 @@ describe( 'Nodification.Tests.Integration.Controllers.RegistrationController', f
var request = localhost.request( 'POST', '/registrations/' + newId, {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'} );
request.write( JSON.stringify( updateDevicesJSON ) );

requestHelper( request, function( response ) {
requestHandler.handle( request, function( err, response ) {
var actual = JSON.parse( response.body );
var expected = updateJSON;
var expectedDevices = updateDevicesJSON;
Expand All @@ -178,7 +162,7 @@ describe( 'Nodification.Tests.Integration.Controllers.RegistrationController', f
it( 'should return all registrations', function( done ) {
var request = localhost.request( 'GET', '/registrations', {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'} );

requestHelper( request, function( response ) {
requestHandler.handle( request, function( err, response ) {
var actual = JSON.parse( response.body );
var expected = updateJSON;
var expectedDevices = updateDevicesJSON;
Expand Down Expand Up @@ -207,7 +191,7 @@ describe( 'Nodification.Tests.Integration.Controllers.RegistrationController', f
var request = localhost.request( 'DELETE', '/registrations/' + newId, {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'} );
request.write('{}');

requestHelper( request, function( response ) {
requestHandler.handle( request, function( err, response ) {
response.statusCode.should.equal( 200 );

done();
Expand Down

0 comments on commit 834894e

Please sign in to comment.