@@ -6,15 +6,59 @@ import * as ipldDagJson from '@ipld/dag-json'
6
6
import { stop } from '@libp2p/interface'
7
7
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
8
8
import { expect } from 'aegir/chai'
9
+ import * as cborg from 'cborg'
9
10
import { marshal } from 'ipns'
11
+ import { CID } from 'multiformats/cid'
12
+ import * as raw from 'multiformats/codecs/raw'
13
+ import { sha256 } from 'multiformats/hashes/sha2'
10
14
import { VerifiedFetch } from '../src/verified-fetch.js'
11
15
import { createHelia } from './fixtures/create-offline-helia.js'
12
16
import type { Helia } from '@helia/interface'
13
17
18
+ interface Codec {
19
+ encode ( obj : any ) : Uint8Array
20
+ decode ( obj : Uint8Array ) : any
21
+ }
22
+
23
+ interface AcceptCborTestArgs {
24
+ obj : any
25
+ type : string
26
+ codec ?: Codec
27
+ }
28
+
14
29
describe ( 'accept header' , ( ) => {
15
30
let helia : Helia
16
31
let verifiedFetch : VerifiedFetch
17
32
33
+ function shouldNotAcceptCborWith ( { obj, type, codec = ipldDagCbor } : AcceptCborTestArgs ) : void {
34
+ it ( `should return 406 Not Acceptable if CBOR ${ type } field is encountered` , async ( ) => {
35
+ const buf = codec . encode ( obj )
36
+ const rawCid = CID . createV1 ( raw . code , await sha256 . digest ( buf ) )
37
+ await helia . blockstore . put ( rawCid , buf )
38
+ const dagCborCid = CID . createV1 ( ipldDagCbor . code , rawCid . multihash )
39
+
40
+ const resp = await verifiedFetch . fetch ( dagCborCid , {
41
+ headers : {
42
+ accept : 'application/json'
43
+ }
44
+ } )
45
+
46
+ expect ( resp . status ) . to . equal ( 406 )
47
+ expect ( resp . statusText ) . to . equal ( 'Not Acceptable' )
48
+
49
+ const resp2 = await verifiedFetch . fetch ( dagCborCid , {
50
+ headers : {
51
+ accept : 'application/octet-stream'
52
+ }
53
+ } )
54
+
55
+ expect ( resp2 . status ) . to . equal ( 200 )
56
+
57
+ const out = codec . decode ( new Uint8Array ( await resp2 . arrayBuffer ( ) ) )
58
+ expect ( out ) . to . deep . equal ( obj , 'could not round-trip as application/octet-stream' )
59
+ } )
60
+ }
61
+
18
62
beforeEach ( async ( ) => {
19
63
helia = await createHelia ( )
20
64
verifiedFetch = new VerifiedFetch ( {
@@ -246,4 +290,39 @@ describe('accept header', () => {
246
290
247
291
expect ( buf ) . to . equalBytes ( marshal ( record ) )
248
292
} )
293
+
294
+ shouldNotAcceptCborWith ( {
295
+ obj : {
296
+ hello : 'world' ,
297
+ invalid : undefined
298
+ } ,
299
+ type : 'undefined' ,
300
+ // `undefined` is not supported by the IPLD data model so we have to encode
301
+ // using cborg and not @ipld /dag-cbor
302
+ codec : cborg
303
+ } )
304
+
305
+ shouldNotAcceptCborWith ( {
306
+ obj : {
307
+ hello : 'world' ,
308
+ invalid : BigInt ( Number . MAX_SAFE_INTEGER ) + 10n
309
+ } ,
310
+ type : 'BigInt'
311
+ } )
312
+
313
+ shouldNotAcceptCborWith ( {
314
+ obj : {
315
+ hello : 'world' ,
316
+ invalid : Uint8Array . from ( [ 0 , 1 , 2 , 3 ] )
317
+ } ,
318
+ type : 'Uint8Array'
319
+ } )
320
+
321
+ shouldNotAcceptCborWith ( {
322
+ obj : {
323
+ hello : 'world' ,
324
+ invalid : CID . parse ( 'QmbxpRxwKXxnJQjnPqm1kzDJSJ8YgkLxH23mcZURwPHjGv' )
325
+ } ,
326
+ type : 'CID'
327
+ } )
249
328
} )
0 commit comments