-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathmessage.js
476 lines (401 loc) · 13.6 KB
/
message.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
var MAX_PAYLOAD_SIZE = 2048;
describe('Message', function () {
var sample_token = '5b51030d d5bad758 fbad5004 bad35c31 e4e0f550 f77f20d4 f737bf8d 3d5524c6'
, device = new Buffer(sample_token.replace(/\s/g, ''), 'hex');
var agent = new apnagent.MockAgent()
, Message = __apnagent.Message;
describe('.set()', function () {
it('should set key/value pairs', function () {
var msg = new Message()
, res = msg.set('answer', 42);
msg.should
.have.property('payload')
.an('object')
.that.deep.equals({ answer: 42 });
res.should.deep.equal(msg);
});
it('should set object', function () {
var msg = new Message()
, res = msg.set({ answer: 42, who: 'deep thought' });
msg.should
.have.property('payload')
.an('object')
.that.deep.equals({
answer: 42
, who: 'deep thought'
});
res.should.deep.equal(msg);
});
});
describe('.badge()', function () {
it('should set badge', function () {
var msg = new Message()
, res = msg.badge(42);
msg.should.have.property('settings')
.and.have.property('badge', 42);
res.should.deep.equal(msg);
});
});
describe('.contentAvailable(true)', function () {
it('should set content-available flag', function () {
var msg = new Message()
, res = msg.contentAvailable(true);
msg.should
.have.property('settings')
.and.have.property('content-available', 1);
res.should.deep.equal(msg);
});
});
describe('.contentAvailable(false)', function () {
it('should set content-available flag to 0', function () {
var msg = new Message()
, res = msg.contentAvailable(false);
msg.should
.have.property('settings')
.and.not.have.property('content-available');
res.should.deep.equal(msg);
});
});
describe('.sound()', function () {
it('should set sound', function () {
var msg = new Message()
, res = msg.sound('bell');
msg.should
.have.property('settings')
.and.have.property('sound', 'bell');
res.should.deep.equal(msg);
});
});
describe('.alert()', function () {
it('should set key/value pairs', function () {
var msg = new Message();
msg
.alert('body', 'Hello Universe')
.alert('action-loc-key', 'KEY')
.alert('loc-key', 'LOCKEY')
.alert('loc-args', [ 'one', 'two' ])
.alert('launch-image', 'img.png')
.alert('ignore-me', true)
.alert('title', 'Greeting')
.alert('title-loc-key', 'TITLE-LOCKEY')
.alert('title-loc-args', ['three', 'four'])
.alert('actions', [
{
"id" : "delete",
"title" : "Delete"
},
{
"id" : "reply-to",
"loc-key" : "REPLYTO",
"loc-args" : ["Jane"]
}]);
msg.should.have.property('aps').an('object');
msg.aps.should.not.have.property('ignore-me');
msg.aps.should.deep.equal({
'body': 'Hello Universe'
, 'action-loc-key': 'KEY'
, 'loc-key': 'LOCKEY'
, 'loc-args': [ 'one', 'two' ]
, 'launch-image': 'img.png'
, 'title': 'Greeting'
, 'title-loc-key': 'TITLE-LOCKEY'
, 'title-loc-args': ['three', 'four']
, 'actions': [
{
"id" : "delete",
"title" : "Delete"
},
{
"id" : "reply-to",
"loc-key" : "REPLYTO",
"loc-args" : ["Jane"]
}]
});
});
it("should reject non array values for args and actions", function() {
var msg = new Message();
msg
.alert('loc-args', 'one')
.alert('title-loc-args', 'three')
.alert('actions', {
'id' : 'delete',
'title' : 'Delete'
});
msg.should.have.property('aps').an('object');
msg.aps.should.not.have.property('loc-args');
msg.aps.should.not.have.property('title-loc-args');
msg.aps.should.not.have.property('actions');
});
it('should set object', function () {
var msg = new Message();
msg.alert({
'body': 'Hello Universe'
, 'action-loc-key': 'KEY'
, 'loc-key': 'LOCKEY'
, 'loc-args': [ 'one', 'two' ]
, 'launch-image': 'img.png'
, 'ignore-me': true
, 'title': 'Greeting'
, 'title-loc-key': 'TITLE-LOCKEY'
, 'title-loc-args': ['three', 'four']
});
msg.should.have.property('aps').an('object');
msg.aps.should.not.have.property('ignore-me');
msg.aps.should.deep.equal({
'body': 'Hello Universe'
, 'action-loc-key': 'KEY'
, 'loc-key': 'LOCKEY'
, 'loc-args': [ 'one', 'two' ]
, 'launch-image': 'img.png'
, 'title': 'Greeting'
, 'title-loc-key': 'TITLE-LOCKEY'
, 'title-loc-args': ['three', 'four']
});
});
});
describe('.category()', function () {
it('should not have a category upon creation', function () {
var msg = new Message();
msg.should.have.property('settings').an('object');
msg.settings.should.not.have.property('category');
});
it('should set category', function () {
var msg = new Message();
msg.category('INVITE_CATEGORY');
msg.should.have.property('settings').an('object');
msg.settings.should.have.property('category');
msg.settings.category.should.equal('INVITE_CATEGORY');
});
it('should reject non-string values', function () {
var msg = new Message();
msg
.category(5)
.category({x:5})
.category([5]);
msg.should.have.property('settings').an('object');
msg.settings.should.not.have.property('category');
});
});
describe('.device()', function () {
it('should set as buffer', function () {
var msg = new Message()
, res = msg.device(device);
msg.meta.device.toString().should.equal(device.toString('hex'));
res.should.deep.equal(msg);
});
it('should set as string', function () {
var msg = new Message()
, res = msg.device(sample_token);
msg.meta.device.toString().should.equal(device.toString('hex'));
res.should.deep.equal(msg);
});
});
describe('.serialize()', function () {
var longBody = new Array(1900).concat([
'Hello Universe. I am going to make'
, 'this string really long so that it is truncated'
, 'when I attempt to convert it to a string. If you are'
, 'sending messages that are this long then you really'
, 'need to understand what a notification is.'
]).join(' ');
var longBodyUnicode = new Array(1800).concat([
'Οσα συμβαίνουν στην Ουκρανία έχουν ανοίξει'
, 'μια εξαιρετικά ενδιαφέρουσα συζήτηση για το'
, 'κατά πόσον οδεύουμε προς μια νέα παγκόσμια γεωπολιτική'
, 'συγκυρία. Κάποιοι πιστεύουν ότι ο χαμένος διπολισμός'
, 'του Ψυχρού Πολέμου επανέρχεται, άλλοι θεωρούν ότι η'
, 'σημερινή Ρωσία δεν μπορεί να παίξει τον ρόλο.'
]).join(' ');
it('should get payload when only alert body', function () {
var msg = new Message();
msg
.device('00')
.set('custom', 'variable')
.alert('body', 'Hello Universe')
.badge(1);
var json = msg.serialize();
Buffer.byteLength(JSON.stringify(json.payload), msg.encoding)
.should.not.be.above(MAX_PAYLOAD_SIZE);
json.payload.should.deep.equal({
custom: 'variable'
, aps: {
alert: 'Hello Universe'
, badge: 1
}
});
});
it('should get payload for complex alert', function () {
var msg = new Message();
msg
.device('00')
.set('custom', 'variable')
.alert('body', 'Hello Universe')
.alert('loc-key', 'SOME_KEY')
.badge(3)
.sound('ping');
var json = msg.serialize();
Buffer.byteLength(JSON.stringify(json.payload), msg.encoding)
.should.not.be.above(MAX_PAYLOAD_SIZE);
json.payload.should.deep.equal({
custom: 'variable'
, aps: {
alert: {
body: 'Hello Universe'
, 'loc-key': 'SOME_KEY'
}
, badge: 3
, sound: 'ping'
}
});
});
it('should have no alert body when alert not specified', function() {
var msg = new Message();
msg.device('00').sound('default');
var json = msg.serialize();
json.payload.should.deep.equal({
aps: {
sound: 'default'
}
});
});
it('should truncate when only alert body', function () {
var msg = new Message();
msg
.device('feedface')
.set('custom', 'variable')
.alert('body', longBody)
.badge(1);
var json = msg.serialize();
Buffer.byteLength(JSON.stringify(json.payload), msg.encoding)
.should.not.be.above(MAX_PAYLOAD_SIZE);
json.payload.should.deep.equal({
custom: 'variable'
, aps: {
alert:
new Array(1900).concat([ 'Hello Universe. I am going to make'
, 'this string really long so that it is truncated'
, 'when I...'
]).join(' ')
, badge: 1
}
});
});
it('should truncate when only alert body (unicode)', function () {
var msg = new Message();
msg
.device('feedface')
.set('custom', 'variable')
.alert('body', longBodyUnicode)
.badge(1);
var json = msg.serialize();
Buffer.byteLength(JSON.stringify(json.payload), msg.encoding)
.should.not.be.above(MAX_PAYLOAD_SIZE);
json.payload.should.deep.equal({
custom: 'variable'
, aps: {
alert:
new Array(1800).concat([ 'Οσα συμβαίνουν στην Ουκρανία έχουν ανοίξει'
, 'μια εξαιρετικά ενδιαφέρουσα συζήτηση για το'
, 'κατά πόσον...'
]).join(' ')
, badge: 1
}
});
});
it('should truncate for complex alert body', function () {
var msg = new Message();
msg
.device('00')
.set('custom', 'variable')
.alert('body', longBody)
.alert('launch-image', 'img.png')
.badge(1);
var json = msg.serialize();
Buffer.byteLength(JSON.stringify(json.payload), msg.encoding)
.should.not.be.above(MAX_PAYLOAD_SIZE);
json.payload.should.deep.equal({
custom: 'variable'
, aps: {
alert: {
body:
new Array(1900).concat([ 'Hello Universe. I am going to make'
, 'this string really long...'
]).join(' ')
, 'launch-image': 'img.png'
}
, badge: 1
}
});
});
it('should truncate for complex alert body (unicode)', function () {
var msg = new Message();
msg
.device('00')
.set('custom', 'variable')
.alert('body', longBodyUnicode)
.alert('launch-image', 'img.png')
.badge(1);
var json = msg.serialize();
Buffer.byteLength(JSON.stringify(json.payload), msg.encoding)
.should.not.be.above(MAX_PAYLOAD_SIZE);
json.payload.should.deep.equal({
custom: 'variable'
, aps: {
alert: {
body:
new Array(1800).concat([ 'Οσα συμβαίνουν στην Ουκρανία έχουν ανοίξει'
, 'μια εξαιρετικά ενδιαφέρουσα συζήτηση για...'
]).join(' ')
, 'launch-image': 'img.png'
}
, badge: 1
}
});
});
it('should throw when too long w/ only alert body', function () {
var msg = new Message();
msg
.device('feedface')
.set('custom_body', longBody)
.alert('body', 'Hello Universe')
.badge(1);
(function () {
msg.serialize();
}).should.throw('Message too long.');
});
it('should throw when too long w/ complex alert body', function () {
var msg = new Message();
msg
.device('feedface')
.set('custom_body', longBody)
.alert('body', 'Hello Universe')
.alert('launch-image', 'img.png')
.badge(1);
(function () {
msg.serialize();
}).should.throw('Message too long.');
});
it('should throw when device not specified', function () {
var msg = new Message();
(function () {
msg.serialize();
}).should.throw('Message device not specified.');
});
it('should throw when codec enhanced and no expiration', function () {
var msg = new Message(null, 'enhanced');
msg.device('feedface');
(function () {
msg.serialize();
}).should.throw('Message expiration not specified for enhanced codec delivery.');
});
it('should throw when codec enhanced and no agent', function () {
var msg = new Message(null, 'enhanced');
msg
.device('feedface')
.expires('1d');
(function () {
msg.serialize();
}).should.throw('Message agent not specified for enhanced codec delivery.');
});
});
});