Skip to content

Commit 19a53a4

Browse files
author
Matt Gaunt
committed
review feedback
1 parent 18a9678 commit 19a53a4

File tree

2 files changed

+219
-8
lines changed

2 files changed

+219
-8
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,8 @@ const vapidHeaders = vapidHelper.getVapidHeaders(
293293
);
294294
```
295295

296-
Encrypts the payload according to the [Message Encryption for Web
297-
Push](https://webpush-wg.github.io/webpush-encryption/) standard.
298-
299-
> (*sendNotification* will automatically encrypt the payload for you, so if
300-
> you use *sendNotification* you don't need to worry about it).
296+
The *getVapidHeaders()* method will take in the values needed to create a
297+
an Authorization and Crypto-Key header.
301298

302299
### Input
303300

@@ -353,8 +350,11 @@ try {
353350
}
354351
```
355352

356-
> **Note:** `generateRequestDetails()` you don't need to define a payload,
357-
and this method will return details without a GCM API Key and / or VAPID keys.
353+
> **Note:** When calling `generateRequestDetails()` the payload argument
354+
does not *need* to be defined, passing in null will return no body and
355+
> exclude any unnesscary headers.
356+
> Headers related to the GCM API Key and / or VAPID keys will be included
357+
> if supplied and required.
358358
359359
### Input
360360

@@ -391,7 +391,7 @@ retained by the push service (by default, four weeks);
391391

392392
### Returns
393393

394-
An object contains all the details needed to make a network request, the
394+
An object containing all the details needed to make a network request, the
395395
object will contain:
396396

397397
- *endpoint*, the URL to send the request to;

