@@ -49,25 +49,25 @@ suite('sendNotification', function() {
49
49
} ) ;
50
50
51
51
req . on ( 'end' , function ( ) {
52
- assert . equal ( req . headers [ 'content-length' ] , body . length , 'Content-Length header correct' ) ;
52
+ try {
53
+ assert . equal ( req . headers [ 'content-length' ] , body . length , 'Content-Length header correct' ) ;
53
54
54
- if ( typeof TTL !== 'undefined' ) {
55
- assert . equal ( req . headers [ 'ttl' ] , TTL , 'TTL header correct' ) ;
56
- }
55
+ if ( typeof TTL !== 'undefined' ) {
56
+ assert . equal ( req . headers [ 'ttl' ] , TTL , 'TTL header correct' ) ;
57
+ }
57
58
58
- if ( typeof message !== 'undefined' ) {
59
- assert ( body . length > 0 ) ;
59
+ if ( typeof message !== 'undefined' ) {
60
+ assert ( body . length > 0 ) ;
60
61
61
- assert . equal ( req . headers [ 'encryption' ] . indexOf ( 'keyid=p256dh;salt=' ) , 0 , 'Encryption header correct' ) ;
62
- var salt = req . headers [ 'encryption' ] . substring ( 'keyid=p256dh;salt=' . length ) ;
62
+ assert . equal ( req . headers [ 'encryption' ] . indexOf ( 'keyid=p256dh;salt=' ) , 0 , 'Encryption header correct' ) ;
63
+ var salt = req . headers [ 'encryption' ] . substring ( 'keyid=p256dh;salt=' . length ) ;
63
64
64
- if ( ! isGCM ) {
65
65
assert . equal ( req . headers [ 'content-type' ] , 'application/octet-stream' , 'Content-Type header correct' ) ;
66
66
67
- var keys = req . headers [ 'crypto-key' ] . split ( ', ' ) ;
67
+ var keys = req . headers [ 'crypto-key' ] . split ( '; ' ) ;
68
68
var appServerPublicKey = keys . find ( function ( key ) {
69
- return key . indexOf ( 'keyid=p256dh; dh=' ) === 0 ;
70
- } ) . substring ( 'keyid=p256dh; dh=' . length ) ;
69
+ return key . indexOf ( 'dh=' ) === 0 ;
70
+ } ) . substring ( 'dh=' . length ) ;
71
71
72
72
assert . equal ( req . headers [ 'content-encoding' ] , 'aesgcm' , 'Content-Encoding header correct' ) ;
73
73
@@ -80,61 +80,45 @@ suite('sendNotification', function() {
80
80
authSecret : urlBase64 . encode ( userAuth ) ,
81
81
padSize : 2 ,
82
82
} ) ;
83
- } else {
84
- assert . equal ( req . headers [ 'crypto-key' ] . indexOf ( 'keyid=p256dh;dh=' ) , 0 , 'Crypto-Key header correct' ) ;
85
- assert . equal ( req . headers [ 'content-encoding' ] , 'aesgcm' , 'Content-Encoding header correct' ) ;
86
- var appServerPublicKey = req . headers [ 'crypto-key' ] . substring ( 'keyid=p256dh;dh=' . length ) ;
87
-
88
- ece . saveKey ( 'webpushKey' , userCurve , 'P-256' ) ;
89
83
90
- var raw_data = urlBase64 . decode ( JSON . parse ( body ) . raw_data ) ;
84
+ assert ( decrypted . equals ( new Buffer ( message ) ) , "Cipher text correctly decoded" ) ;
85
+ }
91
86
92
- var decrypted = ece . decrypt ( raw_data , {
93
- keyid : 'webpushKey' ,
94
- dh : appServerPublicKey ,
95
- salt : salt ,
96
- authSecret : urlBase64 . encode ( userAuth ) ,
97
- padSize : 2 ,
87
+ if ( vapid ) {
88
+ var keys = req . headers [ 'crypto-key' ] . split ( ';' ) ;
89
+ var vapidKey = keys . find ( function ( key ) {
90
+ return key . indexOf ( 'p256ecdsa=' ) === 0 ;
98
91
} ) ;
92
+
93
+ assert . equal ( vapidKey . indexOf ( 'p256ecdsa=' ) , 0 , 'Crypto-Key header correct' ) ;
94
+ var appServerVapidPublicKey = urlBase64 . decode ( vapidKey . substring ( 'p256ecdsa=' . length ) ) ;
95
+
96
+ assert ( appServerVapidPublicKey . equals ( vapidKeys . publicKey ) ) ;
97
+
98
+ var authorizationHeader = req . headers [ 'authorization' ] ;
99
+ assert . equal ( authorizationHeader . indexOf ( 'Bearer ' ) , 0 , 'Authorization header correct' ) ;
100
+ var jwt = authorizationHeader . substring ( 'Bearer ' . length ) ;
101
+ //assert(jws.verify(jwt, 'ES256', appServerVapidPublicKey)), 'JWT valid');
102
+ var decoded = jws . decode ( jwt ) ;
103
+ assert . equal ( decoded . header . typ , 'JWT' ) ;
104
+ assert . equal ( decoded . header . alg , 'ES256' ) ;
105
+ assert . equal ( decoded . payload . aud , 'https://www.mozilla.org/' ) ;
106
+ assert ( decoded . payload . exp > Date . now ( ) / 1000 ) ;
107
+ assert . equal ( decoded . payload . sub , 'mailto:[email protected] ' ) ;
99
108
}
100
109
101
- assert ( decrypted . equals ( new Buffer ( message ) ) , "Cipher text correctly decoded" ) ;
102
- }
110
+ if ( isGCM ) {
111
+ assert . equal ( req . headers [ 'authorization' ] , 'key=my_gcm_key' , 'Authorization header correct' ) ;
112
+ }
103
113
104
- if ( vapid ) {
105
- var keys = req . headers [ 'crypto-key' ] . split ( ',' ) ;
106
- var vapidKey = keys . find ( function ( key ) {
107
- return key . indexOf ( 'p256ecdsa=' ) === 0 ;
108
- } ) ;
109
-
110
- assert . equal ( vapidKey . indexOf ( 'p256ecdsa=' ) , 0 , 'Crypto-Key header correct' ) ;
111
- var appServerVapidPublicKey = urlBase64 . decode ( vapidKey . substring ( 'p256ecdsa=' . length ) ) ;
112
-
113
- assert ( appServerVapidPublicKey . equals ( vapidKeys . publicKey ) ) ;
114
-
115
- var authorizationHeader = req . headers [ 'authorization' ] ;
116
- assert . equal ( authorizationHeader . indexOf ( 'Bearer ' ) , 0 , 'Authorization header correct' ) ;
117
- var jwt = authorizationHeader . substring ( 'Bearer ' . length ) ;
118
- //assert(jws.verify(jwt, 'ES256', appServerVapidPublicKey)), 'JWT valid');
119
- var decoded = jws . decode ( jwt ) ;
120
- assert . equal ( decoded . header . typ , 'JWT' ) ;
121
- assert . equal ( decoded . header . alg , 'ES256' ) ;
122
- assert . equal ( decoded . payload . aud , 'https://www.mozilla.org/' ) ;
123
- assert ( decoded . payload . exp > Date . now ( ) / 1000 ) ;
124
- assert . equal ( decoded . payload . sub , 'mailto:[email protected] ' ) ;
125
- }
114
+ res . writeHead ( statusCode ? statusCode : 201 ) ;
126
115
127
- if ( isGCM ) {
128
- assert . equal ( JSON . stringify ( JSON . parse ( body ) . registration_ids ) , '["someSubscriptionID"]' ) ;
129
- assert . equal ( req . headers [ 'authorization' ] , 'key=my_gcm_key' , 'Authorization header correct' ) ;
130
- assert . equal ( req . headers [ 'content-type' ] , 'application/json' , 'Content-Type header correct' ) ;
131
- assert . equal ( req . headers [ 'content-length' ] , body . length , 'Content-Length header correct' ) ;
116
+ res . end ( statusCode !== 404 ? 'ok' : 'not found' ) ;
117
+ } catch ( err ) {
118
+ console . error ( err ) ;
119
+ res . writeHead ( 500 ) ;
120
+ res . end ( ) ;
132
121
}
133
-
134
- res . writeHead ( statusCode ? statusCode : 201 ) ;
135
-
136
- res . end ( statusCode !== 404 ? 'ok' : 'not found' ) ;
137
-
138
122
server . close ( ) ;
139
123
} ) ;
140
124
} ) ;
@@ -334,7 +318,7 @@ suite('sendNotification', function() {
334
318
335
319
webPush . setGCMAPIKey ( 'my_gcm_key' ) ;
336
320
337
- return startServer ( undefined , undefined , 200 , true )
321
+ return startServer ( undefined , undefined , undefined , true )
338
322
. then ( function ( ) {
339
323
return webPush . sendNotification ( 'https://android.googleapis.com/gcm/send/someSubscriptionID' ) ;
340
324
} )
@@ -356,7 +340,7 @@ suite('sendNotification', function() {
356
340
357
341
webPush . setGCMAPIKey ( 'my_gcm_key' ) ;
358
342
359
- return startServer ( 'hello' , undefined , 200 , true )
343
+ return startServer ( 'hello' , undefined , undefined , true )
360
344
. then ( function ( ) {
361
345
return webPush . sendNotification ( 'https://android.googleapis.com/gcm/send/someSubscriptionID' , {
362
346
userPublicKey : urlBase64 . encode ( userPublicKey ) ,
@@ -498,7 +482,7 @@ suite('sendNotification', function() {
498
482
499
483
webPush . setGCMAPIKey ( 'my_gcm_key' ) ;
500
484
501
- return startServer ( undefined , undefined , 200 , true )
485
+ return startServer ( undefined , undefined , undefined , true )
502
486
. then ( function ( ) {
503
487
return webPush . sendNotification ( 'https://android.googleapis.com/gcm/send/someSubscriptionID' , {
504
488
vapid : {
0 commit comments