-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathaccept-header.spec.ts
331 lines (283 loc) · 9.17 KB
/
accept-header.spec.ts
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
import { dagCbor } from '@helia/dag-cbor'
import { dagJson } from '@helia/dag-json'
import { ipns } from '@helia/ipns'
import * as ipldDagCbor from '@ipld/dag-cbor'
import * as ipldDagJson from '@ipld/dag-json'
import { generateKeyPair } from '@libp2p/crypto/keys'
import { stop } from '@libp2p/interface'
import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { expect } from 'aegir/chai'
import * as cborg from 'cborg'
import { marshalIPNSRecord } from 'ipns'
import { CID } from 'multiformats/cid'
import * as raw from 'multiformats/codecs/raw'
import { sha256 } from 'multiformats/hashes/sha2'
import { VerifiedFetch } from '../src/verified-fetch.js'
import { createHelia } from './fixtures/create-offline-helia.js'
import type { Helia } from '@helia/interface'
interface Codec {
encode(obj: any): Uint8Array
decode(obj: Uint8Array): any
}
interface AcceptCborTestArgs {
obj: any
type: string
codec?: Codec
}
describe('accept header', () => {
let helia: Helia
let verifiedFetch: VerifiedFetch
function shouldNotAcceptCborWith ({ obj, type, codec = ipldDagCbor }: AcceptCborTestArgs): void {
it(`should return 406 Not Acceptable if CBOR ${type} field is encountered`, async () => {
const buf = codec.encode(obj)
const rawCid = CID.createV1(raw.code, await sha256.digest(buf))
await helia.blockstore.put(rawCid, buf)
const dagCborCid = CID.createV1(ipldDagCbor.code, rawCid.multihash)
const resp = await verifiedFetch.fetch(dagCborCid, {
headers: {
accept: 'application/json'
}
})
expect(resp.status).to.equal(406)
expect(resp.statusText).to.equal('Not Acceptable')
const resp2 = await verifiedFetch.fetch(dagCborCid, {
headers: {
accept: 'application/octet-stream'
}
})
expect(resp2.status).to.equal(200)
const out = codec.decode(new Uint8Array(await resp2.arrayBuffer()))
expect(out).to.deep.equal(obj, 'could not round-trip as application/octet-stream')
})
}
beforeEach(async () => {
helia = await createHelia()
verifiedFetch = new VerifiedFetch({
helia
})
})
afterEach(async () => {
await stop(helia, verifiedFetch)
})
it('should allow specifying application/vnd.ipld.raw accept header to skip data decoding', async () => {
// JSON-compliant CBOR - if decoded would otherwise cause `Content-Type` to
// be set to `application/json`
const obj = {
hello: 'world'
}
const c = dagCbor(helia)
const cid = await c.add(obj)
const resp = await verifiedFetch.fetch(cid, {
headers: {
accept: 'application/vnd.ipld.raw'
}
})
expect(resp.headers.get('content-type')).to.equal('application/vnd.ipld.raw')
const output = ipldDagCbor.decode(new Uint8Array(await resp.arrayBuffer()))
expect(output).to.deep.equal(obj)
})
it('should allow specifying application/octet-stream accept header to skip data decoding', async () => {
// JSON-compliant CBOR - if decoded would otherwise cause `Content-Type` to
// be set to `application/json`
const obj = {
hello: 'world'
}
const c = dagCbor(helia)
const cid = await c.add(obj)
const resp = await verifiedFetch.fetch(cid, {
headers: {
accept: 'application/octet-stream'
}
})
expect(resp.headers.get('content-type')).to.equal('application/octet-stream')
const output = ipldDagCbor.decode(new Uint8Array(await resp.arrayBuffer()))
expect(output).to.deep.equal(obj)
})
it('should transform DAG-CBOR to DAG-JSON', async () => {
const obj = {
hello: 'world'
}
const c = dagCbor(helia)
const cid = await c.add(obj)
const resp = await verifiedFetch.fetch(cid, {
headers: {
accept: 'application/vnd.ipld.dag-json'
}
})
expect(resp.headers.get('content-type')).to.equal('application/vnd.ipld.dag-json')
const output = ipldDagJson.decode(new Uint8Array(await resp.arrayBuffer()))
expect(output).to.deep.equal(obj)
})
it('should transform DAG-CBOR to JSON', async () => {
const obj = {
hello: 'world'
}
const c = dagCbor(helia)
const cid = await c.add(obj)
const resp = await verifiedFetch.fetch(cid, {
headers: {
accept: 'application/json'
}
})
expect(resp.headers.get('content-type')).to.equal('application/json')
const output = ipldDagJson.decode(new Uint8Array(await resp.arrayBuffer()))
expect(output).to.deep.equal(obj)
})
it('should transform DAG-JSON to DAG-CBOR', async () => {
const obj = {
hello: 'world'
}
const j = dagJson(helia)
const cid = await j.add(obj)
const resp = await verifiedFetch.fetch(cid, {
headers: {
accept: 'application/vnd.ipld.dag-cbor'
}
})
expect(resp.headers.get('content-type')).to.equal('application/vnd.ipld.dag-cbor')
const output = ipldDagCbor.decode(new Uint8Array(await resp.arrayBuffer()))
expect(output).to.deep.equal(obj)
})
it('should transform DAG-JSON to CBOR', async () => {
const obj = {
hello: 'world'
}
const j = dagJson(helia)
const cid = await j.add(obj)
const resp = await verifiedFetch.fetch(cid, {
headers: {
accept: 'application/cbor'
}
})
expect(resp.headers.get('content-type')).to.equal('application/cbor')
const output = ipldDagCbor.decode(new Uint8Array(await resp.arrayBuffer()))
expect(output).to.deep.equal(obj)
})
it('should return 406 Not Acceptable if the accept header cannot be adhered to', async () => {
const obj = {
hello: 'world'
}
const c = dagCbor(helia)
const cid = await c.add(obj)
const resp = await verifiedFetch.fetch(cid, {
headers: {
accept: 'application/what-even-is-this'
}
})
expect(resp.status).to.equal(406)
expect(resp.statusText).to.equal('Not Acceptable')
})
it('should support wildcards', async () => {
const obj = {
hello: 'world'
}
const c = dagCbor(helia)
const cid = await c.add(obj)
const resp = await verifiedFetch.fetch(cid, {
headers: {
accept: 'application/what-even-is-this, */*, application/vnd.ipld.raw'
}
})
expect(resp.status).to.equal(200)
expect(resp.headers.get('content-type')).to.equal('application/json')
})
it('should support type wildcards', async () => {
const obj = {
hello: 'world'
}
const c = dagCbor(helia)
const cid = await c.add(obj)
const resp = await verifiedFetch.fetch(cid, {
headers: {
accept: '*/json, application/vnd.ipld.raw'
}
})
expect(resp.status).to.equal(200)
expect(resp.headers.get('content-type')).to.equal('application/json')
})
it('should support subtype wildcards', async () => {
const obj = {
hello: 'world'
}
const c = dagCbor(helia)
const cid = await c.add(obj)
const resp = await verifiedFetch.fetch(cid, {
headers: {
accept: 'application/*, application/vnd.ipld.raw'
}
})
expect(resp.status).to.equal(200)
expect(resp.headers.get('content-type')).to.equal('application/json')
})
it('should support q-factor weighting', async () => {
const obj = {
hello: 'world'
}
const c = dagCbor(helia)
const cid = await c.add(obj)
const resp = await verifiedFetch.fetch(cid, {
headers: {
// these all match, application/json would be chosen as it is first but
// application/octet-stream has a higher weighting so it should win
accept: [
'application/json;q=0.1',
'application/application/vnd.ipld.raw;q=0.5',
'application/octet-stream;q=0.8'
].join(', ')
}
})
expect(resp.status).to.equal(200)
expect(resp.headers.get('content-type')).to.equal('application/octet-stream')
})
it('should support fetching IPNS records', async () => {
const key = await generateKeyPair('Ed25519')
const peerId = peerIdFromPrivateKey(key)
const obj = {
hello: 'world'
}
const c = dagCbor(helia)
const cid = await c.add(obj)
const i = ipns(helia)
const record = await i.publish(key, cid)
const resp = await verifiedFetch.fetch(`ipns://${peerId}`, {
headers: {
accept: 'application/vnd.ipfs.ipns-record'
}
})
expect(resp.status).to.equal(200)
expect(resp.headers.get('content-type')).to.equal('application/vnd.ipfs.ipns-record')
const buf = await resp.arrayBuffer()
expect(new Uint8Array(buf)).to.equalBytes(marshalIPNSRecord(record))
})
shouldNotAcceptCborWith({
obj: {
hello: 'world',
invalid: undefined
},
type: 'undefined',
// `undefined` is not supported by the IPLD data model so we have to encode
// using cborg and not @ipld/dag-cbor
codec: cborg
})
shouldNotAcceptCborWith({
obj: {
hello: 'world',
invalid: BigInt(Number.MAX_SAFE_INTEGER) + 10n
},
type: 'BigInt'
})
shouldNotAcceptCborWith({
obj: {
hello: 'world',
invalid: Uint8Array.from([0, 1, 2, 3])
},
type: 'Uint8Array'
})
shouldNotAcceptCborWith({
obj: {
hello: 'world',
invalid: CID.parse('QmbxpRxwKXxnJQjnPqm1kzDJSJ8YgkLxH23mcZURwPHjGv')
},
type: 'CID'
})
})