-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from filecoin-saturn/cid-detection
fix: detect cids in query parameters
- Loading branch information
Showing
4 changed files
with
130 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,87 @@ | ||
import assert from 'node:assert/strict' | ||
import { describe, it } from 'node:test' | ||
import { findCIDInURL, getCidPathFromURL } from '#src/utils.js' | ||
import { findCIDPathInURL } from '#src/utils.js' | ||
|
||
describe('controller', () => { | ||
it('should find cid in the subdomain', () => { | ||
it('finds the cid in the subdomain', () => { | ||
const cid = 'bafybeigt4657qnz5bi2pa7tdsbiobny55hkpt5vupgnueex22tzvwxfiym' | ||
const url = `https://${cid}.ipfs.dweb.link` | ||
|
||
const foundCid = findCIDInURL(url) | ||
assert.strictEqual(foundCid, cid) | ||
assert.strictEqual(findCIDPathInURL(url), cid) | ||
}) | ||
|
||
it('should find cid in the url path', () => { | ||
it('finds the cidPath in the subdomain', () => { | ||
const cid = 'bafybeidrf56yzbkocajbloyafrebrdzsam3uj35sce2fdyo4elb6zzoily' | ||
const path = 'test/cat.png' | ||
const cidPath = `${cid}/${path}` | ||
const url = `https://${cid}.ipfs.dweb.link/${path}` | ||
|
||
assert.strictEqual(findCIDPathInURL(url), cidPath) | ||
}) | ||
|
||
it('finds the cid in the url path', () => { | ||
const cid = 'QmS29VtmK7Ax6TMmMwbwqtuKSGRJTLJAmHMW83qGvBBxhV' | ||
const url = `https://ipfs.io/ipfs/${cid}` | ||
|
||
const foundCid = findCIDInURL(url) | ||
assert.strictEqual(foundCid, cid) | ||
assert.strictEqual(findCIDPathInURL(url), cid) | ||
}) | ||
|
||
it('should find cidPath in the subdomain', () => { | ||
const cid = 'bafybeigt4657qnz5bi2pa7tdsbiobny55hkpt5vupgnueex22tzvwxfiym' | ||
const path = 'hello/world.png' | ||
const cidPath = `${cid}/${path}` | ||
const url = `https://${cid}.ipfs.dweb.link/${path}` | ||
it('finds the cidPath in the url path', () => { | ||
const cidPath = 'QmS29VtmK7Ax6TMmMwbwqtuKSGRJTLJAmHMW83qGvBBxhV/cat.png' | ||
const url = `https://ipfs.io/ipfs/${cidPath}` | ||
|
||
const foundCidPath = getCidPathFromURL(url, cid) | ||
assert.strictEqual(foundCidPath, cidPath) | ||
assert.strictEqual(findCIDPathInURL(url), cidPath) | ||
}) | ||
|
||
it('should find cidPath in the url path', () => { | ||
const cid = 'QmS29VtmK7Ax6TMmMwbwqtuKSGRJTLJAmHMW83qGvBBxhV' | ||
const path = 'hello/world.png' | ||
it('finds the cid in an encoded query param', () => { | ||
const cid = 'bafybeidrf56yzbkocajbloyafrebrdzsam3uj35sce2fdyo4elb6zzoily' | ||
const url = `https://proxy.com/?url=ipfs.io%2Fipfs%2F${cid}/` | ||
|
||
assert.strictEqual(findCIDPathInURL(url), cid) | ||
}) | ||
|
||
it('finds the cidPath in an encoded query param', () => { | ||
const cidPath = 'bafybeidrf56yzbkocajbloyafrebrdzsam3uj35sce2fdyo4elb6zzoily/test/cat.png' | ||
const url = `https://proxy.com/?url=https%3A%2F%2Fipfs.io%2Fipfs%2F${cidPath}` | ||
|
||
assert.strictEqual(findCIDPathInURL(url), cidPath) | ||
}) | ||
|
||
it('finds the subdomain cid in an encoded query param', () => { | ||
const cid = 'bafybeidrf56yzbkocajbloyafrebrdzsam3uj35sce2fdyo4elb6zzoily' | ||
const param = `${cid}.ipfs.dweb.link` | ||
const url = `https://proxy.com/?url=${param}` | ||
|
||
assert.strictEqual(findCIDPathInURL(url), cid) | ||
}) | ||
|
||
it('finds the subdomain cidPath in an encoded query param', () => { | ||
const cid = 'bafybeidrf56yzbkocajbloyafrebrdzsam3uj35sce2fdyo4elb6zzoily' | ||
const path = 'dog/cow/cat.png' | ||
const cidPath = `${cid}/${path}` | ||
const url = `https://ipfs.io/ipfs/${cid}/${path}` | ||
const param = `https%3A%2F%2F${cid}.ipfs.dweb.link/${path}` | ||
const url = `https://proxy.com/?url=${param}` | ||
|
||
assert.strictEqual(findCIDPathInURL(url), cidPath) | ||
}) | ||
|
||
it('finds the plain cid (no /ipfs/ prefix) in an encoded query param', () => { | ||
const cid = 'bafybeidrf56yzbkocajbloyafrebrdzsam3uj35sce2fdyo4elb6zzoily' | ||
const url = `https://proxy.com/?cid=${cid}` | ||
|
||
assert.strictEqual(findCIDPathInURL(url), cid) | ||
}) | ||
|
||
it('finds the plain cidPath (no /ipfs/ prefix) in an encoded query param', () => { | ||
const cidPath = 'bafybeidrf56yzbkocajbloyafrebrdzsam3uj35sce2fdyo4elb6zzoily/test/cat.png' | ||
const url = `https://proxy.com/?cid=${cidPath}` | ||
|
||
assert.strictEqual(findCIDPathInURL(url), cidPath) | ||
}) | ||
|
||
it('returns null if cid not found', () => { | ||
const url = 'https://example.com/hello/world.png' | ||
|
||
const foundCidPath = getCidPathFromURL(url, cid) | ||
assert.strictEqual(foundCidPath, cidPath) | ||
assert.strictEqual(findCIDPathInURL(url), null) | ||
}) | ||
}) |