-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconneg.test.js
More file actions
762 lines (672 loc) · 29.1 KB
/
conneg.test.js
File metadata and controls
762 lines (672 loc) · 29.1 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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/**
* 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');
});
it('should advertise N3 support in Accept-Put when conneg enabled', async () => {
const res = await request('/connegtest/public/alice.json');
const acceptPut = res.headers.get('Accept-Put');
assert.ok(acceptPut && acceptPut.includes('text/n3'),
'Accept-Put should include text/n3 (canAcceptInput accepts it under conneg)');
});
it('should advertise N3 support in Accept-Post for containers when conneg enabled', async () => {
const res = await request('/connegtest/public/');
const acceptPost = res.headers.get('Accept-Post');
assert.ok(acceptPost && acceptPost.includes('text/n3'),
'Accept-Post should include text/n3 (canAcceptInput accepts it under conneg)');
});
it('should advertise application/json in Accept-Put', async () => {
const res = await request('/connegtest/public/alice.json');
const acceptPut = res.headers.get('Accept-Put');
assert.ok(acceptPut && acceptPut.includes('application/json'),
'Accept-Put should include application/json (canAcceptInput treats it as a JSON-LD alias)');
});
it('should advertise application/json in Accept-Post for containers', async () => {
const res = await request('/connegtest/public/');
const acceptPost = res.headers.get('Accept-Post');
assert.ok(acceptPost && acceptPost.includes('application/json'),
'Accept-Post should include application/json (canAcceptInput treats it as a JSON-LD alias)');
});
});
// 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');
});
});
// ACL resources require a JSON-LD payload (application/ld+json or
// application/json) on PUT regardless of conneg setting: round-trip
// serialization between JSON-LD and Turtle has known limitations
// that can cause data loss. See #295.
describe('ACL content-type guard (#295)', () => {
const aclJsonLd = {
'@context': { acl: 'http://www.w3.org/ns/auth/acl#' },
'@graph': [
{
'@id': '#owner',
'@type': 'acl:Authorization',
'acl:agent': { '@id': '#me' },
'acl:accessTo': { '@id': './' },
'acl:mode': [{ '@id': 'acl:Read' }, { '@id': 'acl:Write' }, { '@id': 'acl:Control' }]
}
]
};
it('rejects text/turtle PUT to .acl with 415', async () => {
const turtle = `
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
<#owner> a acl:Authorization;
acl:mode acl:Read.
`;
const res = await request('/connegtest/public/turtle-reject.acl', {
method: 'PUT',
headers: { 'Content-Type': 'text/turtle' },
body: turtle,
auth: 'connegtest'
});
assertStatus(res, 415);
assertHeaderContains(res, 'Accept', 'application/ld+json');
assertHeaderContains(res, 'Accept', 'application/json');
assertHeaderContains(res, 'Accept-Put', 'application/ld+json');
assertHeaderContains(res, 'Accept-Put', 'application/json');
});
it('rejects text/n3 PUT to .acl with 415', async () => {
const res = await request('/connegtest/public/n3-reject.acl', {
method: 'PUT',
headers: { 'Content-Type': 'text/n3' },
body: '@prefix acl: <http://www.w3.org/ns/auth/acl#>. <#x> a acl:Authorization.',
auth: 'connegtest'
});
assertStatus(res, 415);
assertHeaderContains(res, 'Accept', 'application/ld+json');
assertHeaderContains(res, 'Accept', 'application/json');
assertHeaderContains(res, 'Accept-Put', 'application/ld+json');
assertHeaderContains(res, 'Accept-Put', 'application/json');
});
it('rejects text/plain PUT to .acl with 415 (URL-extension protection)', async () => {
const res = await request('/connegtest/public/plain-reject.acl', {
method: 'PUT',
headers: { 'Content-Type': 'text/plain' },
body: 'arbitrary text',
auth: 'connegtest'
});
assertStatus(res, 415);
assertHeaderContains(res, 'Accept', 'application/ld+json');
assertHeaderContains(res, 'Accept', 'application/json');
assertHeaderContains(res, 'Accept-Put', 'application/ld+json');
assertHeaderContains(res, 'Accept-Put', 'application/json');
});
it('rejects PUT to .acl with no Content-Type with 415', async () => {
// Use Uint8Array body so fetch() doesn't auto-set Content-Type
// (which it does for string bodies: text/plain;charset=UTF-8).
const res = await request('/connegtest/public/no-ct-reject.acl', {
method: 'PUT',
body: new Uint8Array([1, 2, 3, 4]),
auth: 'connegtest'
});
assertStatus(res, 415);
assertHeaderContains(res, 'Accept', 'application/ld+json');
assertHeaderContains(res, 'Accept', 'application/json');
assertHeaderContains(res, 'Accept-Put', 'application/ld+json');
assertHeaderContains(res, 'Accept-Put', 'application/json');
});
it('accepts application/ld+json PUT to .acl', async () => {
const res = await request('/connegtest/public/jsonld-accept.acl', {
method: 'PUT',
headers: { 'Content-Type': 'application/ld+json' },
body: JSON.stringify(aclJsonLd),
auth: 'connegtest'
});
assert.ok(res.status < 300, `JSON-LD PUT to .acl should succeed, got ${res.status}`);
});
it('accepts application/json PUT to .acl', async () => {
const res = await request('/connegtest/public/json-accept.acl', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(aclJsonLd),
auth: 'connegtest'
});
assert.ok(res.status < 300, `application/json PUT to .acl should succeed, got ${res.status}`);
});
});
});
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');
});
it('should advertise application/json in Accept-Put when conneg disabled', async () => {
const res = await request('/noconneg/public/');
const acceptPut = res.headers.get('Accept-Put');
assert.ok(acceptPut && acceptPut.includes('application/json'),
'Accept-Put should include application/json (canAcceptInput treats it as a JSON-LD alias)');
});
it('should advertise application/json in Accept-Post when conneg disabled', async () => {
const res = await request('/noconneg/public/');
const acceptPost = res.headers.get('Accept-Post');
assert.ok(acceptPost && acceptPost.includes('application/json'),
'Accept-Post should include application/json (canAcceptInput treats it as a JSON-LD alias)');
});
it('should not advertise text/n3 in Accept-Put when conneg disabled', async () => {
const res = await request('/noconneg/public/');
const acceptPut = res.headers.get('Accept-Put');
assert.ok(!acceptPut || !acceptPut.includes('text/n3'),
'Accept-Put should NOT include text/n3 when conneg disabled');
});
});
// The .acl content-type guard applies regardless of conneg setting (#295).
// The default deployment configuration is conneg disabled, so ensure the
// guard fires there too.
describe('ACL content-type guard (#295) — conneg disabled', () => {
const aclJsonLd = {
'@context': { acl: 'http://www.w3.org/ns/auth/acl#' },
'@graph': [
{
'@id': '#owner',
'@type': 'acl:Authorization',
'acl:agent': { '@id': '#me' },
'acl:accessTo': { '@id': './' },
'acl:mode': [{ '@id': 'acl:Read' }, { '@id': 'acl:Write' }, { '@id': 'acl:Control' }]
}
]
};
it('rejects text/turtle PUT to .acl with 415', async () => {
const turtle = `@prefix acl: <http://www.w3.org/ns/auth/acl#>. <#x> a acl:Authorization.`;
const res = await request('/noconneg/public/turtle-reject.acl', {
method: 'PUT',
headers: { 'Content-Type': 'text/turtle' },
body: turtle,
auth: 'noconneg'
});
assertStatus(res, 415);
assertHeaderContains(res, 'Accept', 'application/ld+json');
assertHeaderContains(res, 'Accept', 'application/json');
assertHeaderContains(res, 'Accept-Put', 'application/ld+json');
assertHeaderContains(res, 'Accept-Put', 'application/json');
});
it('rejects text/plain PUT to .acl with 415', async () => {
const res = await request('/noconneg/public/plain-reject.acl', {
method: 'PUT',
headers: { 'Content-Type': 'text/plain' },
body: 'arbitrary text',
auth: 'noconneg'
});
assertStatus(res, 415);
assertHeaderContains(res, 'Accept', 'application/ld+json');
assertHeaderContains(res, 'Accept', 'application/json');
assertHeaderContains(res, 'Accept-Put', 'application/ld+json');
assertHeaderContains(res, 'Accept-Put', 'application/json');
});
it('rejects PUT to .acl with no Content-Type with 415', async () => {
// Use Uint8Array body so fetch() doesn't auto-set Content-Type
// (which it does for string bodies: text/plain;charset=UTF-8).
const res = await request('/noconneg/public/no-ct-reject.acl', {
method: 'PUT',
body: new Uint8Array([1, 2, 3, 4]),
auth: 'noconneg'
});
assertStatus(res, 415);
assertHeaderContains(res, 'Accept', 'application/ld+json');
assertHeaderContains(res, 'Accept', 'application/json');
assertHeaderContains(res, 'Accept-Put', 'application/ld+json');
assertHeaderContains(res, 'Accept-Put', 'application/json');
});
it('accepts application/ld+json PUT to .acl', async () => {
const res = await request('/noconneg/public/jsonld-accept.acl', {
method: 'PUT',
headers: { 'Content-Type': 'application/ld+json' },
body: JSON.stringify(aclJsonLd),
auth: 'noconneg'
});
assert.ok(res.status < 300, `JSON-LD PUT to .acl should succeed, got ${res.status}`);
});
it('accepts application/json PUT to .acl', async () => {
const res = await request('/noconneg/public/json-accept.acl', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(aclJsonLd),
auth: 'noconneg'
});
assert.ok(res.status < 300, `application/json PUT to .acl should succeed, got ${res.status}`);
});
});
});
// 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));
});
});
describe('container with index.html — browser Accept (#409)', () => {
// Regression: a container that has an index.html with a *valid*
// <script type="application/ld+json"> data island used to return that
// data island as application/ld+json to plain browser GETs.
// selectContentType iterates the Accept list — for a browser sending
// `Accept: text/html, ..., */*;q=0.8` it sees text/html (and other
// HTML-ish types) but doesn't recognize any of them, then hits the
// `*/*` arm and returns JSON-LD, so the user-visible page silently
// flipped to JSON.
const HTML_WITH_JSONLD = '<!DOCTYPE html><html><head><title>Home</title>'
+ '<script type="application/ld+json">'
+ JSON.stringify({ '@context': { foaf: 'http://xmlns.com/foaf/0.1/' }, '@id': '#me', 'foaf:name': 'Carol' })
+ '</script></head><body><h1>hello</h1></body></html>';
before(async () => {
// Container with an index.html containing a parseable JSON-LD island.
await request('/qwtest/public/page/', { method: 'PUT', auth: 'qwtest' });
await request('/qwtest/public/page/index.html', {
method: 'PUT',
headers: { 'Content-Type': 'text/html' },
body: HTML_WITH_JSONLD,
auth: 'qwtest'
});
});
it('browser Accept (text/html with */*;q=0.8) → text/html, not JSON-LD', async () => {
const res = await request('/qwtest/public/page/', {
headers: { Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' }
});
assertStatus(res, 200);
assert.strictEqual(ct(res), 'text/html',
'browser GET on a container with index.html must return the HTML body, not the embedded data island');
const body = await res.text();
assert.ok(body.includes('<h1>hello</h1>'), 'response should be the index.html body');
});
it('plain Accept: text/html → text/html', async () => {
const res = await request('/qwtest/public/page/', { headers: { Accept: 'text/html' } });
assertStatus(res, 200);
assert.strictEqual(ct(res), 'text/html');
});
it('explicit Accept: application/ld+json → JSON-LD from data island still works', async () => {
const res = await request('/qwtest/public/page/', {
headers: { Accept: 'application/ld+json' }
});
assertStatus(res, 200);
assert.strictEqual(ct(res), 'application/ld+json');
const body = await res.json();
assert.strictEqual(body['foaf:name'], 'Carol',
'should still extract the data island when JSON-LD is explicitly asked for');
});
it('explicit Accept: text/turtle → Turtle from data island still works', async () => {
const res = await request('/qwtest/public/page/', { headers: { Accept: 'text/turtle' } });
assertStatus(res, 200);
assert.strictEqual(ct(res), 'text/turtle');
const body = await res.text();
assert.ok(body.includes('Carol'), 'turtle output should contain the data island content');
});
// The original bug was specifically GET vs HEAD divergence — the HEAD
// handler already had the explicitJson guard, GET didn't. Pin the
// parity here so any future drift between the two branches fails.
const parityCases = [
['browser Accept', { Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' }],
['Accept: text/html', { Accept: 'text/html' }],
['Accept: application/ld+json', { Accept: 'application/ld+json' }],
['Accept: text/turtle', { Accept: 'text/turtle' }]
];
for (const [label, headers] of parityCases) {
it(`HEAD === GET content-type — ${label}`, async () => {
const get = await request('/qwtest/public/page/', { headers });
const head = await request('/qwtest/public/page/', { 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}`);
});
}
});
});