1
var assert = require('assert')
2
, http = require('http')
5
var Notifier = require('../lib/notifier')
12
'happy_retry_secs': 0.02
19
'happy_retry_secs': 0.02
22
suite('Notifier', function() {
23
setup(function(done) {
25
self.s = http.createServer(function(req, resp) {
26
self.s.emit(req.method, req, resp)
28
self.s.listen(0, 'localhost', function() {
29
self.url = 'http://localhost:' + self.s.address().port
38
test('happy-notify', function(done) {
40
this.s.on('POST', function(req, resp) {
41
req.on('data', function(chunk) {
44
req.on('end', function() {
45
resp.writeHead(200, {"Content-Type": "application/json"})
49
var n = new Notifier(this.url, cfg)
50
var approxExpire = new Date
51
approxExpire.setUTCMinutes(approxExpire.getUTCMinutes()+10)
52
n.notify("N", "T", {m: 42}, function() {
53
var reqObj = JSON.parse(b)
54
var expireOn = Date.parse(reqObj.expire_on)
55
delete reqObj.expire_on
56
assert.ok(expireOn >= approxExpire)
57
assert.deepEqual(reqObj, {
66
test('retry-notify', function(done) {
69
this.s.on('POST', function(req, resp) {
72
resp.writeHead(503, {"Content-Type": "application/json"})
76
req.on('data', function(chunk) {
79
req.on('end', function() {
80
resp.writeHead(200, {"Content-Type": "application/json"})
84
var n = new Notifier(this.url, cfg)
85
var approxExpire = new Date
86
approxExpire.setUTCMinutes(approxExpire.getUTCMinutes()+10)
87
n.notify("N", "T", {m: 42}, function() {
88
var reqObj = JSON.parse(b)
89
var expireOn = Date.parse(reqObj.expire_on)
90
delete reqObj.expire_on
91
assert.ok(expireOn >= approxExpire)
92
assert.deepEqual(reqObj, {
101
function flakyPOST(s, fail, tokens) {
102
s.on('POST', function(req, resp) {
104
req.on('data', function(chunk) {
107
req.on('end', function() {
108
var reqObj = JSON.parse(b)
111
resp.writeHead(503, {"Content-Type": "application/json"})
115
tokens[reqObj.token] = 1
116
resp.writeHead(200, {"Content-Type": "application/json"})
122
test('retry-notify-2-retries', function(done) {
124
flakyPOST(this.s, 2, tokens)
125
var n = new Notifier(this.url, cfg)
127
assert.deepEqual(tokens, {"T1": 1})
130
n.notify("N1", "T1", {m: 42}, yay)
133
test('retry-notify-2-batches', function(done) {
135
flakyPOST(this.s, 2, tokens)
136
var n = new Notifier(this.url, cfg)
141
assert.deepEqual(tokens, {"T1": 1, "T2": 1})
145
n.notify("N1", "T1", {m: 42}, yay)
146
n.notify("N2", "T2", {m: 42}, yay)
149
test('retry-notify-expired', function(done) {
151
flakyPOST(this.s, 2, tokens)
152
var n = new Notifier(this.url, cfg_batch2)
157
assert.deepEqual(tokens, {"T2": 1})
161
n.notify("N1", "T1", {m: 42}, yay, new Date)
162
n.notify("N2", "T2", {m: 42}, yay)
165
test('unknown-token-notify', function(done) {
166
this.s.on('POST', function(req, resp) {
167
resp.writeHead(400, {"Content-Type": "application/json"})
168
resp.end('{"error": "unknown-token"}')
170
var n = new Notifier(this.url, cfg)
171
n.on('unknownToken', function(nick, token) {
172
assert.equal(nick, "N")
173
assert.equal(token, "T")
176
n.notify("N", "T", {m: 42})
179
test('error-notify', function(done) {
180
this.s.on('POST', function(req, resp) {
184
var n = new Notifier(this.url, cfg)
185
n.on('pushError', function(err, resp, body) {
186
assert.equal(resp.statusCode, 500)
189
n.notify("N", "T", {m: 42})