Last active
December 17, 2021 09:08
-
-
Save JonasBa/71d1cf01a221146c8c2d3ab449f162df to your computer and use it in GitHub Desktop.
URL sentry console test
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
function dsntoString(dsn, withPassword) { | |
const { host, port, path, pass, projectId, protocol, publicKey } = dsn; | |
return ( | |
`${protocol}//${publicKey}${withPassword && pass ? `:${pass}` : ''}` + | |
`@${host}${port ? `:${port}` : ''}${path}/${projectId}` | |
); | |
} | |
function dsnFromString(str) { | |
const url = new URL(str); | |
const pathComponents = url.pathname.split('/'); | |
const projectId = pathComponents.pop(); | |
return dsnFromComponents({ | |
host: url.hostname, | |
pass: url.password, | |
path: pathComponents.join('/'), | |
projectId: projectId || '', | |
port: url.port, | |
protocol: url.protocol, | |
publicKey: url.username, | |
}); | |
} | |
function dsnFromComponents(components) { | |
// TODO this is for backwards compatibility, and can be removed in a future version | |
if ('user' in components && !('publicKey' in components)) { | |
components.publicKey = components.user; | |
} | |
return { | |
user: components.publicKey || '', | |
protocol: components.protocol, | |
publicKey: components.publicKey || '', | |
pass: components.pass || '', | |
host: components.host, | |
port: components.port || '', | |
path: components.path || '', | |
projectId: components.projectId, | |
}; | |
} | |
function makeDsn(from) { | |
const components = typeof from === 'string' ? dsnFromString(from) : dsnFromComponents(from); | |
const dsn = { | |
...components, | |
toString: (withPassword) => dsntoString(dsn, withPassword), | |
}; | |
return dsn; | |
} | |
const expect = (a) => { | |
return { | |
toBe: (b) => { | |
if(a !== b) { | |
throw new Error(`${a} does not match ${b}`) | |
} | |
}, | |
toThrow: (cb) => { | |
let thrown = false; | |
try { | |
cb() | |
} catch(e) { | |
thrown = true; | |
} | |
if(!thrown) throw new Error(`${cb.toString()} did not throw`) | |
} | |
} | |
} | |
const test = (name, cb) => { | |
let result = false; | |
try { cb(); result = true; } catch(e) {} | |
console.log(name, result ? "✅" : "⛔️") | |
} | |
test('parses a valid full Dsn', () => { | |
const dsn = makeDsn('https://abc:xyz@sentry.io:1234/123'); | |
expect(dsn.protocol).toBe('https:'); | |
expect(dsn.publicKey).toBe('abc'); | |
expect(dsn.pass).toBe('xyz'); | |
expect(dsn.host).toBe('sentry.io'); | |
expect(dsn.port).toBe('1234'); | |
expect(dsn.path).toBe(''); | |
expect(dsn.projectId).toBe('123'); | |
}); | |
test('parses a valid partial Dsn', () => { | |
const dsn = makeDsn('https://abc@sentry.io/123/321'); | |
expect(dsn.protocol).toBe('https:'); | |
expect(dsn.publicKey).toBe('abc'); | |
expect(dsn.pass).toBe(''); | |
expect(dsn.host).toBe('sentry.io'); | |
expect(dsn.port).toBe(''); | |
expect(dsn.path).toBe('/123'); | |
expect(dsn.projectId).toBe('321'); | |
}); | |
test('with a long path', () => { | |
const dsn = makeDsn('https://abc@sentry.io/sentry/custom/installation/321'); | |
expect(dsn.protocol).toBe('https:'); | |
expect(dsn.publicKey).toBe('abc'); | |
expect(dsn.pass).toBe(''); | |
expect(dsn.host).toBe('sentry.io'); | |
expect(dsn.port).toBe(''); | |
expect(dsn.path).toBe('/sentry/custom/installation'); | |
expect(dsn.projectId).toBe('321'); | |
}); | |
test('with a query string', () => { | |
const dsn = makeDsn('https://abc@sentry.io/321?sample.rate=0.1&other=value'); | |
expect(dsn.protocol).toBe('https:'); | |
expect(dsn.publicKey).toBe('abc'); | |
expect(dsn.pass).toBe(''); | |
expect(dsn.host).toBe('sentry.io'); | |
expect(dsn.port).toBe(''); | |
expect(dsn.path).toBe(''); | |
expect(dsn.projectId).toBe('321'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment