Skip to content

Commit

Permalink
test: test than IPNS:// urls are handled properly
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Jan 19, 2024
1 parent 58a2c87 commit ade40b2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
19 changes: 11 additions & 8 deletions packages/verified-fetch/src/utils/parse-url-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,23 @@ export async function parseUrlString ({ urlString, ipns }: ParseUrlStringOptions
}

Check warning on line 38 in packages/verified-fetch/src/utils/parse-url-string.ts

View check run for this annotation

Codecov / codecov/patch

packages/verified-fetch/src/utils/parse-url-string.ts#L36-L38

Added lines #L36 - L38 were not covered by tests
} else if (protocol === 'ipns') {
if (cidOrPeerIdOrDnsLink.includes('.')) {
log.trace('Attempting to resolve DNSLink for %s', cidOrPeerIdOrDnsLink)
try {
cid = await ipns.resolveDns(cidOrPeerIdOrDnsLink)
log.trace('resolved %s to %c', cidOrPeerIdOrDnsLink, cid)
} catch (err) {
log.error(err)
throw new TypeError('Invalid DNSLink for ipns://<dnslink> URL')
}

Check warning on line 48 in packages/verified-fetch/src/utils/parse-url-string.ts

View check run for this annotation

Codecov / codecov/patch

packages/verified-fetch/src/utils/parse-url-string.ts#L46-L48

Added lines #L46 - L48 were not covered by tests
}

try {
const peerId = peerIdFromString(cidOrPeerIdOrDnsLink)
cid = await ipns.resolve(peerId)
} catch (err) {
log.error(err)
// ignore non PeerId
} else {
log.trace('Attempting to resolve PeerId for %s', cidOrPeerIdOrDnsLink)
try {
const peerId = peerIdFromString(cidOrPeerIdOrDnsLink)
cid = await ipns.resolve(peerId)
log.trace('resolved %s to %c', cidOrPeerIdOrDnsLink, cid)
} catch (err) {
log.error(err)
}
}

Check warning on line 58 in packages/verified-fetch/src/utils/parse-url-string.ts

View check run for this annotation

Codecov / codecov/patch

packages/verified-fetch/src/utils/parse-url-string.ts#L50-L58

Added lines #L50 - L58 were not covered by tests
} else {
throw new TypeError('Invalid protocol for URL. Please use ipfs:// or ipns:// URLs only.')
Expand Down
30 changes: 29 additions & 1 deletion packages/verified-fetch/test/parse-url-string.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { expect } from 'aegir/chai'
import { CID } from 'multiformats/cid'
import { stubInterface } from 'sinon-ts'
import { parseUrlString } from '../src/utils/parse-url-string.js'
import type { IPNS } from '@helia/ipns'

describe('parseUrlString', () => {
describe('ipfs:// URLs', () => {
describe('ipfs://<CID> URLs', () => {
it('can parse a URL with CID only', async () => {
const ipns = stubInterface<IPNS>({})
const result = await parseUrlString({
Expand All @@ -26,4 +27,31 @@ describe('parseUrlString', () => {
expect(result.path).to.equal('1 - Barrel - Part 1/1 - Barrel - Part 1 - alt.txt')
})
})

describe('ipns://<dnsLinkDomain> URLs', () => {
const ipns = stubInterface<IPNS>({
resolveDns: async (dnsLink: string) => {
expect(dnsLink).to.equal('mydomain.com')
return CID.parse('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr')
}
})
it('can parse a URL with DNSLinkDomain only', async () => {
const result = await parseUrlString({
urlString: 'ipns://mydomain.com',
ipns
})
expect(result.protocol).to.equal('ipns')
expect(result.cid.toString()).to.equal('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr')
expect(result.path).to.equal('')
})
it('can parse a URL with DNSLinkDomain+path', async () => {
const result = await parseUrlString({
urlString: 'ipns://mydomain.com/some/path/to/file.txt',
ipns
})
expect(result.protocol).to.equal('ipns')
expect(result.cid.toString()).to.equal('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr')
expect(result.path).to.equal('some/path/to/file.txt')
})
})
})

0 comments on commit ade40b2

Please sign in to comment.