-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcache-control-header.spec.ts
115 lines (96 loc) · 3.71 KB
/
cache-control-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
import { dagCbor } from '@helia/dag-cbor'
import { ipns } from '@helia/ipns'
import { stop } from '@libp2p/interface'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { dns } from '@multiformats/dns'
import { expect } from 'aegir/chai'
import Sinon from 'sinon'
import { VerifiedFetch } from '../src/verified-fetch.js'
import { createHelia } from './fixtures/create-offline-helia.js'
import { answerFake } from './fixtures/dns-answer-fake.js'
import type { Helia } from '@helia/interface'
import type { IPNS } from '@helia/ipns'
import type { DNSResponse } from '@multiformats/dns'
describe('cache-control header', () => {
let helia: Helia
let name: IPNS
let verifiedFetch: VerifiedFetch
let customDnsResolver: Sinon.SinonStub<any[], Promise<DNSResponse>>
beforeEach(async () => {
customDnsResolver = Sinon.stub()
helia = await createHelia({
dns: dns({
resolvers: {
'.': customDnsResolver
}
})
})
name = ipns(helia)
verifiedFetch = new VerifiedFetch({
helia
})
})
afterEach(async () => {
await stop(helia, verifiedFetch)
})
it('should allow return the correct max-age in the cache header for immutable responses', async () => {
const obj = {
hello: 'world'
}
const c = dagCbor(helia)
const cid = await c.add(obj)
const resp = await verifiedFetch.fetch(cid)
expect(resp).to.be.ok()
expect(resp.status).to.equal(200)
expect(resp.headers.get('Cache-Control')).to.equal('public, max-age=29030400, immutable')
})
it('should return not contain immutable in the cache-control header for an IPNS name', async () => {
const obj = {
hello: 'world'
}
const c = dagCbor(helia)
const cid = await c.add(obj)
const oneHourInMs = 1000 * 60 * 60
const peerId = await createEd25519PeerId()
// ipns currently only allows customising the lifetime which is also used as the TTL
await name.publish(peerId, cid, { lifetime: oneHourInMs })
const resp = await verifiedFetch.fetch(`ipns://${peerId}`)
expect(resp).to.be.ok()
expect(resp.status).to.equal(200)
expect(resp.headers.get('Cache-Control')).to.not.containIgnoreCase('immutable')
})
it('should return the correct max-age in the cache-control header for an IPNS name', async () => {
const obj = {
hello: 'world'
}
const c = dagCbor(helia)
const cid = await c.add(obj)
const oneHourInSeconds = 60 * 60
const peerId = await createEd25519PeerId()
/**
* ipns currently only allows customising the lifetime which is also used as the TTL
*
* lifetime is coming back as 100000 times larger than expected
*
* @see https://github.com/ipfs/js-ipns/blob/16e0e10682fa9a663e0bb493a44d3e99a5200944/src/index.ts#L200
* @see https://github.com/ipfs/js-ipns/pull/308
*/
await name.publish(peerId, cid, { lifetime: oneHourInSeconds * 1000 }) // pass to ipns as milliseconds
const resp = await verifiedFetch.fetch(`ipns://${peerId}`)
expect(resp).to.be.ok()
expect(resp.status).to.equal(200)
expect(resp.headers.get('Cache-Control')).to.equal(`public, max-age=${oneHourInSeconds}`)
})
it('should not contain immutable in the cache-control header for a DNSLink name', async () => {
const obj = {
hello: 'world'
}
const c = dagCbor(helia)
const cid = await c.add(obj)
customDnsResolver.withArgs('_dnslink.example-domain.com').resolves(answerFake(`dnslink=/ipfs/${cid}`, 666, '_dnslink.example-domain.com', 16))
const resp = await verifiedFetch.fetch('ipns://example-domain.com')
expect(resp).to.be.ok()
expect(resp.status).to.equal(200)
expect(resp.headers.get('Cache-Control')).to.equal('public, max-age=666')
})
})