FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScriptSolidServer/test/notifications.test.js at patchAppend · bourgeoa/JavaScriptSolidServer · GitHub
bourgeoa
JavaScriptSolidServer
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScriptSolidServer
/
test
/
notifications.test.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
438 lines (372 loc) · 13.9 KB
Breadcrumbs
JavaScriptSolidServer
/
test
/
notifications.test.js
Copy path
File metadata and controls
438 lines (372 loc) · 13.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/**
* WebSocket Notifications Tests
*
* Tests the solid-0.1 WebSocket notification protocol.
*/
import
{
describe
,
it
,
before
,
after
}
from
'node:test'
;
import
assert
from
'node:assert'
;
import
{
WebSocket
}
from
'ws'
;
import
{
startTestServer
,
stopTestServer
,
request
,
createTestPod
,
getBaseUrl
,
assertStatus
,
assertHeader
,
assertHeaderContains
}
from
'./helpers.js'
;
describe
(
'WebSocket Notifications (notifications enabled)'
,
(
)
=>
{
let
wsUrl
;
before
(
async
(
)
=>
{
// Start server with notifications ENABLED
await
startTestServer
(
{
notifications
:
true
}
)
;
await
createTestPod
(
'notifytest'
)
;
// Get WebSocket URL from Updates-Via header
const
res
=
await
request
(
'/notifytest/'
,
{
method
:
'OPTIONS'
}
)
;
wsUrl
=
res
.
headers
.
get
(
'Updates-Via'
)
;
}
)
;
after
(
async
(
)
=>
{
await
stopTestServer
(
)
;
}
)
;
describe
(
'Discovery'
,
(
)
=>
{
it
(
'should return Updates-Via header in OPTIONS response'
,
async
(
)
=>
{
const
res
=
await
request
(
'/notifytest/public/'
,
{
method
:
'OPTIONS'
}
)
;
assertStatus
(
res
,
204
)
;
const
updatesVia
=
res
.
headers
.
get
(
'Updates-Via'
)
;
assert
.
ok
(
updatesVia
,
'Should have Updates-Via header'
)
;
assert
.
ok
(
updatesVia
.
startsWith
(
'ws://'
)
||
updatesVia
.
startsWith
(
'wss://'
)
,
'Updates-Via should be a WebSocket URL'
)
;
}
)
;
it
(
'should return Updates-Via header in GET response'
,
async
(
)
=>
{
const
res
=
await
request
(
'/notifytest/'
)
;
assertStatus
(
res
,
200
)
;
const
updatesVia
=
res
.
headers
.
get
(
'Updates-Via'
)
;
assert
.
ok
(
updatesVia
,
'GET response should have Updates-Via header'
)
;
}
)
;
it
(
'should expose Updates-Via in CORS headers'
,
async
(
)
=>
{
const
res
=
await
request
(
'/notifytest/'
,
{
headers
:
{
'Origin'
:
'http://example.org'
}
}
)
;
const
expose
=
res
.
headers
.
get
(
'Access-Control-Expose-Headers'
)
;
assert
.
ok
(
expose
&&
expose
.
includes
(
'Updates-Via'
)
,
'Updates-Via should be in exposed headers'
)
;
}
)
;
}
)
;
describe
(
'WebSocket Protocol'
,
(
)
=>
{
it
(
'should connect and receive protocol greeting'
,
async
(
)
=>
{
const
ws
=
new
WebSocket
(
wsUrl
)
;
const
message
=
await
new
Promise
(
(
resolve
,
reject
)
=>
{
ws
.
on
(
'open'
,
(
)
=>
{
}
)
;
ws
.
on
(
'message'
,
(
data
)
=>
resolve
(
data
.
toString
(
)
)
)
;
ws
.
on
(
'error'
,
reject
)
;
setTimeout
(
(
)
=>
reject
(
new
Error
(
'Timeout'
)
)
,
5000
)
;
}
)
;
assert
.
strictEqual
(
message
,
'protocol solid-0.1'
)
;
ws
.
close
(
)
;
}
)
;
it
(
'should acknowledge subscription'
,
async
(
)
=>
{
const
ws
=
new
WebSocket
(
wsUrl
)
;
const
baseUrl
=
getBaseUrl
(
)
;
const
resourceUrl
=
`
${
baseUrl
}
/notifytest/public/test.json`
;
const
messages
=
[
]
;
await
new
Promise
(
(
resolve
,
reject
)
=>
{
ws
.
on
(
'open'
,
(
)
=>
{
ws
.
send
(
`sub
${
resourceUrl
}
`
)
;
}
)
;
ws
.
on
(
'message'
,
(
data
)
=>
{
messages
.
push
(
data
.
toString
(
)
)
;
if
(
messages
.
length
>=
2
)
resolve
(
)
;
}
)
;
ws
.
on
(
'error'
,
reject
)
;
setTimeout
(
(
)
=>
resolve
(
)
,
2000
)
;
}
)
;
assert
.
ok
(
messages
.
includes
(
'protocol solid-0.1'
)
,
'Should receive protocol greeting'
)
;
assert
.
ok
(
messages
.
some
(
m
=>
m
.
startsWith
(
'ack '
)
)
,
'Should receive ack'
)
;
ws
.
close
(
)
;
}
)
;
}
)
;
describe
(
'Notifications'
,
(
)
=>
{
it
(
'should receive pub notification on PUT'
,
async
(
)
=>
{
const
ws
=
new
WebSocket
(
wsUrl
)
;
const
baseUrl
=
getBaseUrl
(
)
;
const
resourceUrl
=
`
${
baseUrl
}
/notifytest/public/notify-put.json`
;
const
notifications
=
[
]
;
await
new
Promise
(
(
resolve
)
=>
{
ws
.
on
(
'open'
,
(
)
=>
{
ws
.
send
(
`sub
${
resourceUrl
}
`
)
;
}
)
;
ws
.
on
(
'message'
,
(
data
)
=>
{
const
msg
=
data
.
toString
(
)
;
if
(
msg
.
startsWith
(
'pub '
)
)
{
notifications
.
push
(
msg
)
;
resolve
(
)
;
}
}
)
;
// Wait for subscription to be established
setTimeout
(
async
(
)
=>
{
// Create the resource
await
request
(
'/notifytest/public/notify-put.json'
,
{
method
:
'PUT'
,
headers
:
{
'Content-Type'
:
'application/ld+json'
}
,
body
:
JSON
.
stringify
(
{
'@id'
:
'#test'
,
'http://example.org/p'
:
'value'
}
)
,
auth
:
'notifytest'
}
)
;
}
,
500
)
;
setTimeout
(
(
)
=>
resolve
(
)
,
3000
)
;
}
)
;
assert
.
ok
(
notifications
.
length
>
0
,
'Should receive pub notification'
)
;
assert
.
ok
(
notifications
[
0
]
.
includes
(
resourceUrl
)
,
'Notification should include resource URL'
)
;
ws
.
close
(
)
;
}
)
;
it
(
'should receive pub notification on PATCH'
,
async
(
)
=>
{
const
baseUrl
=
getBaseUrl
(
)
;
const
resourceUrl
=
`
${
baseUrl
}
/notifytest/public/notify-patch2.json`
;
// First create the resource
await
request
(
'/notifytest/public/notify-patch2.json'
,
{
method
:
'PUT'
,
headers
:
{
'Content-Type'
:
'application/ld+json'
}
,
body
:
JSON
.
stringify
(
{
'@id'
:
'#test'
,
'http://example.org/name'
:
'Original'
}
)
,
auth
:
'notifytest'
}
)
;
// Create WebSocket AFTER the initial PUT to avoid race condition
const
ws
=
new
WebSocket
(
wsUrl
)
;
const
notifications
=
[
]
;
await
new
Promise
(
(
resolve
)
=>
{
ws
.
on
(
'open'
,
(
)
=>
{
ws
.
send
(
`sub
${
resourceUrl
}
`
)
;
}
)
;
ws
.
on
(
'message'
,
(
data
)
=>
{
const
msg
=
data
.
toString
(
)
;
if
(
msg
.
startsWith
(
'pub '
)
)
{
notifications
.
push
(
msg
)
;
resolve
(
)
;
}
}
)
;
// Wait for subscription to be established, then patch
setTimeout
(
async
(
)
=>
{
const
patch
=
`
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
_:patch a solid:InsertDeletePatch;
solid:inserts { <#test> <http://example.org/name> "Updated" }.
`
;
await
request
(
'/notifytest/public/notify-patch2.json'
,
{
method
:
'PATCH'
,
headers
:
{
'Content-Type'
:
'text/n3'
}
,
body
:
patch
,
auth
:
'notifytest'
}
)
;
}
,
500
)
;
setTimeout
(
(
)
=>
resolve
(
)
,
3000
)
;
}
)
;
assert
.
ok
(
notifications
.
length
>
0
,
'Should receive pub notification for PATCH'
)
;
ws
.
close
(
)
;
}
)
;
it
(
'should receive pub notification on DELETE'
,
async
(
)
=>
{
const
baseUrl
=
getBaseUrl
(
)
;
const
resourceUrl
=
`
${
baseUrl
}
/notifytest/public/notify-delete2.json`
;
// First create the resource
await
request
(
'/notifytest/public/notify-delete2.json'
,
{
method
:
'PUT'
,
headers
:
{
'Content-Type'
:
'application/ld+json'
}
,
body
:
JSON
.
stringify
(
{
'@id'
:
'#test'
}
)
,
auth
:
'notifytest'
}
)
;
// Create WebSocket AFTER the initial PUT to avoid race condition
const
ws
=
new
WebSocket
(
wsUrl
)
;
const
notifications
=
[
]
;
await
new
Promise
(
(
resolve
)
=>
{
ws
.
on
(
'open'
,
(
)
=>
{
ws
.
send
(
`sub
${
resourceUrl
}
`
)
;
}
)
;
ws
.
on
(
'message'
,
(
data
)
=>
{
const
msg
=
data
.
toString
(
)
;
if
(
msg
.
startsWith
(
'pub '
)
)
{
notifications
.
push
(
msg
)
;
resolve
(
)
;
}
}
)
;
// Wait for subscription, then delete
setTimeout
(
async
(
)
=>
{
await
request
(
'/notifytest/public/notify-delete2.json'
,
{
method
:
'DELETE'
,
auth
:
'notifytest'
}
)
;
}
,
500
)
;
setTimeout
(
(
)
=>
resolve
(
)
,
3000
)
;
}
)
;
assert
.
ok
(
notifications
.
length
>
0
,
'Should receive pub notification for DELETE'
)
;
ws
.
close
(
)
;
}
)
;
it
(
'should receive container notification when child changes'
,
async
(
)
=>
{
const
ws
=
new
WebSocket
(
wsUrl
)
;
const
baseUrl
=
getBaseUrl
(
)
;
const
containerUrl
=
`
${
baseUrl
}
/notifytest/public/`
;
const
notifications
=
[
]
;
await
new
Promise
(
(
resolve
)
=>
{
ws
.
on
(
'open'
,
(
)
=>
{
// Subscribe to container
ws
.
send
(
`sub
${
containerUrl
}
`
)
;
}
)
;
ws
.
on
(
'message'
,
(
data
)
=>
{
const
msg
=
data
.
toString
(
)
;
if
(
msg
.
startsWith
(
'pub '
)
)
{
notifications
.
push
(
msg
)
;
resolve
(
)
;
}
}
)
;
// Wait for subscription, then create a child resource
setTimeout
(
async
(
)
=>
{
await
request
(
'/notifytest/public/child-resource.json'
,
{
method
:
'PUT'
,
headers
:
{
'Content-Type'
:
'application/ld+json'
}
,
body
:
JSON
.
stringify
(
{
'@id'
:
'#child'
}
)
,
auth
:
'notifytest'
}
)
;
}
,
500
)
;
setTimeout
(
(
)
=>
resolve
(
)
,
3000
)
;
}
)
;
assert
.
ok
(
notifications
.
length
>
0
,
'Container should receive notification for child changes'
)
;
ws
.
close
(
)
;
}
)
;
}
)
;
describe
(
'Multiple Subscribers'
,
(
)
=>
{
it
(
'should notify all subscribers'
,
async
(
)
=>
{
const
ws1
=
new
WebSocket
(
wsUrl
)
;
const
ws2
=
new
WebSocket
(
wsUrl
)
;
const
baseUrl
=
getBaseUrl
(
)
;
const
resourceUrl
=
`
${
baseUrl
}
/notifytest/public/multi-sub.json`
;
const
notifications1
=
[
]
;
const
notifications2
=
[
]
;
await
new
Promise
(
(
resolve
)
=>
{
let
ready
=
0
;
const
setupWs
=
(
ws
,
notifications
)
=>
{
ws
.
on
(
'open'
,
(
)
=>
{
ws
.
send
(
`sub
${
resourceUrl
}
`
)
;
}
)
;
ws
.
on
(
'message'
,
(
data
)
=>
{
const
msg
=
data
.
toString
(
)
;
if
(
msg
.
startsWith
(
'ack '
)
)
{
ready
++
;
if
(
ready
===
2
)
{
// Both subscribed, trigger change
setTimeout
(
async
(
)
=>
{
await
request
(
'/notifytest/public/multi-sub.json'
,
{
method
:
'PUT'
,
headers
:
{
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
test
:
true
}
)
,
auth
:
'notifytest'
}
)
;
}
,
100
)
;
}
}
if
(
msg
.
startsWith
(
'pub '
)
)
{
notifications
.
push
(
msg
)
;
if
(
notifications1
.
length
>
0
&&
notifications2
.
length
>
0
)
{
resolve
(
)
;
}
}
}
)
;
}
;
setupWs
(
ws1
,
notifications1
)
;
setupWs
(
ws2
,
notifications2
)
;
setTimeout
(
(
)
=>
resolve
(
)
,
4000
)
;
}
)
;
assert
.
ok
(
notifications1
.
length
>
0
,
'First subscriber should receive notification'
)
;
assert
.
ok
(
notifications2
.
length
>
0
,
'Second subscriber should receive notification'
)
;
ws1
.
close
(
)
;
ws2
.
close
(
)
;
}
)
;
}
)
;
}
)
;
describe
(
'WebSocket ACL Enforcement'
,
(
)
=>
{
let
wsUrl
;
before
(
async
(
)
=>
{
await
startTestServer
(
{
notifications
:
true
}
)
;
await
createTestPod
(
'aclnotify'
)
;
const
res
=
await
request
(
'/aclnotify/'
,
{
method
:
'OPTIONS'
}
)
;
wsUrl
=
res
.
headers
.
get
(
'Updates-Via'
)
;
}
)
;
after
(
async
(
)
=>
{
await
stopTestServer
(
)
;
}
)
;
it
(
'should allow anonymous subscription to public resources'
,
async
(
)
=>
{
const
ws
=
new
WebSocket
(
wsUrl
)
;
const
baseUrl
=
getBaseUrl
(
)
;
const
resourceUrl
=
`
${
baseUrl
}
/aclnotify/public/anon-allowed.json`
;
const
messages
=
[
]
;
await
new
Promise
(
(
resolve
,
reject
)
=>
{
ws
.
on
(
'open'
,
(
)
=>
{
ws
.
send
(
`sub
${
resourceUrl
}
`
)
;
}
)
;
ws
.
on
(
'message'
,
(
data
)
=>
{
messages
.
push
(
data
.
toString
(
)
)
;
if
(
messages
.
length
>=
2
)
resolve
(
)
;
}
)
;
ws
.
on
(
'error'
,
reject
)
;
setTimeout
(
(
)
=>
resolve
(
)
,
2000
)
;
}
)
;
assert
.
ok
(
messages
.
includes
(
'protocol solid-0.1'
)
,
'Should receive protocol greeting'
)
;
assert
.
ok
(
messages
.
some
(
m
=>
m
===
`ack
${
resourceUrl
}
`
)
,
'Should receive ack for public resource'
)
;
ws
.
close
(
)
;
await
new
Promise
(
r
=>
setTimeout
(
r
,
50
)
)
;
// Allow WebSocket to fully close
}
)
;
it
(
'should deny anonymous subscription to private resources'
,
async
(
)
=>
{
const
ws
=
new
WebSocket
(
wsUrl
)
;
const
baseUrl
=
getBaseUrl
(
)
;
const
resourceUrl
=
`
${
baseUrl
}
/aclnotify/private/secret.json`
;
const
messages
=
[
]
;
await
new
Promise
(
(
resolve
,
reject
)
=>
{
ws
.
on
(
'open'
,
(
)
=>
{
ws
.
send
(
`sub
${
resourceUrl
}
`
)
;
}
)
;
ws
.
on
(
'message'
,
(
data
)
=>
{
messages
.
push
(
data
.
toString
(
)
)
;
if
(
messages
.
some
(
m
=>
m
.
startsWith
(
'err '
)
)
)
resolve
(
)
;
}
)
;
ws
.
on
(
'error'
,
reject
)
;
setTimeout
(
(
)
=>
resolve
(
)
,
2000
)
;
}
)
;
assert
.
ok
(
messages
.
includes
(
'protocol solid-0.1'
)
,
'Should receive protocol greeting'
)
;
assert
.
ok
(
messages
.
some
(
m
=>
m
===
`err
${
resourceUrl
}
forbidden`
)
,
`Should receive err forbidden for private resource. Got:
${
messages
.
join
(
', '
)
}
`
)
;
ws
.
close
(
)
;
await
new
Promise
(
r
=>
setTimeout
(
r
,
50
)
)
;
// Allow WebSocket to fully close
}
)
;
it
(
'should deny subscription to resources on other servers'
,
async
(
)
=>
{
const
ws
=
new
WebSocket
(
wsUrl
)
;
const
externalUrl
=
'https://evil.example.com/steal/data.json'
;
const
messages
=
[
]
;
await
new
Promise
(
(
resolve
,
reject
)
=>
{
ws
.
on
(
'open'
,
(
)
=>
{
ws
.
send
(
`sub
${
externalUrl
}
`
)
;
}
)
;
ws
.
on
(
'message'
,
(
data
)
=>
{
messages
.
push
(
data
.
toString
(
)
)
;
if
(
messages
.
some
(
m
=>
m
.
startsWith
(
'err '
)
)
)
resolve
(
)
;
}
)
;
ws
.
on
(
'error'
,
reject
)
;
setTimeout
(
(
)
=>
resolve
(
)
,
2000
)
;
}
)
;
assert
.
ok
(
messages
.
some
(
m
=>
m
===
`err
${
externalUrl
}
forbidden`
)
,
'Should deny subscription to external URLs'
)
;
ws
.
close
(
)
;
await
new
Promise
(
r
=>
setTimeout
(
r
,
50
)
)
;
// Allow WebSocket to fully close
}
)
;
}
)
;
describe
(
'WebSocket Notifications (notifications disabled - default)'
,
(
)
=>
{
before
(
async
(
)
=>
{
// Start server with notifications DISABLED (default)
await
startTestServer
(
{
notifications
:
false
}
)
;
await
createTestPod
(
'nonotify'
)
;
}
)
;
after
(
async
(
)
=>
{
await
stopTestServer
(
)
;
}
)
;
it
(
'should NOT return Updates-Via header when notifications disabled'
,
async
(
)
=>
{
const
res
=
await
request
(
'/nonotify/'
,
{
method
:
'OPTIONS'
}
)
;
assertStatus
(
res
,
204
)
;
const
updatesVia
=
res
.
headers
.
get
(
'Updates-Via'
)
;
assert
.
strictEqual
(
updatesVia
,
null
,
'Should NOT have Updates-Via header when disabled'
)
;
}
)
;
}
)
;
Back
|
FazBrowse Home
|
New Git URL