light-sentry-sdk/tests/dsn.test.ts

58 lines
1.8 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { parseDSN, getEnvelopeUrl } from '../src/utils/dsn';
describe('dsn utils', () => {
describe('parseDSN', () => {
it('should parse valid DSN correctly', () => {
const dsn = parseDSN('https://proj_abc123@log.example.com/1001');
expect(dsn.protocol).toBe('https');
expect(dsn.publicKey).toBe('proj_abc123');
expect(dsn.host).toBe('log.example.com');
expect(dsn.projectId).toBe('1001');
});
it('should parse DSN with http protocol', () => {
const dsn = parseDSN('http://proj_abc123@log.example.com/1001');
expect(dsn.protocol).toBe('http');
});
it('should parse DSN with numeric public key', () => {
const dsn = parseDSN('https://123456@log.example.com/1001');
expect(dsn.publicKey).toBe('123456');
});
it('should throw error for invalid DSN', () => {
expect(() => parseDSN('invalid-dsn')).toThrow();
});
it('should throw error for DSN without project ID', () => {
expect(() => parseDSN('https://proj_abc123@log.example.com')).toThrow();
});
});
describe('getEnvelopeUrl', () => {
it('should return correct envelope URL', () => {
const dsn = {
protocol: 'https',
publicKey: 'proj_abc123',
host: 'log.example.com',
projectId: '1001',
};
const url = getEnvelopeUrl(dsn);
expect(url).toContain('https://log.example.com/api/1001/envelope/');
expect(url).toContain('sentry_key=proj_abc123');
});
it('should use http for non-https protocol', () => {
const dsn = {
protocol: 'http',
publicKey: 'proj_abc123',
host: 'log.example.com',
projectId: '1001',
};
const url = getEnvelopeUrl(dsn);
expect(url).toContain('http://log.example.com');
});
});
});