-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix] 200s don't respond with JSON on POST.
[test] Upgraded the tests to include getDeviceTokenCounts and make broadcast optional
- Loading branch information
Showing
3 changed files
with
111 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |