Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add tests for directory redirects for gateways #15

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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
Loading