FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScriptSolidServer/test/patch.test.js at la3d/main · LA3D/JavaScriptSolidServer · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
LA3D
/
JavaScriptSolidServer
Public
forked from
JavaScriptSolidServer/JavaScriptSolidServer
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScriptSolidServer
/
test
/
patch.test.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
361 lines (303 loc) · 11.3 KB
Breadcrumbs
JavaScriptSolidServer
/
test
/
patch.test.js
Copy path
File metadata and controls
361 lines (303 loc) · 11.3 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
/**
* PATCH (N3 Patch) tests
*/
import
{
describe
,
it
,
before
,
after
}
from
'node:test'
;
import
assert
from
'node:assert'
;
import
{
startTestServer
,
stopTestServer
,
request
,
createTestPod
,
getBaseUrl
,
assertStatus
,
assertHeader
}
from
'./helpers.js'
;
describe
(
'PATCH Operations'
,
(
)
=>
{
before
(
async
(
)
=>
{
await
startTestServer
(
)
;
await
createTestPod
(
'patchtest'
)
;
}
)
;
after
(
async
(
)
=>
{
await
stopTestServer
(
)
;
}
)
;
describe
(
'N3 Patch'
,
(
)
=>
{
it
(
'should insert a triple into a JSON-LD resource'
,
async
(
)
=>
{
// Create initial resource
const
initial
=
{
'@context'
:
{
'foaf'
:
'http://xmlns.com/foaf/0.1/'
}
,
'@id'
:
'#me'
,
'foaf:name'
:
'Alice'
}
;
await
request
(
'/patchtest/public/patch-insert.json'
,
{
method
:
'PUT'
,
headers
:
{
'Content-Type'
:
'application/ld+json'
}
,
body
:
JSON
.
stringify
(
initial
)
,
auth
:
'patchtest'
}
)
;
// Apply N3 Patch to insert a new triple
const
patch
=
`
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
_:patch a solid:InsertDeletePatch;
solid:inserts { <#me> foaf:mbox <mailto:alice@example.org> }.
`
;
const
res
=
await
request
(
'/patchtest/public/patch-insert.json'
,
{
method
:
'PATCH'
,
headers
:
{
'Content-Type'
:
'text/n3'
}
,
body
:
patch
,
auth
:
'patchtest'
}
)
;
assertStatus
(
res
,
204
)
;
// Verify the change
const
verify
=
await
request
(
'/patchtest/public/patch-insert.json'
)
;
const
data
=
await
verify
.
json
(
)
;
assert
.
ok
(
data
[
'foaf:mbox'
]
,
'Should have new mbox property'
)
;
}
)
;
it
(
'should delete a triple from a JSON-LD resource'
,
async
(
)
=>
{
// Create initial resource with multiple properties
const
initial
=
{
'@context'
:
{
'foaf'
:
'http://xmlns.com/foaf/0.1/'
}
,
'@id'
:
'#me'
,
'foaf:name'
:
'Bob'
,
'foaf:age'
:
30
}
;
await
request
(
'/patchtest/public/patch-delete.json'
,
{
method
:
'PUT'
,
headers
:
{
'Content-Type'
:
'application/ld+json'
}
,
body
:
JSON
.
stringify
(
initial
)
,
auth
:
'patchtest'
}
)
;
// Apply N3 Patch to delete the age property
const
patch
=
`
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
_:patch a solid:InsertDeletePatch;
solid:deletes { <#me> foaf:age 30 }.
`
;
const
res
=
await
request
(
'/patchtest/public/patch-delete.json'
,
{
method
:
'PATCH'
,
headers
:
{
'Content-Type'
:
'text/n3'
}
,
body
:
patch
,
auth
:
'patchtest'
}
)
;
assertStatus
(
res
,
204
)
;
// Verify the change
const
verify
=
await
request
(
'/patchtest/public/patch-delete.json'
)
;
const
data
=
await
verify
.
json
(
)
;
assert
.
ok
(
!
data
[
'foaf:age'
]
,
'Should not have age property'
)
;
assert
.
strictEqual
(
data
[
'foaf:name'
]
,
'Bob'
,
'Should still have name'
)
;
}
)
;
it
(
'should insert and delete in same patch'
,
async
(
)
=>
{
// Create initial resource
const
initial
=
{
'@context'
:
{
'foaf'
:
'http://xmlns.com/foaf/0.1/'
}
,
'@id'
:
'#me'
,
'foaf:name'
:
'Charlie'
}
;
await
request
(
'/patchtest/public/patch-both.json'
,
{
method
:
'PUT'
,
headers
:
{
'Content-Type'
:
'application/ld+json'
}
,
body
:
JSON
.
stringify
(
initial
)
,
auth
:
'patchtest'
}
)
;
// Apply N3 Patch to change name
const
patch
=
`
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
_:patch a solid:InsertDeletePatch;
solid:deletes { <#me> foaf:name "Charlie" };
solid:inserts { <#me> foaf:name "Charles" }.
`
;
const
res
=
await
request
(
'/patchtest/public/patch-both.json'
,
{
method
:
'PATCH'
,
headers
:
{
'Content-Type'
:
'text/n3'
}
,
body
:
patch
,
auth
:
'patchtest'
}
)
;
assertStatus
(
res
,
204
)
;
// Verify the change
const
verify
=
await
request
(
'/patchtest/public/patch-both.json'
)
;
const
data
=
await
verify
.
json
(
)
;
assert
.
strictEqual
(
data
[
'foaf:name'
]
,
'Charles'
,
'Name should be updated'
)
;
}
)
;
it
(
'should add a new subject node'
,
async
(
)
=>
{
// Create initial resource with
@graph
const
initial
=
{
'@context'
:
{
'foaf'
:
'http://xmlns.com/foaf/0.1/'
}
,
'@graph'
:
[
{
'@id'
:
'#alice'
,
'foaf:name'
:
'Alice'
}
]
}
;
await
request
(
'/patchtest/public/patch-newnode.json'
,
{
method
:
'PUT'
,
headers
:
{
'Content-Type'
:
'application/ld+json'
}
,
body
:
JSON
.
stringify
(
initial
)
,
auth
:
'patchtest'
}
)
;
// Add a new person
const
patch
=
`
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
_:patch a solid:InsertDeletePatch;
solid:inserts { <#bob> foaf:name "Bob" }.
`
;
const
res
=
await
request
(
'/patchtest/public/patch-newnode.json'
,
{
method
:
'PATCH'
,
headers
:
{
'Content-Type'
:
'text/n3'
}
,
body
:
patch
,
auth
:
'patchtest'
}
)
;
assertStatus
(
res
,
204
)
;
// Verify the change
const
verify
=
await
request
(
'/patchtest/public/patch-newnode.json'
)
;
const
data
=
await
verify
.
json
(
)
;
assert
.
ok
(
data
[
'@graph'
]
,
'Should have @graph'
)
;
assert
.
strictEqual
(
data
[
'@graph'
]
.
length
,
2
,
'Should have 2 nodes'
)
;
}
)
;
it
(
'should handle semicolon shorthand and rdf:type "a" keyword'
,
async
(
)
=>
{
// Create initial resource with
@graph
const
initial
=
{
'@context'
:
{
'solid'
:
'http://www.w3.org/ns/solid/terms#'
}
,
'@graph'
:
[
]
}
;
await
request
(
'/patchtest/public/patch-semicolon.json'
,
{
method
:
'PUT'
,
headers
:
{
'Content-Type'
:
'application/ld+json'
}
,
body
:
JSON
.
stringify
(
initial
)
,
auth
:
'patchtest'
}
)
;
// Use semicolons and 'a' keyword (Turtle shorthand)
const
patch
=
`
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
@prefix wf: <http://www.w3.org/2005/01/wf/flow#>.
_:patch a solid:InsertDeletePatch;
solid:inserts {
<#reg1> a solid:TypeRegistration;
solid:forClass wf:Tracker;
solid:instance <https://example.com/todo/data.jsonld#this>.
}.
`
;
const
res
=
await
request
(
'/patchtest/public/patch-semicolon.json'
,
{
method
:
'PATCH'
,
headers
:
{
'Content-Type'
:
'text/n3'
}
,
body
:
patch
,
auth
:
'patchtest'
}
)
;
assertStatus
(
res
,
204
)
;
// Verify all three triples were inserted
const
verify
=
await
request
(
'/patchtest/public/patch-semicolon.json'
)
;
const
data
=
await
verify
.
json
(
)
;
const
node
=
data
[
'@graph'
]
.
find
(
n
=>
n
[
'@id'
]
&&
n
[
'@id'
]
.
includes
(
'#reg1'
)
)
;
assert
.
ok
(
node
,
'Should have the reg1 node'
)
;
// Check rdf:type value (from 'a' keyword)
const
rdfType
=
node
[
'rdf:type'
]
||
node
[
'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
]
;
assert
.
ok
(
rdfType
,
'Should have rdf:type (from "a" keyword)'
)
;
const
typeId
=
rdfType
[
'@id'
]
||
rdfType
;
assert
.
ok
(
String
(
typeId
)
.
includes
(
'TypeRegistration'
)
,
`rdf:type should be TypeRegistration, got
${
typeId
}
`
)
;
// Check solid:forClass value
const
forClass
=
node
[
'solid:forClass'
]
;
assert
.
ok
(
forClass
,
'Should have solid:forClass'
)
;
const
forClassId
=
forClass
[
'@id'
]
||
forClass
;
assert
.
ok
(
String
(
forClassId
)
.
includes
(
'Tracker'
)
,
`solid:forClass should be Tracker, got
${
forClassId
}
`
)
;
// Check solid:instance value (contains a dot in the IRI - tests IRI splitting)
const
instance
=
node
[
'solid:instance'
]
;
assert
.
ok
(
instance
,
'Should have solid:instance'
)
;
const
instanceId
=
instance
[
'@id'
]
||
instance
;
assert
.
strictEqual
(
instanceId
,
'https://example.com/todo/data.jsonld#this'
,
'solid:instance should have full IRI preserved'
)
;
}
)
;
}
)
;
describe
(
'PATCH Error Handling'
,
(
)
=>
{
it
(
'should return 415 for unsupported content type'
,
async
(
)
=>
{
// Create a resource first
await
request
(
'/patchtest/public/patch-error.json'
,
{
method
:
'PUT'
,
headers
:
{
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
test
:
true
}
)
,
auth
:
'patchtest'
}
)
;
const
res
=
await
request
(
'/patchtest/public/patch-error.json'
,
{
method
:
'PATCH'
,
headers
:
{
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
op
:
'add'
}
)
,
auth
:
'patchtest'
}
)
;
assertStatus
(
res
,
415
)
;
}
)
;
it
(
'should create resource if it does not exist'
,
async
(
)
=>
{
const
patch
=
`
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
_:patch a solid:InsertDeletePatch;
solid:inserts { <#me> <http://example.org/p> "test" }.
`
;
const
res
=
await
request
(
'/patchtest/public/patch-created.json'
,
{
method
:
'PATCH'
,
headers
:
{
'Content-Type'
:
'text/n3'
}
,
body
:
patch
,
auth
:
'patchtest'
}
)
;
// PATCH creates resources in Solid
assertStatus
(
res
,
201
)
;
// Verify resource was created with the inserted data
const
getRes
=
await
request
(
'/patchtest/public/patch-created.json'
)
;
assertStatus
(
getRes
,
200
)
;
}
)
;
it
(
'should return 409 when patching non-JSON-LD resource'
,
async
(
)
=>
{
// Create a plain text resource
await
request
(
'/patchtest/public/plain.txt'
,
{
method
:
'PUT'
,
headers
:
{
'Content-Type'
:
'text/plain'
}
,
body
:
'Hello World'
,
auth
:
'patchtest'
}
)
;
const
patch
=
`
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
_:patch a solid:InsertDeletePatch;
solid:inserts { <#me> <http://example.org/p> "test" }.
`
;
const
res
=
await
request
(
'/patchtest/public/plain.txt'
,
{
method
:
'PATCH'
,
headers
:
{
'Content-Type'
:
'text/n3'
}
,
body
:
patch
,
auth
:
'patchtest'
}
)
;
assertStatus
(
res
,
409
)
;
}
)
;
it
(
'should return 409 for PATCH to container'
,
async
(
)
=>
{
const
patch
=
`
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
_:patch a solid:InsertDeletePatch;
solid:inserts { <#me> <http://example.org/p> "test" }.
`
;
const
res
=
await
request
(
'/patchtest/public/'
,
{
method
:
'PATCH'
,
headers
:
{
'Content-Type'
:
'text/n3'
}
,
body
:
patch
,
auth
:
'patchtest'
}
)
;
assertStatus
(
res
,
409
)
;
}
)
;
it
(
'should require authentication for PATCH'
,
async
(
)
=>
{
// Create a resource first
await
request
(
'/patchtest/public/patch-auth.json'
,
{
method
:
'PUT'
,
headers
:
{
'Content-Type'
:
'application/json'
}
,
body
:
JSON
.
stringify
(
{
test
:
true
}
)
,
auth
:
'patchtest'
}
)
;
const
patch
=
`
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
_:patch a solid:InsertDeletePatch;
solid:inserts { <#me> <http://example.org/p> "test" }.
`
;
// Try without auth
const
res
=
await
request
(
'/patchtest/public/patch-auth.json'
,
{
method
:
'PATCH'
,
headers
:
{
'Content-Type'
:
'text/n3'
}
,
body
:
patch
// No auth
}
)
;
assertStatus
(
res
,
401
)
;
}
)
;
}
)
;
}
)
;
Back
|
FazBrowse Home
|
New Git URL