/
dsn.test.ts
191 lines (175 loc) · 5.86 KB
/
dsn.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { Dsn } from '../src/dsn';
import { SentryError } from '../src/error';
describe('Dsn', () => {
describe('fromComponents', () => {
test('applies all components', () => {
const dsn = new Dsn({
host: 'sentry.io',
pass: 'xyz',
port: '1234',
projectId: '123',
protocol: 'https',
user: 'abc',
});
expect(dsn.protocol).toBe('https');
expect(dsn.user).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('applies partial components', () => {
const dsn = new Dsn({
host: 'sentry.io',
projectId: '123',
protocol: 'https',
user: 'abc',
});
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('');
expect(dsn.projectId).toBe('123');
});
test('throws for missing components', () => {
expect(
() =>
new Dsn({
host: '',
projectId: '123',
protocol: 'https',
user: 'abc',
}),
).toThrow(SentryError);
expect(
() =>
new Dsn({
host: 'sentry.io',
projectId: '',
protocol: 'https',
user: 'abc',
}),
).toThrow(SentryError);
expect(
() =>
new Dsn({
host: 'sentry.io',
projectId: '123',
protocol: '' as 'http', // Trick the type checker here
user: 'abc',
}),
).toThrow(SentryError);
expect(
() =>
new Dsn({
host: 'sentry.io',
projectId: '123',
protocol: 'https',
user: '',
}),
).toThrow(SentryError);
});
test('throws for invalid components', () => {
expect(
() =>
new Dsn({
host: 'sentry.io',
projectId: '123',
protocol: 'httpx' as 'http', // Trick the type checker here
user: 'abc',
}),
).toThrow(SentryError);
expect(
() =>
new Dsn({
host: 'sentry.io',
port: 'xxx',
projectId: '123',
protocol: 'https',
user: 'abc',
}),
).toThrow(SentryError);
});
});
describe('fromString', () => {
test('parses a valid full Dsn', () => {
const dsn = new Dsn('https://abc:xyz@sentry.io:1234/123');
expect(dsn.protocol).toBe('https');
expect(dsn.user).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 = new Dsn('https://abc@sentry.io/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://abc@sentry.io/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('with a query string', () => {
const dsn = new Dsn('https://abc@sentry.io/321?sample.rate=0.1&other=value');
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('');
expect(dsn.projectId).toBe('321');
});
test('throws when provided invalid Dsn', () => {
expect(() => new Dsn('some@random.dsn')).toThrow(SentryError);
});
test('throws without mandatory fields', () => {
expect(() => new Dsn('://abc@sentry.io/123')).toThrow(SentryError);
expect(() => new Dsn('https://@sentry.io/123')).toThrow(SentryError);
expect(() => new Dsn('https://abc@123')).toThrow(SentryError);
expect(() => new Dsn('https://abc@sentry.io/')).toThrow(SentryError);
});
test('throws for invalid fields', () => {
expect(() => new Dsn('httpx://abc@sentry.io/123')).toThrow(SentryError);
expect(() => new Dsn('httpx://abc@sentry.io:xxx/123')).toThrow(SentryError);
expect(() => new Dsn('http://abc@sentry.io/abc')).toThrow(SentryError);
});
});
describe('toString', () => {
test('excludes the password by default', () => {
const dsn = new Dsn('https://abc:xyz@sentry.io:1234/123');
expect(dsn.toString()).toBe('https://abc@sentry.io:1234/123');
});
test('optionally includes the password', () => {
const dsn = new Dsn('https://abc:xyz@sentry.io:1234/123');
expect(dsn.toString(true)).toBe('https://abc:xyz@sentry.io:1234/123');
});
test('renders no password if missing', () => {
const dsn = new Dsn('https://abc@sentry.io:1234/123');
expect(dsn.toString(true)).toBe('https://abc@sentry.io:1234/123');
});
test('renders no port if missing', () => {
const dsn = new Dsn('https://abc@sentry.io/123');
expect(dsn.toString()).toBe('https://abc@sentry.io/123');
});
test('renders the full path correctly', () => {
const dsn = new Dsn('https://abc@sentry.io/sentry/custom/installation/321');
expect(dsn.toString()).toBe('https://abc@sentry.io/sentry/custom/installation/321');
});
});
});