aerogear-sender-client
Advanced tools
Comparing version 0.3.1 to 0.4.0
@@ -35,1 +35,10 @@ var agSender = require( "./lib/aerogear-sender-client" ).AeroGear, | ||
// Or you can use just the callback | ||
agSender.Sender( url ).send( message, settings, function( err, response ) { | ||
if( err ) { | ||
console.log( err ); | ||
return; | ||
} | ||
console.log( "success called", response ); | ||
}); |
@@ -23,3 +23,16 @@ /* Node.js Sender API for the AeroGear Unified Push server | ||
function send( serverSettings, message ) { | ||
function doEvent( status, message, callback ) { | ||
if( !callback || typeof callback !== "function" ) { | ||
this.emit( status, message ); | ||
return; | ||
} | ||
if( status === "error" ) { | ||
callback( message ); | ||
} else { | ||
callback( null, message ); | ||
} | ||
} | ||
function send( serverSettings, message, callback ) { | ||
// we stash the 'https' module on a local variable, IF the server is deployed using SSL. | ||
@@ -32,7 +45,7 @@ // Otherwise the 'http' module is stashed | ||
if( res.statusCode >= 400 ) { | ||
that.emit( "error", res.statusCode ); | ||
doEvent.call( that, "error", res.statusCode, callback ); | ||
} else if( res.statusCode === 301 || res.statusCode === 302 || res.statusCode === 303 ) { | ||
//Moved Status, Need to resend | ||
if( !res.headers.location ) { | ||
that.emit( "error", "redirect url is not available" ); | ||
doEvent.call( that, "error", "redirect url is not available", callback ); | ||
} else { | ||
@@ -47,3 +60,3 @@ var url = urlParser.parse( res.headers.location ); | ||
send.call( that, serverSettings, message ); | ||
send.call( that, serverSettings, message, callback ); | ||
} | ||
@@ -53,3 +66,3 @@ } else { | ||
res.on( "data", function ( chunk ) { | ||
that.emit( "success", chunk ); | ||
doEvent.call( that, "success", chunk, callback ); | ||
}); | ||
@@ -60,3 +73,3 @@ } | ||
req.on( "error", function( error ) { | ||
that.emit( "error", "problem with request: " + error.message ); | ||
doEvent.call( that, "error", "problem with request: " + error.message, callback ); | ||
}); | ||
@@ -93,2 +106,6 @@ | ||
AeroGear.Sender = function( url ) { | ||
if( !url ) { | ||
throw "UnifiedPushSenderError"; | ||
} | ||
if ( !( this instanceof AeroGear.Sender ) ) { | ||
@@ -131,6 +148,10 @@ return new AeroGear.Sender( url ); | ||
*/ | ||
AeroGear.Sender.prototype.send = function( message, settings ) { | ||
AeroGear.Sender.prototype.send = function( message, settings, callback ) { | ||
settings = settings || {}; | ||
if( !settings.applicationID || !settings.masterSecret ) { | ||
throw "UnifiedPushSenderError"; | ||
} | ||
var serverSettings, setting, crit, | ||
@@ -157,5 +178,5 @@ url = urlParser.parse( this.getUrl() ), | ||
send.call( this, serverSettings, newMessage ); | ||
send.call( this, serverSettings, newMessage, callback ); | ||
return this; | ||
}; |
{ | ||
"name": "aerogear-sender-client", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Sender api for the AeroGear Unified Push server", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/aerogear/aerogear-unifiedpush-nodejs-client", |
@@ -48,5 +48,7 @@ # aerogear-sender-client [![Build Status](https://secure.travis-ci.org/aerogear/aerogear-unifiedpush-nodejs-client.png?branch=master)](https://travis-ci.org/aerogear/aerogear-unifiedpush-nodejs-client) | ||
Send a Message | ||
### Send a Message | ||
You can use either listen for the success and error events | ||
agSender.Sender( url ).send( message, settings ).on( "success", function( response ) { | ||
@@ -56,1 +58,9 @@ console.log( "success called", response ); | ||
Or you can use a callback | ||
agSender.Sender( url ).send( message, settings, function( err, response ) { | ||
if( !err ) { | ||
console.log( "success called", response ); | ||
return; | ||
} | ||
}); |
@@ -6,3 +6,3 @@ var chai = require( "chai" ), | ||
AeroGear = require( "../lib/aerogear-sender-client" ).AeroGear, | ||
server; | ||
url = "http://localhost:8080/ag-push"; | ||
@@ -14,3 +14,3 @@ chai.use( sinonChai ); | ||
it( "Sender should be an object", function() { | ||
var sender = AeroGear.Sender(); | ||
var sender = AeroGear.Sender( url ); | ||
expect( sender ).to.be.an( "object" ); | ||
@@ -20,3 +20,3 @@ }); | ||
it( "Sender should inherit events", function() { | ||
var sender = AeroGear.Sender(); | ||
var sender = AeroGear.Sender( url ); | ||
expect( sender.emit ).to.exist; | ||
@@ -26,3 +26,3 @@ }); | ||
it( "Sender should have a send method", function() { | ||
var sender = AeroGear.Sender(); | ||
var sender = AeroGear.Sender( url ); | ||
expect( sender.send ).to.exist; | ||
@@ -32,10 +32,16 @@ }); | ||
it( "Sender should have a URL constructed", function() { | ||
var sender = AeroGear.Sender( "http://localhost:8080/ag-push" ); | ||
var sender = AeroGear.Sender( url ); | ||
expect( sender.getUrl() ).to.equal( "http://localhost:8080/ag-push/rest/sender/" ); | ||
}); | ||
}); | ||
describe( "Create", function() { | ||
it( "Sender should throw an error", function() { | ||
expect( function(){ AeroGear.Sender(); } ).to.throw( "UnifiedPushSenderError" ); | ||
}); | ||
}); | ||
}); | ||
describe( "Sender - send", function() { | ||
var sender = AeroGear.Sender( "http://localhost:8080/ag-push" ), | ||
var sender = AeroGear.Sender( url ), | ||
applicationID = "12345", | ||
@@ -87,4 +93,4 @@ masterSecret = "54321", | ||
describe( "Sender - Handle Moved Status Codes", function() { | ||
var sender = AeroGear.Sender( "http://localhost:8080/ag-push" ), | ||
describe( "Sender - send", function() { | ||
var sender = AeroGear.Sender( url ), | ||
applicationID = "12345", | ||
@@ -100,5 +106,6 @@ masterSecret = "54321", | ||
describe( "Endpoint returns moved", function() { | ||
it( "send should be called with success", function( done ) { | ||
describe( "send Method", function() { | ||
it( "send should be called with success and 'emit' success with callback as an object", function( done ) { | ||
nock( "http://localhost:8080" ) | ||
@@ -108,5 +115,3 @@ .matchHeader('Accept', 'application/json') | ||
.post( "/ag-push/rest/sender/" ) | ||
.reply( 302,{},{'location': "http://localhost:8080/rest/new/sender"} ) | ||
.post( "/rest/new/sender" ) | ||
.reply( 200, {} ); | ||
.reply( 200,{} ); | ||
@@ -116,3 +121,3 @@ settings.applicationID = applicationID; | ||
sender.send( message, settings ).on( "success", function( response ) { | ||
sender.send( message, settings, {} ).on( "success", function( response ) { | ||
expect( response ).to.be.ok; | ||
@@ -122,7 +127,36 @@ done(); | ||
}); | ||
it( "send should be called with error and 'emit' error with a callback as an object", function( done ) { | ||
nock( "http://localhost:8080" ) | ||
.matchHeader('Accept', 'application/json') | ||
.matchHeader('Content-type', 'application/json') | ||
.post( "/ag-push/rest/sender/" ).reply( 400,{} ); | ||
settings.applicationID = applicationID; | ||
settings.masterSecret = masterSecret; | ||
sender.send( message, settings, {} ).on( "error", function( error ) { | ||
expect( error ).to.be.ok; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe( "Endpoint returns moved, with no new location", function() { | ||
it( "send should be called with error", function( done ) { | ||
describe( "Sender - send", function() { | ||
var sender = AeroGear.Sender( url ), | ||
applicationID = "12345", | ||
masterSecret = "54321", | ||
settings = {}, | ||
message = {}; | ||
beforeEach( function() { | ||
settings = {}; | ||
message = {}; | ||
}); | ||
describe( "send Method with callback", function() { | ||
it( "send should be called with success", function( done ) { | ||
nock( "http://localhost:8080" ) | ||
@@ -132,3 +166,3 @@ .matchHeader('Accept', 'application/json') | ||
.post( "/ag-push/rest/sender/" ) | ||
.reply( 302,{},{} ); | ||
.reply( 200,{} ); | ||
@@ -138,13 +172,54 @@ settings.applicationID = applicationID; | ||
sender.send( message, settings ).on( "error", function( error ) { | ||
expect( error ).to.be.ok; | ||
expect( error ).to.equal( "redirect url is not available" ); | ||
sender.send( message, settings, function( err, response ) { | ||
expect( response ).to.be.ok; | ||
done(); | ||
}); | ||
}); | ||
it( "send should be called with error", function( done ) { | ||
nock( "http://localhost:8080" ) | ||
.matchHeader('Accept', 'application/json') | ||
.matchHeader('Content-type', 'application/json') | ||
.post( "/ag-push/rest/sender/" ).reply( 400,{} ); | ||
settings.applicationID = applicationID; | ||
settings.masterSecret = masterSecret; | ||
sender.send( message, settings, function( err ) { | ||
expect( err ).to.be.ok; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe( "Sender", function() { | ||
var sender = AeroGear.Sender( url ), | ||
applicationID = "12345", | ||
masterSecret = "54321", | ||
settings = {}, | ||
message = {}; | ||
beforeEach( function() { | ||
settings = {}; | ||
message = {}; | ||
}); | ||
describe( "send - throw errors", function() { | ||
it( "send should throw an error with no masterSecret", function() { | ||
settings.applicationID = applicationID; | ||
expect( function(){ sender.send( message, settings ); } ).to.throw( "UnifiedPushSenderError" ); | ||
}); | ||
it( "send should throw an error with no applicationID", function() { | ||
settings.masterSecret = masterSecret; | ||
expect( function(){ sender.send( message, settings ); } ).to.throw( "UnifiedPushSenderError" ); | ||
}); | ||
it( "send should throw an error with no applicationID and no masterSecret", function() { | ||
settings.masterSecret = masterSecret; | ||
expect( function(){ sender.send( message, settings ); } ).to.throw( "UnifiedPushSenderError" ); | ||
}); | ||
}); | ||
}); | ||
describe( "Sender - Handle Moved Status Codes", function() { | ||
var sender = AeroGear.Sender( "http://localhost:8080/ag-push" ), | ||
var sender = AeroGear.Sender( url ), | ||
applicationID = "12345", | ||
@@ -151,0 +226,0 @@ masterSecret = "54321", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
134908
11
4866
65