forked from JavaScriptSolidServer/JavaScriptSolidServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconneg.test.js
More file actions
448 lines (385 loc) · 15.2 KB
/
conneg.test.js
File metadata and controls
448 lines (385 loc) · 15.2 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
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
439
440
441
442
443
444
445
446
447
448
/**
* Content Negotiation Tests
*
* Tests Turtle <-> JSON-LD conversion with conneg enabled.
* Note: Content negotiation is OFF by default (JSON-LD native server).
*/
import { describe, it, before, after } from 'node:test';
import assert from 'node:assert';
import {
startTestServer,
stopTestServer,
request,
createTestPod,
assertStatus,
assertHeader,
assertHeaderContains
} from './helpers.js';
describe('Content Negotiation (conneg enabled)', () => {
before(async () => {
// Start server with conneg ENABLED
await startTestServer({ conneg: true });
await createTestPod('connegtest');
});
after(async () => {
await stopTestServer();
});
describe('GET with Accept header', () => {
it('should return JSON-LD when Accept: application/ld+json', async () => {
// Create a JSON-LD resource
const data = {
'@context': { 'foaf': 'http://xmlns.com/foaf/0.1/' },
'@id': '#me',
'foaf:name': 'Alice'
};
await request('/connegtest/public/alice.json', {
method: 'PUT',
headers: { 'Content-Type': 'application/ld+json' },
body: JSON.stringify(data),
auth: 'connegtest'
});
const res = await request('/connegtest/public/alice.json', {
headers: { 'Accept': 'application/ld+json' }
});
assertStatus(res, 200);
assertHeaderContains(res, 'Content-Type', 'application/ld+json');
const body = await res.json();
assert.strictEqual(body['foaf:name'], 'Alice');
});
it('should return Turtle when Accept: text/turtle', async () => {
// Create a JSON-LD resource
const data = {
'@context': { 'foaf': 'http://xmlns.com/foaf/0.1/' },
'@id': '#me',
'foaf:name': 'Bob'
};
await request('/connegtest/public/bob.json', {
method: 'PUT',
headers: { 'Content-Type': 'application/ld+json' },
body: JSON.stringify(data),
auth: 'connegtest'
});
const res = await request('/connegtest/public/bob.json', {
headers: { 'Accept': 'text/turtle' }
});
assertStatus(res, 200);
assertHeaderContains(res, 'Content-Type', 'text/turtle');
const turtle = await res.text();
// Should contain foaf prefix and name
assert.ok(turtle.includes('foaf:') || turtle.includes('http://xmlns.com/foaf/0.1/'),
'Turtle should contain foaf prefix or URI');
assert.ok(turtle.includes('Bob'), 'Turtle should contain the name');
});
it('should default to JSON-LD for */* Accept', async () => {
const res = await request('/connegtest/public/alice.json', {
headers: { 'Accept': '*/*' }
});
assertStatus(res, 200);
assertHeaderContains(res, 'Content-Type', 'application/ld+json');
});
it('should include Vary header with Accept', async () => {
const res = await request('/connegtest/public/alice.json');
const vary = res.headers.get('Vary');
assert.ok(vary && vary.includes('Accept'), 'Should have Vary: Accept');
});
});
describe('PUT with Content-Type', () => {
it('should accept Turtle input and store as JSON-LD', async () => {
const turtle = `
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
<#me> foaf:name "Charlie".
`;
const res = await request('/connegtest/public/charlie.json', {
method: 'PUT',
headers: { 'Content-Type': 'text/turtle' },
body: turtle,
auth: 'connegtest'
});
assertStatus(res, 201);
// Verify it's stored as JSON-LD
const getRes = await request('/connegtest/public/charlie.json', {
headers: { 'Accept': 'application/ld+json' }
});
assertStatus(getRes, 200);
const data = await getRes.json();
assert.ok(data['@context'], 'Should have @context');
});
it('should accept N3 input', async () => {
const n3 = `
@prefix schema: <http://schema.org/>.
<#item> schema:name "Widget".
`;
const res = await request('/connegtest/public/widget.json', {
method: 'PUT',
headers: { 'Content-Type': 'text/n3' },
body: n3,
auth: 'connegtest'
});
assertStatus(res, 201);
});
it('should return 400 for invalid Turtle', async () => {
const invalidTurtle = 'this is not valid turtle {{{';
const res = await request('/connegtest/public/invalid.json', {
method: 'PUT',
headers: { 'Content-Type': 'text/turtle' },
body: invalidTurtle,
auth: 'connegtest'
});
assertStatus(res, 400);
});
});
describe('POST with Content-Type', () => {
it('should accept Turtle input in POST', async () => {
const turtle = `
@prefix dc: <http://purl.org/dc/terms/>.
<#doc> dc:title "My Document".
`;
const res = await request('/connegtest/public/', {
method: 'POST',
headers: {
'Content-Type': 'text/turtle',
'Slug': 'turtle-doc.json'
},
body: turtle,
auth: 'connegtest'
});
assertStatus(res, 201);
const location = res.headers.get('Location');
assert.ok(location, 'Should have Location header');
});
});
describe('Accept-* Headers', () => {
it('should advertise Turtle support in Accept-Put', async () => {
const res = await request('/connegtest/public/alice.json');
const acceptPut = res.headers.get('Accept-Put');
assert.ok(acceptPut && acceptPut.includes('text/turtle'),
'Accept-Put should include text/turtle');
});
it('should advertise Turtle support in Accept-Post for containers', async () => {
const res = await request('/connegtest/public/');
const acceptPost = res.headers.get('Accept-Post');
assert.ok(acceptPost && acceptPost.includes('text/turtle'),
'Accept-Post should include text/turtle');
});
});
// Regression coverage for #294 — Solid convention dotfiles (.acl, .meta)
// were excluded from conneg because getContentType() returned
// application/octet-stream for them. Turtle-native clients (umai etc.)
// fetching <container>/.meta got JSON-LD back and errored on parse.
describe('Solid convention dotfiles (#294)', () => {
const metaData = {
'@context': { 'ldp': 'http://www.w3.org/ns/ldp#' },
'@id': '',
'@type': 'ldp:BasicContainer'
};
before(async () => {
// Write a JSON-LD .meta file (the format JSS writes internally).
await request('/connegtest/public/.meta', {
method: 'PUT',
headers: { 'Content-Type': 'application/ld+json' },
body: JSON.stringify(metaData),
auth: 'connegtest'
});
});
it('serves .meta as JSON-LD by default', async () => {
const res = await request('/connegtest/public/.meta', { auth: 'connegtest' });
assertStatus(res, 200);
assertHeaderContains(res, 'Content-Type', 'application/ld+json');
});
it('serves .meta as Turtle when Accept: text/turtle (the umai case)', async () => {
const res = await request('/connegtest/public/.meta', {
headers: { 'Accept': 'text/turtle' },
auth: 'connegtest'
});
assertStatus(res, 200);
assertHeaderContains(res, 'Content-Type', 'text/turtle');
const turtle = await res.text();
// First byte after the `@prefix` block must parse as Turtle,
// not '{' (the bug signature umai hit).
assert.ok(!turtle.trimStart().startsWith('{'),
`response looks like JSON, not Turtle: ${turtle.slice(0, 60)}`);
});
it('accepts Turtle PUT to .meta and round-trips to JSON-LD', async () => {
const turtle = `
@prefix ldp: <http://www.w3.org/ns/ldp#>.
<> a ldp:BasicContainer.
`;
const putRes = await request('/connegtest/public/.meta', {
method: 'PUT',
headers: { 'Content-Type': 'text/turtle' },
body: turtle,
auth: 'connegtest'
});
assert.ok(putRes.status < 300, `PUT turtle should succeed, got ${putRes.status}`);
// Default GET now serves the converted-and-stored JSON-LD.
const getRes = await request('/connegtest/public/.meta', {
headers: { 'Accept': 'application/ld+json' },
auth: 'connegtest'
});
assertStatus(getRes, 200);
assertHeaderContains(getRes, 'Content-Type', 'application/ld+json');
const body = await getRes.json();
assert.ok(body['@context'] || body['@graph'] || body['@type'] || body['@id'],
'round-tripped JSON-LD should have at least one @-keyword');
});
});
});
describe('Content Negotiation (conneg disabled - default)', () => {
before(async () => {
// Start server with conneg DISABLED (default)
await startTestServer({ conneg: false });
await createTestPod('noconneg');
});
after(async () => {
await stopTestServer();
});
describe('Default JSON-LD behavior', () => {
it('should always return JSON-LD regardless of Accept header', async () => {
// Create resource
const data = {
'@context': { 'foaf': 'http://xmlns.com/foaf/0.1/' },
'@id': '#me',
'foaf:name': 'DefaultUser'
};
await request('/noconneg/public/user.json', {
method: 'PUT',
headers: { 'Content-Type': 'application/ld+json' },
body: JSON.stringify(data),
auth: 'noconneg'
});
// Request Turtle
const res = await request('/noconneg/public/user.json', {
headers: { 'Accept': 'text/turtle' }
});
assertStatus(res, 200);
// Should still return JSON-LD when conneg disabled
const body = await res.json();
assert.strictEqual(body['foaf:name'], 'DefaultUser');
});
it('should accept JSON-LD input', async () => {
const data = { '@id': '#test', 'http://example.org/p': 'value' };
const res = await request('/noconneg/public/test.json', {
method: 'PUT',
headers: { 'Content-Type': 'application/ld+json' },
body: JSON.stringify(data),
auth: 'noconneg'
});
assertStatus(res, 201);
});
it('should accept plain JSON input', async () => {
const data = { foo: 'bar' };
const res = await request('/noconneg/public/plain.json', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
auth: 'noconneg'
});
assertStatus(res, 201);
});
it('should accept non-RDF content types', async () => {
const res = await request('/noconneg/public/readme.txt', {
method: 'PUT',
headers: { 'Content-Type': 'text/plain' },
body: 'Hello World',
auth: 'noconneg'
});
assertStatus(res, 201);
const getRes = await request('/noconneg/public/readme.txt');
assertStatus(getRes, 200);
const text = await getRes.text();
assert.strictEqual(text, 'Hello World');
});
it('should not advertise Turtle in Accept-Put when conneg disabled', async () => {
const res = await request('/noconneg/public/');
const acceptPut = res.headers.get('Accept-Put');
// Should only advertise JSON-LD, not Turtle
assert.ok(acceptPut && acceptPut.includes('application/ld+json'),
'Accept-Put should include application/ld+json');
assert.ok(!acceptPut || !acceptPut.includes('text/turtle'),
'Accept-Put should NOT include text/turtle when conneg disabled');
});
});
});
// Regression coverage for #325 — q-weighted Accept and HEAD/GET parity.
// Previously the conneg dispatcher used naive substring matching on the
// Accept header, so any Accept that mentioned text/turtle (even at q=0.1
// alongside q=1.0 application/ld+json) returned Turtle. Separately, HEAD
// on a container without an index.html hard-coded application/ld+json,
// so HEAD and GET disagreed on content-type for the same URL.
describe('Content Negotiation — q-weights and HEAD/GET parity (#325)', () => {
before(async () => {
await startTestServer({ conneg: true });
await createTestPod('qwtest');
});
after(async () => { await stopTestServer(); });
function ct(res) {
return (res.headers.get('content-type') || '').split(';')[0].trim();
}
describe('container — q-weight respected', () => {
it('Accept: jsonld q=1.0, turtle q=0.1 → JSON-LD', async () => {
const res = await request('/qwtest/', {
headers: { Accept: 'application/ld+json;q=1.0, text/turtle;q=0.1' }
});
assertStatus(res, 200);
assert.strictEqual(ct(res), 'application/ld+json');
const body = await res.text();
assert.ok(body.trimStart().startsWith('{'),
`body should be JSON, got: ${body.slice(0, 80)}`);
});
it('Accept: jsonld, turtle;q=0.5 → JSON-LD wins (downstream repro)', async () => {
const res = await request('/qwtest/', {
headers: { Accept: 'application/ld+json, text/turtle;q=0.5' }
});
assert.strictEqual(ct(res), 'application/ld+json');
const body = await res.text();
assert.ok(body.trimStart().startsWith('{'),
`body should be JSON, got: ${body.slice(0, 80)}`);
});
it('Accept: turtle (explicit) → Turtle', async () => {
const res = await request('/qwtest/', { headers: { Accept: 'text/turtle' } });
assert.strictEqual(ct(res), 'text/turtle');
const body = await res.text();
assert.ok(body.trimStart().startsWith('@prefix'),
`body should be Turtle, got: ${body.slice(0, 80)}`);
});
it('no Accept → JSON-LD (native default)', async () => {
const res = await request('/qwtest/');
assert.strictEqual(ct(res), 'application/ld+json');
});
});
describe('container — HEAD content-type matches GET', () => {
const cases = [
['no Accept', {}],
['jsonld preferred', { Accept: 'application/ld+json;q=1.0, text/turtle;q=0.1' }],
['turtle preferred', { Accept: 'text/turtle' }],
['mixed (q=0.5)', { Accept: 'application/ld+json, text/turtle;q=0.5' }]
];
for (const [label, headers] of cases) {
it(`HEAD === GET content-type — ${label}`, async () => {
const get = await request('/qwtest/', { headers });
const head = await request('/qwtest/', { method: 'HEAD', headers });
assert.strictEqual(get.status, 200);
assert.strictEqual(head.status, 200);
assert.strictEqual(ct(head), ct(get),
`HEAD ct (${ct(head)}) must equal GET ct (${ct(get)}) for ${label}`);
});
}
});
describe('container — auth path matches anonymous', () => {
it('GET with auth returns same content-type as without auth (turtle case)', async () => {
const headers = { Accept: 'text/turtle' };
const anon = await request('/qwtest/', { headers });
const authed = await request('/qwtest/', { headers, auth: 'qwtest' });
assert.strictEqual(ct(anon), 'text/turtle');
assert.strictEqual(ct(authed), ct(anon),
'authenticated GET must report the same content-type as anonymous');
});
it('GET with auth returns same content-type as without auth (jsonld case)', async () => {
const headers = { Accept: 'application/ld+json;q=1.0, text/turtle;q=0.1' };
const anon = await request('/qwtest/', { headers });
const authed = await request('/qwtest/', { headers, auth: 'qwtest' });
assert.strictEqual(ct(anon), 'application/ld+json');
assert.strictEqual(ct(authed), ct(anon));
});
});
});