Skip to content

Commit

Permalink
chore: add tests for directory redirects for gateways
Browse files Browse the repository at this point in the history
Adds tests to ensure gateway and subdomain gateways return redirects
that are browser-cacheable.
  • Loading branch information
achingbrain committed Mar 6, 2024
1 parent 5b825e6 commit aa05fd7
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions packages/verified-fetch/test/verified-fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,60 @@ describe('@helia/verifed-fetch', () => {
expect(ipfsResponse.url).to.equal(`ipfs://${res.cid}/foo`)
})

it('should return a 301 with a trailing slash when a gateway directory is requested without a trailing slash', async () => {
const finalRootFileContent = new Uint8Array([0x01, 0x02, 0x03])

const fs = unixfs(helia)
const res = await last(fs.addAll([{
path: 'foo/index.html',
content: finalRootFileContent
}], {
wrapWithDirectory: true
}))

if (res == null) {
throw new Error('Import failed')
}

Check warning on line 176 in packages/verified-fetch/test/verified-fetch.spec.ts

View check run for this annotation

Codecov / codecov/patch

packages/verified-fetch/test/verified-fetch.spec.ts#L175-L176

Added lines #L175 - L176 were not covered by tests

const stat = await fs.stat(res.cid)
expect(stat.type).to.equal('directory')

const ipfsResponse = await verifiedFetch.fetch(`https://ipfs.local/ipfs/${res.cid}/foo`, {
redirect: 'manual'
})
expect(ipfsResponse).to.be.ok()
expect(ipfsResponse.status).to.equal(301)
expect(ipfsResponse.headers.get('location')).to.equal(`https://ipfs.local/ipfs/${res.cid}/foo/`)
expect(ipfsResponse.url).to.equal(`https://ipfs.local/ipfs/${res.cid}/foo`)
})

it('should return a 301 with a trailing slash when a subdomain gateway directory is requested without a trailing slash', async () => {
const finalRootFileContent = new Uint8Array([0x01, 0x02, 0x03])

const fs = unixfs(helia)
const res = await last(fs.addAll([{
path: 'foo/index.html',
content: finalRootFileContent
}], {
wrapWithDirectory: true
}))

if (res == null) {
throw new Error('Import failed')
}

Check warning on line 203 in packages/verified-fetch/test/verified-fetch.spec.ts

View check run for this annotation

Codecov / codecov/patch

packages/verified-fetch/test/verified-fetch.spec.ts#L202-L203

Added lines #L202 - L203 were not covered by tests

const stat = await fs.stat(res.cid)
expect(stat.type).to.equal('directory')

const ipfsResponse = await verifiedFetch.fetch(`https://${res.cid}.ipfs.local/foo`, {
redirect: 'manual'
})
expect(ipfsResponse).to.be.ok()
expect(ipfsResponse.status).to.equal(301)
expect(ipfsResponse.headers.get('location')).to.equal(`https://${res.cid}.ipfs.local/foo/`)
expect(ipfsResponse.url).to.equal(`https://${res.cid}.ipfs.local/foo`)
})

it('should simulate following a redirect to a path with a slash when a directory is requested without a trailing slash', async () => {
const finalRootFileContent = new Uint8Array([0x01, 0x02, 0x03])

Expand Down

0 comments on commit aa05fd7

Please sign in to comment.