Skip to content

Commit

Permalink
test: peerId urls
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Jan 19, 2024
1 parent ade40b2 commit 4cc599a
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions packages/verified-fetch/test/parse-url-string.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { type PeerId } from '@libp2p/interface'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { expect } from 'aegir/chai'
import { CID } from 'multiformats/cid'
import { stubInterface } from 'sinon-ts'
Expand Down Expand Up @@ -29,12 +31,16 @@ describe('parseUrlString', () => {
})

describe('ipns://<dnsLinkDomain> URLs', () => {
const ipns = stubInterface<IPNS>({
resolveDns: async (dnsLink: string) => {
expect(dnsLink).to.equal('mydomain.com')
return CID.parse('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr')
}
let ipns: IPNS
before(async () => {
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',
Expand All @@ -54,4 +60,37 @@ describe('parseUrlString', () => {
expect(result.path).to.equal('some/path/to/file.txt')
})
})

describe('ipns://<peerId> URLs', () => {
let ipns: IPNS
let testPeerId: PeerId
before(async () => {
testPeerId = await createEd25519PeerId()
ipns = stubInterface<IPNS>({
resolve: async (peerId: PeerId) => {
expect(peerId.toString()).to.equal(testPeerId.toString())
return CID.parse('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr')
}
})
})

it('can parse a URL with PeerId only', async () => {
const result = await parseUrlString({
urlString: `ipns://${testPeerId.toString()}`,
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 PeerId+path', async () => {
const result = await parseUrlString({
urlString: `ipns://${testPeerId.toString()}/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 4cc599a

Please sign in to comment.