test/test-generate-request-details.js

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const urlBase64 = require('urlsafe-base64');
5+
const webPush = require('../src/index');
6+
const crypto = require('crypto');
7+
8+
suite('Test Generate Request Details', function() {
9+
test('is defined', function() {
10+
assert(webPush.generateRequestDetails);
11+
});
12+
13+
const userCurve = crypto.createECDH('prime256v1');
14+
const userPublicKey = userCurve.generateKeys();
15+
const userAuth = crypto.randomBytes(16);
16+
const vapidKeys = require('../src/vapid-helper').generateVAPIDKeys();
17+
18+
const VALID_KEYS = {
19+
p256dh: urlBase64.encode(userPublicKey),
20+
auth: urlBase64.encode(userAuth)
21+
};
22+
23+
const invalidRequests = [
24+
{
25+
testTitle: '0 arguments',
26+
requestOptions: {
27+
28+
}
29+
}, {
30+
testTitle: 'No Endpoint',
31+
requestOptions: {
32+
subscription: {}
33+
}
34+
}, {
35+
testTitle: 'Empty Endpoint',
36+
requestOptions: {
37+
subscription: {
38+
endpoint: ''
39+
}
40+
}
41+
}, {
42+
testTitle: 'Array for Endpoint',
43+
requestOptions: {
44+
subscription: {
45+
endpoint: []
46+
}
47+
}
48+
}, {
49+
testTitle: 'Object for Endpoint',
50+
requestOptions: {
51+
subscription: {
52+
endpoint: {}
53+
}
54+
}
55+
}, {
56+
testTitle: 'Object for Endpoint',
57+
requestOptions: {
58+
subscription: {
59+
endpoint: true
60+
}
61+
}
62+
}, {
63+
testTitle: 'Payload provided with no keys',
64+
requestOptions: {
65+
subscription: {
66+
endpoint: true
67+
},
68+
message: 'hello'
69+
}
70+
}, {
71+
testTitle: 'Payload provided with invalid keys',
72+
requestOptions: {
73+
subscription: {
74+
endpoint: true,
75+
keys: 'silly example'
76+
},
77+
message: 'hello'
78+
}
79+
}, {
80+
testTitle: 'Payload provided with only p256dh keys',
81+
requestOptions: {
82+
subscription: {
83+
endpoint: true,
84+
keys: {
85+
p256dh: urlBase64.encode(userPublicKey)
86+
}
87+
},
88+
message: 'hello'
89+
}
90+
}, {
91+
testTitle: 'Payload provided with only auth keys',
92+
requestOptions: {
93+
subscription: {
94+
endpoint: true,
95+
keys: {
96+
auth: urlBase64.encode(userAuth)
97+
}
98+
},
99+
message: 'hello'
100+
}
101+
}, {
102+
testTitle: 'userPublicKey argument isn\'t a string',
103+
requestOptions: {
104+
subscription: {
105+
keys: {
106+
p256dh: userPublicKey,
107+
auth: urlBase64.encode(userAuth)
108+
}
109+
},
110+
message: 'hello'
111+
},
112+
addEndpoint: true
113+
}, {
114+
testTitle: 'userAuth argument isn\'t a string',
115+
requestOptions: {
116+
subscription: {
117+
keys: {
118+
p256dh: urlBase64.encode(userPublicKey),
119+
auth: userAuth
120+
}
121+
},
122+
message: 'hello'
123+
},
124+
addEndpoint: true
125+
}, {
126+
testTitle: 'userPublicKey argument is too long',
127+
requestOptions: {
128+
subscription: {
129+
keys: {
130+
p256dh: urlBase64.encode(Buffer.concat([userPublicKey, new Buffer(1)])),
131+
auth: urlBase64.encode(userAuth)
132+
}
133+
},
134+
message: 'hello'
135+
},
136+
addEndpoint: true
137+
}, {
138+
testTitle: 'userPublicKey argument is too short',
139+
requestOptions: {
140+
subscription: {
141+
keys: {
142+
p256dh: urlBase64.encode(userPublicKey.slice(1)),
143+
auth: urlBase64.encode(userAuth)
144+
}
145+
},
146+
message: 'hello'
147+
},
148+
addEndpoint: true
149+
}, {
150+
testTitle: 'userAuth argument is too short',
151+
requestOptions: {
152+
subscription: {
153+
keys: {
154+
p256dh: urlBase64.encode(userPublicKey),
155+
auth: urlBase64.encode(userAuth.slice(1))
156+
}
157+
},
158+
message: 'hello'
159+
},
160+
addEndpoint: true
161+
}, {
162+
testTitle: 'rejects when payload isn\'t a string or buffer',
163+
requestOptions: {
164+
subscription: {
165+
keys: VALID_KEYS
166+
},
167+
message: []
168+
},
169+
addEndpoint: true,
170+
serverFlags: ['statusCode=404']
171+
}, {
172+
testTitle: 'send notification with invalid vapid option',
173+
requestOptions: {
174+
subscription: {
175+
keys: VALID_KEYS
176+
},
177+
message: 'hello',
178+
addEndpoint: true,
179+
extraOptions: {
180+
vapid: {
181+
subject: 'mailto:[email protected]',
182+
privateKey: vapidKeys.privateKey,
183+
publicKey: vapidKeys.publicKey
184+
}
185+
}
186+
}
187+
}
188+
];
189+
190+
invalidRequests.forEach(function(invalidRequest) {
191+
test(invalidRequest.testTitle, function() {
192+
if (invalidRequest.addEndpoint) {
193+
invalidRequest.requestOptions.subscription.endpoint =
194+
'https://127.0.0.1:8080';
195+
}
196+
197+
if (invalidRequest.serverFlags) {
198+
invalidRequest.requestOptions.subscription.endpoint += '?' +
199+
invalidRequest.serverFlags.join('&');
200+
}
201+
202+
assert.throws(function() {
203+
return webPush.generateRequestDetails(
204+
invalidRequest.requestOptions.subscription,
205+
invalidRequest.requestOptions.message,
206+
invalidRequest.requestOptions.extraOptions
207+
);
208+
});
209+
});
210+
});
211+
});

0 commit comments

Comments
 (0)