-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpatch.test.js
More file actions
300 lines (251 loc) · 8.79 KB
/
patch.test.js
File metadata and controls
300 lines (251 loc) · 8.79 KB
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
/**
* 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:[email protected]> }.
`;
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');
});
});
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);
});
});
});