Skip to content

fix: Fixes the dsn behaviour #1411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/browser/src/transports/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export abstract class BaseTransport implements Transport {

const protocol = dsn.protocol ? `${dsn.protocol}:` : '';
const port = dsn.port ? `:${dsn.port}` : '';
const endpoint = `${protocol}//${dsn.host}${port}/api/${dsn.path}/store/`;
const path = dsn.path ? `/${dsn.path}` : '';
const endpoint = `${protocol}//${dsn.host}${port}${path}/api/${
dsn.projectId
}/store/`;

// Auth is intentionally sent as part of query string (NOT as custom HTTP header)
// to avoid preflight CORS requests
Expand Down
28 changes: 21 additions & 7 deletions packages/core/src/dsn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ export class DSN implements DSNComponents {
public host!: string;
/** Port of the Sentry instance. */
public port!: string;
/** Project path */
/** Path */
public path!: string;
/** Project ID */
public projectId!: string;

/** Creates a new DSN component */
public constructor(from: DSNLike) {
Expand All @@ -41,10 +43,12 @@ export class DSN implements DSNComponents {
*/
public toString(withPassword: boolean = false): string {
// tslint:disable-next-line:no-this-assignment
const { host, path, pass, port, protocol, user } = this;
const { host, path, pass, port, projectId, protocol, user } = this;
return (
`${protocol}://${user}${withPassword && pass ? `:${pass}` : ''}` +
`@${host}${port ? `:${port}` : ''}/${path}`
`@${host}${port ? `:${port}` : ''}/${
path ? `${path}/` : path
}${projectId}`
);
}

Expand All @@ -55,8 +59,17 @@ export class DSN implements DSNComponents {
throw new SentryError('Invalid DSN');
}

const [protocol, user, pass = '', host, port = '', path] = match.slice(1);
Object.assign(this, { host, pass, path, port, protocol, user });
const [protocol, user, pass = '', host, port = '', lastPath] = match.slice(
1,
);
let path = '';
let projectId = lastPath;
const split = projectId.split('/');
if (split.length > 1) {
path = split.slice(0, -1).join('/');
projectId = split.pop() as string;
}
Object.assign(this, { host, pass, path, projectId, port, protocol, user });
}

/** Maps DSN components into this instance. */
Expand All @@ -66,12 +79,13 @@ export class DSN implements DSNComponents {
this.pass = components.pass || '';
this.host = components.host;
this.port = components.port || '';
this.path = components.path;
this.path = components.path || '';
this.projectId = components.projectId;
}

/** Validates this DSN and throws on error. */
private validate(): void {
for (const component of ['protocol', 'user', 'host', 'path']) {
for (const component of ['protocol', 'user', 'host', 'projectId']) {
if (!this[component as keyof DSNComponents]) {
throw new SentryError(`Invalid DSN: Missing ${component}`);
}
Expand Down
50 changes: 38 additions & 12 deletions packages/core/test/lib/dsn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ describe('DSN', () => {
const dsn = new DSN({
host: 'sentry.io',
pass: 'xyz',
path: '123',
port: '1234',
projectId: '123',
protocol: 'https',
user: 'abc',
});
Expand All @@ -17,13 +17,14 @@ describe('DSN', () => {
expect(dsn.pass).toBe('xyz');
expect(dsn.host).toBe('sentry.io');
expect(dsn.port).toBe('1234');
expect(dsn.path).toBe('123');
expect(dsn.projectId).toBe('123');
expect(dsn.path).toBe('');
});

test('applies partial components', () => {
const dsn = new DSN({
host: 'sentry.io',
path: '123',
projectId: '123',
protocol: 'https',
user: 'abc',
});
Expand All @@ -32,15 +33,16 @@ describe('DSN', () => {
expect(dsn.pass).toBe('');
expect(dsn.host).toBe('sentry.io');
expect(dsn.port).toBe('');
expect(dsn.path).toBe('123');
expect(dsn.projectId).toBe('123');
expect(dsn.path).toBe('');
});

test('throws for missing components', () => {
expect(
() =>
new DSN({
host: '',
path: '123',
projectId: '123',
protocol: 'https',
user: 'abc',
}),
Expand All @@ -49,7 +51,7 @@ describe('DSN', () => {
() =>
new DSN({
host: 'sentry.io',
path: '',
projectId: '',
protocol: 'https',
user: 'abc',
}),
Expand All @@ -58,7 +60,7 @@ describe('DSN', () => {
() =>
new DSN({
host: 'sentry.io',
path: '123',
projectId: '123',
protocol: '' as 'http', // Trick the type checker here
user: 'abc',
}),
Expand All @@ -67,7 +69,7 @@ describe('DSN', () => {
() =>
new DSN({
host: 'sentry.io',
path: '123',
projectId: '123',
protocol: 'https',
user: '',
}),
Expand All @@ -79,7 +81,7 @@ describe('DSN', () => {
() =>
new DSN({
host: 'sentry.io',
path: '123',
projectId: '123',
protocol: 'httpx' as 'http', // Trick the type checker here
user: 'abc',
}),
Expand All @@ -88,8 +90,8 @@ describe('DSN', () => {
() =>
new DSN({
host: 'sentry.io',
path: '123',
port: 'xxx',
projectId: '123',
protocol: 'https',
user: 'abc',
}),
Expand All @@ -105,17 +107,32 @@ describe('DSN', () => {
expect(dsn.pass).toBe('xyz');
expect(dsn.host).toBe('sentry.io');
expect(dsn.port).toBe('1234');
expect(dsn.path).toBe('123');
expect(dsn.projectId).toBe('123');
expect(dsn.path).toBe('');
});

test('parses a valid partial DSN', () => {
const dsn = new DSN('https://[email protected]/123');
const dsn = new DSN('https://[email protected]/123/321');
expect(dsn.protocol).toBe('https');
expect(dsn.user).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 = new DSN(
'https://[email protected]/sentry/custom/installation/321',
);
expect(dsn.protocol).toBe('https');
expect(dsn.user).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('throws when provided invalid DSN', () => {
Expand Down Expand Up @@ -157,5 +174,14 @@ describe('DSN', () => {
const dsn = new DSN('https://[email protected]/123');
expect(dsn.toString()).toBe('https://[email protected]/123');
});

test('renders the full path correctly', () => {
const dsn = new DSN(
'https://[email protected]/sentry/custom/installation/321',
);
expect(dsn.toString()).toBe(
'https://[email protected]/sentry/custom/installation/321',
);
});
});
});
6 changes: 4 additions & 2 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ export interface DSNComponents {
host: string;
/** Port of the Sentry instance. */
port?: string;
/** Project path */
path: string;
/** Sub path/ */
path?: string;
/** Project ID */
projectId: string;
}

/** Anything that can be parsed into a DSN. */
Expand Down