Skip to content

Commit

Permalink
[fix] 200s don't respond with JSON on POST.
Browse files Browse the repository at this point in the history
[test] Upgraded the tests to include getDeviceTokenCounts and make
broadcast optional
  • Loading branch information
cojohn committed Jan 31, 2012
1 parent 2620e31 commit c750919
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 29 deletions.
3 changes: 2 additions & 1 deletion lib/urban-airship.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ UrbanAirship.prototype.pushNotification = function(path, payload, callback) {
this._transport(path, "POST", payload, callback);
}


/*
* Register a new device.
*
Expand Down Expand Up @@ -142,7 +143,7 @@ UrbanAirship.prototype._transport = function(path, method, request_data, callbac
// Success on 200 or 204, 201 on new device registration
else if ([200,201,204].indexOf(response.statusCode) >= 0) {
try {
callback(null, response_data !== "" && JSON.parse(response_data));
callback(null, (response_data !== "" && response_data !== "OK" && JSON.parse(response_data)) || response_data);
}
catch (ex) {
callback(ex);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
],
"name": "urban-airship",
"description": "Urban Airship API wrapper.",
"version": "0.2.1",
"version": "0.2.2",
"repository": {
"type": "git",
"url": "git://github.com/cojohn/node-urban-airship.git"
Expand Down
135 changes: 108 additions & 27 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,115 @@
var UA = require("../lib/urban-airship"),
ua = new UA("<api key>", "<api secret key>", "<api master key>");

var payload0 = {
"device_tokens": ['device_id(s)'],
"aps": {
"alert": 'First Hello',
"badge": 16
}
};
// Your API key
var API_KEY = "",
// Your API master key
MASTER_KEY = "",
// Your API secret key
SECRET_KEY = "",
// 1 to run a broadcast test, 0 to skip
TEST_BROADCAST = 0,
// The number of test events to wait for
EXPECTED_TESTS = 3 + TEST_BROADCAST,
// The device_token for your test device
TEST_DEVICE = "",
UrbanAirship = require("../lib/urban-airship"),
urban_airship = new UrbanAirship(API_KEY, SECRET_KEY, MASTER_KEY),
events = require("events"),
util = require("util");

ua.registerDevice("<token>", function(error) {
ua.pushNotification("/api/push/", payload0, function(error) {
ua.unregisterDevice("<token>", function(error) {

});
var Test = function() {
events.EventEmitter.call(this);
}

util.inherits(Test, events.EventEmitter);

Test.prototype.pushBroadcast = function() {
var self = this,
payload = {
"aps": {
"alert": "Hello everyone!",
"badge": 3
}
};

urban_airship.pushNotification("/api/push/broadcast/", payload, function(error) {
self.emit("finished", error, "pushBroadcast")
});
});
}

Test.prototype.pushNotification = function() {
var self = this,
payload = {
"device_tokens": [TEST_DEVICE],
"aps": {
"alert": "Hello test device!",
"badge": 3
}
};

urban_airship.pushNotification("/api/push/", payload, function(error) {
self.emit("finished", error, "pushNotification")
});
}

var test = new Test(),
failed = 0,
passed = 0;

var payload1 = {
"aps": {
"badge": 15,
"alert": "Second Hello!"
},
"exclude_tokens": [
"device token you want to skip",
"another device token you want to skip"
]
};

ua.pushNotification("/api/push/broadcast/", payload1, function(error) {
test.on("finished", function(error, test_name) {
if (error) {
failed += 1;
console.log("Failed " + test_name + ".");
error.message && console.log(error.message);
error.stack && console.log(error.stack);
}
else {
passed += 1;
console.log("Passed " + test_name + ".");
}

if (passed + failed === EXPECTED_TESTS) {
urban_airship.unregisterDevice(TEST_DEVICE, function(error) {
if (error) {
failed += 1;
console.log("Failed unregisterDevice.");
}
else {
passed += 1;
console.log("Passed unregisterDevice.");
}
console.log("Completed " + (EXPECTED_TESTS + 1) + " tests (" + passed + " passed, " + failed + " failed).");
process.exit();
});
}
});

test.on("registered", function(error) {
if (error) {
error.messge && console.log(error.message);
error.stack && console.log(error.stack);
process.exit();
}
else {
passed += 1;
console.log("Passed registerDevice.");

if (TEST_BROADCAST) {
test.pushBroadcast();
}

test.pushNotification();

urban_airship.getDeviceTokenCounts(function(error, total, active) {
if ((!total && isNaN(total)) || (!active && isNaN(active))) {
test.emit("finished", new Error("Bogus data: " + total + ", " + active + "."), "getDeviceTokenCounts");
}
else {
test.emit("finished", error, "getDeviceTokenCounts");
}
});
}
});

urban_airship.registerDevice(TEST_DEVICE, function(error) {
test.emit("registered", error);
});

0 comments on commit c750919

Please sign in to comment.