Commit 9bb50d6a authored by Moody Salem's avatar Moody Salem

unit tests for the uri to http method

parent b08bb7ea
import uriToHttp from './uriToHttp'
describe('uriToHttp', () => {
it('returns .eth.link for ens names', () => {
expect(uriToHttp('t2crtokens.eth')).toEqual(['https://t2crtokens.eth.link'])
})
it('returns https first for http', () => {
expect(uriToHttp('http://test.com')).toEqual(['https://test.com', 'http://test.com'])
})
it('returns https for https', () => {
expect(uriToHttp('https://test.com')).toEqual(['https://test.com'])
})
it('returns ipfs gateways for ipfs:// urls', () => {
expect(uriToHttp('ipfs://QmV8AfDE8GFSGQvt3vck8EwAzsPuNTmtP8VcQJE3qxRPaZ')).toEqual([
'https://cloudflare-ipfs.com/ipfs/QmV8AfDE8GFSGQvt3vck8EwAzsPuNTmtP8VcQJE3qxRPaZ/',
'https://ipfs.io/ipfs/QmV8AfDE8GFSGQvt3vck8EwAzsPuNTmtP8VcQJE3qxRPaZ/'
])
})
it('returns ipns gateways for ipns:// urls', () => {
expect(uriToHttp('ipns://app.uniswap.org')).toEqual([
'https://cloudflare-ipfs.com/ipns/app.uniswap.org/',
'https://ipfs.io/ipns/app.uniswap.org/'
])
})
it('returns empty array for invalid scheme', () => {
expect(uriToHttp('blah:test')).toEqual([])
})
})
...@@ -10,20 +10,18 @@ export default function uriToHttp(uri: string): string[] { ...@@ -10,20 +10,18 @@ export default function uriToHttp(uri: string): string[] {
} else if (parsed.protocol === 'https:') { } else if (parsed.protocol === 'https:') {
return [uri] return [uri]
} else if (parsed.protocol === 'ipfs:') { } else if (parsed.protocol === 'ipfs:') {
const hash = parsed.pathname.substring(2) const hash = parsed.href.match(/^ipfs:(\/\/)?(.*)$/)?.[2]
return [`https://cloudflare-ipfs.com/ipfs/${hash}/`, `https://ipfs.infura.io/ipfs/${hash}/`] return [`https://cloudflare-ipfs.com/ipfs/${hash}/`, `https://ipfs.io/ipfs/${hash}/`]
} else if (parsed.protocol === 'ipns:') { } else if (parsed.protocol === 'ipns:') {
const name = parsed.pathname.substring(2) const name = parsed.href.match(/^ipns:(\/\/)?(.*)$/)?.[2]
return [`https://cloudflare-ipfs.com/ipns/${name}/`, `https://ipfs.infura.io/ipns/${name}/`] return [`https://cloudflare-ipfs.com/ipns/${name}/`, `https://ipfs.io/ipns/${name}/`]
} else { } else {
console.error('Unrecognized protocol', parsed)
return [] return []
} }
} catch (error) { } catch (error) {
if (uri.toLowerCase().endsWith('.eth')) { if (uri.toLowerCase().endsWith('.eth')) {
return [`https://${uri.toLowerCase()}.link`] return [`https://${uri.toLowerCase()}.link`]
} }
console.error('Failed to parse URI', error)
return [] return []
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment