diff --git a/packages/verified-fetch/test/utils/parse-url-string.spec.ts b/packages/verified-fetch/test/utils/parse-url-string.spec.ts index c1aae30c..675ff331 100644 --- a/packages/verified-fetch/test/utils/parse-url-string.spec.ts +++ b/packages/verified-fetch/test/utils/parse-url-string.spec.ts @@ -90,49 +90,64 @@ describe('parseUrlString', () => { }) it('can parse a URL with CID only', async () => { - const result = await parseUrlString({ - urlString: 'ipfs://QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr', - ipns, - logger - }) - expect(result.protocol).to.equal('ipfs') - expect(result.cid.toString()).to.equal('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr') - expect(result.path).to.equal('') + await assertMatchUrl( + 'ipfs://QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr', { + protocol: 'ipfs', + cid: 'QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr', + path: '', + query: {} + } + ) }) it('can parse URL with CID+path', async () => { - const result = await parseUrlString({ - urlString: 'ipfs://QmdmQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7RgQm/1 - Barrel - Part 1/1 - Barrel - Part 1 - alt.txt', - ipns, - logger - }) - expect(result.protocol).to.equal('ipfs') - expect(result.cid.toString()).to.equal('QmdmQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7RgQm') - expect(result.path).to.equal('1 - Barrel - Part 1/1 - Barrel - Part 1 - alt.txt') + await assertMatchUrl( + 'ipfs://QmdmQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7RgQm/1 - Barrel - Part 1/1 - Barrel - Part 1 - alt.txt', { + protocol: 'ipfs', + cid: 'QmdmQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7RgQm', + path: '1 - Barrel - Part 1/1 - Barrel - Part 1 - alt.txt', + query: {} + } + ) }) it('can parse URL with CID+queryString', async () => { - const result = await parseUrlString({ - urlString: 'ipfs://QmdmQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7RgQm?format=car', - ipns, - logger - }) - expect(result.protocol).to.equal('ipfs') - expect(result.cid.toString()).to.equal('QmdmQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7RgQm') - expect(result.path).to.equal('') - expect(result.query).to.deep.equal({ format: 'car' }) + await assertMatchUrl( + 'ipfs://QmdmQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7RgQm?format=car', { + protocol: 'ipfs', + cid: 'QmdmQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7RgQm', + path: '', + query: { + format: 'car' + } + } + ) + }) + + it('can parse URL with CID, trailing slash and queryString', async () => { + await assertMatchUrl( + 'ipfs://QmdmQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7RgQm/?format=car', { + protocol: 'ipfs', + cid: 'QmdmQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7RgQm', + path: '', + query: { + format: 'car' + } + } + ) }) it('can parse URL with CID+path+queryString', async () => { - const result = await parseUrlString({ - urlString: 'ipfs://QmdmQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7RgQm/1 - Barrel - Part 1/1 - Barrel - Part 1 - alt.txt?format=tar', - ipns, - logger - }) - expect(result.protocol).to.equal('ipfs') - expect(result.cid.toString()).to.equal('QmdmQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7RgQm') - expect(result.path).to.equal('1 - Barrel - Part 1/1 - Barrel - Part 1 - alt.txt') - expect(result.query).to.deep.equal({ format: 'tar' }) + await assertMatchUrl( + 'ipfs://QmdmQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7RgQm/1 - Barrel - Part 1/1 - Barrel - Part 1 - alt.txt?format=tar', { + protocol: 'ipfs', + cid: 'QmdmQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7RgQm', + path: '1 - Barrel - Part 1/1 - Barrel - Part 1 - alt.txt', + query: { + format: 'tar' + } + } + ) }) }) @@ -154,14 +169,14 @@ describe('parseUrlString', () => { path: '' }) - const result = await parseUrlString({ - urlString: 'ipns://mydomain.com', - ipns, - logger - }) - expect(result.protocol).to.equal('ipns') - expect(result.cid.toString()).to.equal('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr') - expect(result.path).to.equal('') + await assertMatchUrl( + 'ipns://mydomain.com', { + protocol: 'ipns', + cid: 'QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr', + path: '', + query: {} + } + ) }) it('can parse a URL with DNSLinkDomain+path', async () => { @@ -170,14 +185,14 @@ describe('parseUrlString', () => { path: '' }) - const result = await parseUrlString({ - urlString: 'ipns://mydomain.com/some/path/to/file.txt', - ipns, - logger - }) - expect(result.protocol).to.equal('ipns') - expect(result.cid.toString()).to.equal('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr') - expect(result.path).to.equal('some/path/to/file.txt') + await assertMatchUrl( + 'ipns://mydomain.com/some/path/to/file.txt', { + protocol: 'ipns', + cid: 'QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr', + path: 'some/path/to/file.txt', + query: {} + } + ) }) it('can parse a URL with DNSLinkDomain+queryString', async () => { @@ -186,15 +201,16 @@ describe('parseUrlString', () => { path: '' }) - const result = await parseUrlString({ - urlString: 'ipns://mydomain.com?format=json', - ipns, - logger - }) - expect(result.protocol).to.equal('ipns') - expect(result.cid.toString()).to.equal('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr') - expect(result.path).to.equal('') - expect(result.query).to.deep.equal({ format: 'json' }) + await assertMatchUrl( + 'ipns://mydomain.com?format=json', { + protocol: 'ipns', + cid: 'QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr', + path: '', + query: { + format: 'json' + } + } + ) }) it('can parse a URL with DNSLinkDomain+path+queryString', async () => { @@ -203,15 +219,34 @@ describe('parseUrlString', () => { path: '' }) - const result = await parseUrlString({ - urlString: 'ipns://mydomain.com/some/path/to/file.txt?format=json', - ipns, - logger: defaultLogger() + await assertMatchUrl( + 'ipns://mydomain.com/some/path/to/file.txt?format=json', { + protocol: 'ipns', + cid: 'QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr', + path: 'some/path/to/file.txt', + query: { + format: 'json' + } + } + ) + }) + + it('can parse a URL with DNSLinkDomain+directoryPath+queryString', async () => { + ipns.resolveDns.withArgs('mydomain.com').resolves({ + cid: CID.parse('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr'), + path: '' }) - expect(result.protocol).to.equal('ipns') - expect(result.cid.toString()).to.equal('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr') - expect(result.path).to.equal('some/path/to/file.txt') - expect(result.query).to.deep.equal({ format: 'json' }) + + await assertMatchUrl( + 'ipns://mydomain.com/some/path/to/dir/?format=json', { + protocol: 'ipns', + cid: 'QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr', + path: 'some/path/to/dir/', + query: { + format: 'json' + } + } + ) }) }) @@ -399,14 +434,15 @@ describe('parseUrlString', () => { cid: CID.parse('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr'), path: '' }) - const result = await parseUrlString({ - urlString: `ipns://${testPeerId}`, - ipns, - logger - }) - expect(result.protocol).to.equal('ipns') - expect(result.cid.toString()).to.equal('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr') - expect(result.path).to.equal('') + + await assertMatchUrl( + `ipns://${testPeerId}`, { + protocol: 'ipns', + cid: 'QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr', + path: '', + query: {} + } + ) }) it('can parse a URL with PeerId+path', async () => { @@ -414,14 +450,15 @@ describe('parseUrlString', () => { cid: CID.parse('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr'), path: '' }) - const result = await parseUrlString({ - urlString: `ipns://${testPeerId}/some/path/to/file.txt`, - ipns, - logger - }) - expect(result.protocol).to.equal('ipns') - expect(result.cid.toString()).to.equal('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr') - expect(result.path).to.equal('some/path/to/file.txt') + + await assertMatchUrl( + `ipns://${testPeerId}/some/path/to/file.txt`, { + protocol: 'ipns', + cid: 'QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr', + path: 'some/path/to/file.txt', + query: {} + } + ) }) it('can parse a URL with PeerId+path with a trailing slash', async () => { @@ -429,14 +466,15 @@ describe('parseUrlString', () => { cid: CID.parse('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr'), path: '' }) - const result = await parseUrlString({ - urlString: `ipns://${testPeerId}/some/path/to/dir/`, - ipns, - logger - }) - expect(result.protocol).to.equal('ipns') - expect(result.cid.toString()).to.equal('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr') - expect(result.path).to.equal('some/path/to/dir/') + + await assertMatchUrl( + `ipns://${testPeerId}/some/path/to/dir/`, { + protocol: 'ipns', + cid: 'QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr', + path: 'some/path/to/dir/', + query: {} + } + ) }) it('can parse a URL with PeerId+queryString', async () => { @@ -444,15 +482,17 @@ describe('parseUrlString', () => { cid: CID.parse('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr'), path: '' }) - const result = await parseUrlString({ - urlString: `ipns://${testPeerId}?fomat=dag-cbor`, - ipns, - logger - }) - expect(result.protocol).to.equal('ipns') - expect(result.cid.toString()).to.equal('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr') - expect(result.path).to.equal('') - expect(result.query).to.deep.equal({ fomat: 'dag-cbor' }) + + await assertMatchUrl( + `ipns://${testPeerId}?format=dag-cbor`, { + protocol: 'ipns', + cid: 'QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr', + path: '', + query: { + format: 'dag-cbor' + } + } + ) }) it('can parse a URL with PeerId+path+queryString', async () => { @@ -460,15 +500,17 @@ describe('parseUrlString', () => { cid: CID.parse('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr'), path: '' }) - const result = await parseUrlString({ - urlString: `ipns://${testPeerId}/some/path/to/file.txt?fomat=dag-cbor`, - ipns, - logger - }) - expect(result.protocol).to.equal('ipns') - expect(result.cid.toString()).to.equal('QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr') - expect(result.path).to.equal('some/path/to/file.txt') - expect(result.query).to.deep.equal({ fomat: 'dag-cbor' }) + + await assertMatchUrl( + `ipns://${testPeerId}/some/path/to/file.txt?format=dag-cbor`, { + protocol: 'ipns', + cid: 'QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr', + path: 'some/path/to/file.txt', + query: { + format: 'dag-cbor' + } + } + ) }) it('should parse an ipns:// url with a path that resolves to a CID with a path', async () => { @@ -482,16 +524,14 @@ describe('parseUrlString', () => { path: recordPath }) - await expect(parseUrlString({ - urlString: `ipns://${peerId}/${requestPath}`, - ipns, - logger - })).to.eventually.deep.equal({ - protocol: 'ipns', - path: `${recordPath}/${requestPath}`, - cid, - query: {} - }) + await assertMatchUrl( + `ipns://${peerId}/${requestPath}`, { + protocol: 'ipns', + cid: 'QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr', + path: `${recordPath}/${requestPath}`, + query: {} + } + ) }) it('should parse an ipns:// url with a path that resolves to a CID with a path with a trailing slash', async () => { @@ -505,16 +545,14 @@ describe('parseUrlString', () => { path: recordPath }) - await expect(parseUrlString({ - urlString: `ipns://${peerId}/${requestPath}`, - ipns, - logger - })).to.eventually.deep.equal({ - protocol: 'ipns', - path: 'foo/bar/baz.txt', - cid, - query: {} - }) + await assertMatchUrl( + `ipns://${peerId}/${requestPath}`, { + protocol: 'ipns', + path: 'foo/bar/baz.txt', + cid: cid.toString(), + query: {} + } + ) }) it('should parse an ipns:// url with a path that resolves to a CID with a path with a trailing slash', async () => { @@ -528,16 +566,14 @@ describe('parseUrlString', () => { path: recordPath }) - await expect(parseUrlString({ - urlString: `ipns://${peerId}/${requestPath}`, - ipns, - logger - })).to.eventually.deep.equal({ - protocol: 'ipns', - path: 'foo/bar/baz/qux.txt', - cid, - query: {} - }) + await assertMatchUrl( + `ipns://${peerId}/${requestPath}`, { + protocol: 'ipns', + path: 'foo/bar/baz/qux.txt', + cid: cid.toString(), + query: {} + } + ) }) }) @@ -576,6 +612,17 @@ describe('parseUrlString', () => { ) }) + it('can parse an IPNS Path with PeerId+directoryPath', async () => { + await assertMatchUrl( + `/ipns/${peerId}/path/to/dir/`, { + protocol: 'ipns', + cid: cid.toString(), + path: 'path/to/dir/', + query: {} + } + ) + }) + it('can parse an IPNS Path with PeerId+queryString', async () => { await assertMatchUrl( `/ipns/${peerId}?format=car`, { @@ -601,6 +648,19 @@ describe('parseUrlString', () => { } ) }) + + it('can parse an IPNS Path with PeerId+directoryPath+queryString', async () => { + await assertMatchUrl( + `/ipns/${peerId}/path/to/dir/?format=tar`, { + protocol: 'ipns', + cid: cid.toString(), + path: 'path/to/dir/', + query: { + format: 'tar' + } + } + ) + }) }) HTTP_PROTOCOLS.forEach(proto => { @@ -639,6 +699,17 @@ describe('parseUrlString', () => { ) }) + it('can parse an IPNS Gateway URL with CID+directoryPath', async () => { + await assertMatchUrl( + `${proto}://example.com/ipns/${peerId}/path/to/dir/`, { + protocol: 'ipns', + cid: cid.toString(), + path: 'path/to/dir/', + query: {} + } + ) + }) + it('can parse an IPNS Gateway URL with CID+queryString', async () => { await assertMatchUrl( `${proto}://example.com/ipns/${peerId}?format=car`, { @@ -664,6 +735,19 @@ describe('parseUrlString', () => { } ) }) + + it('can parse an IPNS Gateway URL with CID+directoryPath+queryString', async () => { + await assertMatchUrl( + `${proto}://example.com/ipns/${peerId}/path/to/dir/?format=tar`, { + protocol: 'ipns', + cid: cid.toString(), + path: 'path/to/dir/', + query: { + format: 'tar' + } + } + ) + }) }) }) @@ -703,6 +787,17 @@ describe('parseUrlString', () => { ) }) + it('can parse a IPNS Subdomain Gateway URL with CID+directoryPath', async () => { + await assertMatchUrl( + `${proto}://${peerId}.ipns.example.com/path/to/dir/`, { + protocol: 'ipns', + cid: cid.toString(), + path: 'path/to/dir/', + query: {} + } + ) + }) + it('can parse a IPNS Subdomain Gateway URL with CID+queryString', async () => { await assertMatchUrl( `${proto}://${peerId}.ipns.example.com?format=car`, { @@ -728,6 +823,19 @@ describe('parseUrlString', () => { } ) }) + + it('can parse a IPNS Subdomain Gateway URL with CID+directoryPath+queryString', async () => { + await assertMatchUrl( + `${proto}://${peerId}.ipns.example.com/path/to/dir/?format=tar`, { + protocol: 'ipns', + cid: cid.toString(), + path: 'path/to/dir/', + query: { + format: 'tar' + } + } + ) + }) }) }) })