-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathurl.test.ts
More file actions
684 lines (651 loc) · 24.8 KB
/
url.test.ts
File metadata and controls
684 lines (651 loc) · 24.8 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
import { describe, expect, it } from 'vitest';
import {
getHttpSpanDetailsFromUrlObject,
getSanitizedUrlString,
getSanitizedUrlStringFromUrlObject,
isURLObjectRelative,
parseStringToURLObject,
parseUrl,
stripDataUrlContent,
stripUrlQueryAndFragment,
} from '../../../src/utils/url';
describe('stripQueryStringAndFragment', () => {
const urlString = 'http://dogs.are.great:1231/yay/';
const queryString = '?furry=yes&funny=very';
const fragment = '#adoptnotbuy';
it('strips query string from url', () => {
const urlWithQueryString = `${urlString}${queryString}`;
expect(stripUrlQueryAndFragment(urlWithQueryString)).toBe(urlString);
});
it('strips fragment from url', () => {
const urlWithFragment = `${urlString}${fragment}`;
expect(stripUrlQueryAndFragment(urlWithFragment)).toBe(urlString);
});
it('strips query string and fragment from url', () => {
const urlWithQueryStringAndFragment = `${urlString}${queryString}${fragment}`;
expect(stripUrlQueryAndFragment(urlWithQueryStringAndFragment)).toBe(urlString);
});
});
describe('getSanitizedUrlString', () => {
it.each([
['regular url', 'https://somedomain.com', 'https://somedomain.com'],
['regular url with a path', 'https://somedomain.com/path/to/happiness', 'https://somedomain.com/path/to/happiness'],
[
'url with standard http port 80',
'http://somedomain.com:80/path/to/happiness',
'http://somedomain.com/path/to/happiness',
],
[
'url with standard https port 443',
'https://somedomain.com:443/path/to/happiness',
'https://somedomain.com/path/to/happiness',
],
[
'url with non-standard port',
'https://somedomain.com:4200/path/to/happiness',
'https://somedomain.com:4200/path/to/happiness',
],
[
'url with query params',
'https://somedomain.com:4200/path/to/happiness?auhtToken=abc123¶m2=bar',
'https://somedomain.com:4200/path/to/happiness',
],
[
'url with a fragment',
'https://somedomain.com/path/to/happiness#somewildfragment123',
'https://somedomain.com/path/to/happiness',
],
[
'url with a fragment and query params',
'https://somedomain.com/path/to/happiness#somewildfragment123?auhtToken=abc123¶m2=bar',
'https://somedomain.com/path/to/happiness',
],
[
'url with authorization',
'https://username:password@somedomain.com',
'https://[filtered]:[filtered]@somedomain.com',
],
['same-origin url', '/api/v4/users?id=123', '/api/v4/users'],
['url with port 8080', 'http://172.31.12.144:8080/test', 'http://172.31.12.144:8080/test'],
['url with port 4433', 'http://172.31.12.144:4433/test', 'http://172.31.12.144:4433/test'],
['url with port 443', 'http://172.31.12.144:443/test', 'http://172.31.12.144/test'],
['url with IP and port 80', 'http://172.31.12.144:80/test', 'http://172.31.12.144/test'],
])('returns a sanitized URL for a %s', (_, rawUrl: string, sanitizedURL: string) => {
const urlObject = parseUrl(rawUrl);
expect(getSanitizedUrlString(urlObject)).toEqual(sanitizedURL);
});
});
describe('parseUrl', () => {
it.each([
[
'https://somedomain.com',
{ host: 'somedomain.com', path: '', search: '', hash: '', protocol: 'https', relative: '' },
],
[
'https://somedomain.com/path/to/happiness',
{
host: 'somedomain.com',
path: '/path/to/happiness',
search: '',
hash: '',
protocol: 'https',
relative: '/path/to/happiness',
},
],
[
'https://somedomain.com/path/to/happiness?auhtToken=abc123¶m2=bar',
{
host: 'somedomain.com',
path: '/path/to/happiness',
search: '?auhtToken=abc123¶m2=bar',
hash: '',
protocol: 'https',
relative: '/path/to/happiness?auhtToken=abc123¶m2=bar',
},
],
[
'https://somedomain.com/path/to/happiness?auhtToken=abc123¶m2=bar#wildfragment',
{
host: 'somedomain.com',
path: '/path/to/happiness',
search: '?auhtToken=abc123¶m2=bar',
hash: '#wildfragment',
protocol: 'https',
relative: '/path/to/happiness?auhtToken=abc123¶m2=bar#wildfragment',
},
],
[
'https://somedomain.com/path/to/happiness#somewildfragment123',
{
host: 'somedomain.com',
path: '/path/to/happiness',
search: '',
hash: '#somewildfragment123',
protocol: 'https',
relative: '/path/to/happiness#somewildfragment123',
},
],
[
'https://somedomain.com/path/to/happiness#somewildfragment123?auhtToken=abc123¶m2=bar',
{
host: 'somedomain.com',
path: '/path/to/happiness',
search: '',
hash: '#somewildfragment123?auhtToken=abc123¶m2=bar',
protocol: 'https',
relative: '/path/to/happiness#somewildfragment123?auhtToken=abc123¶m2=bar',
},
],
[
// yup, this is a valid URL (protocol-agnostic URL)
'//somedomain.com/path/to/happiness?auhtToken=abc123¶m2=bar#wildfragment',
{
host: 'somedomain.com',
path: '/path/to/happiness',
search: '?auhtToken=abc123¶m2=bar',
hash: '#wildfragment',
protocol: undefined,
relative: '/path/to/happiness?auhtToken=abc123¶m2=bar#wildfragment',
},
],
['', {}],
[
'\n',
{
hash: '',
host: undefined,
path: '\n',
protocol: undefined,
relative: '\n',
search: '',
},
],
[
'somerandomString',
{
hash: '',
host: undefined,
path: 'somerandomString',
protocol: undefined,
relative: 'somerandomString',
search: '',
},
],
[
'somedomain.com',
{
host: undefined,
path: 'somedomain.com',
search: '',
hash: '',
protocol: undefined,
relative: 'somedomain.com',
},
],
[
'somedomain.com/path/?q=1#fragment',
{
host: undefined,
path: 'somedomain.com/path/',
search: '?q=1',
hash: '#fragment',
protocol: undefined,
relative: 'somedomain.com/path/?q=1#fragment',
},
],
])('returns parsed partial URL object for %s', (url: string, expected: any) => {
expect(parseUrl(url)).toEqual(expected);
});
});
describe('parseStringToURLObject', () => {
it.each([
[
'invalid URL',
'invalid-url',
{
isRelative: true,
pathname: '/invalid-url',
search: '',
hash: '',
},
],
['valid absolute URL', 'https://somedomain.com', expect.any(URL)],
['valid absolute URL with base', 'https://somedomain.com', expect.any(URL), 'https://base.com'],
[
'relative URL',
'/path/to/happiness',
{
isRelative: true,
pathname: '/path/to/happiness',
search: '',
hash: '',
},
],
[
'relative URL with query',
'/path/to/happiness?q=1',
{
isRelative: true,
pathname: '/path/to/happiness',
search: '?q=1',
hash: '',
},
],
[
'relative URL with hash',
'/path/to/happiness#section',
{
isRelative: true,
pathname: '/path/to/happiness',
search: '',
hash: '#section',
},
],
[
'relative URL with query and hash',
'/path/to/happiness?q=1#section',
{
isRelative: true,
pathname: '/path/to/happiness',
search: '?q=1',
hash: '#section',
},
],
['URL with port', 'https://somedomain.com:8080/path', expect.any(URL)],
['URL with auth', 'https://user:pass@somedomain.com', expect.any(URL)],
['URL with special chars', 'https://somedomain.com/path/with spaces/and/special@chars', expect.any(URL)],
['URL with unicode', 'https://somedomain.com/path/with/unicode/测试', expect.any(URL)],
['URL with multiple query params', 'https://somedomain.com/path?q1=1&q2=2&q3=3', expect.any(URL)],
['URL with encoded chars', 'https://somedomain.com/path/%20%2F%3F%23', expect.any(URL)],
['URL with IPv4', 'https://192.168.1.1/path', expect.any(URL)],
['URL with IPv6', 'https://[2001:db8::1]/path', expect.any(URL)],
['URL with subdomain', 'https://sub.somedomain.com/path', expect.any(URL)],
['URL with multiple subdomains', 'https://sub1.sub2.somedomain.com/path', expect.any(URL)],
['URL with trailing slash', 'https://somedomain.com/path/', expect.any(URL)],
['URL with empty path', 'https://somedomain.com', expect.any(URL)],
['URL with root path', 'https://somedomain.com/', expect.any(URL)],
['URL with file extension', 'https://somedomain.com/path/file.html', expect.any(URL)],
['URL with custom protocol', 'custom://somedomain.com/path', expect.any(URL)],
['URL with query containing special chars', 'https://somedomain.com/path?q=hello+world&x=1/2', expect.any(URL)],
['URL with hash containing special chars', 'https://somedomain.com/path#section/1/2', expect.any(URL)],
[
'URL with all components',
'https://user:pass@sub.somedomain.com:8080/path/file.html?q=1#section',
expect.any(URL),
],
])('handles %s', (_, url: string, expected: any, base?: string) => {
expect(parseStringToURLObject(url, base)).toEqual(expected);
});
it('does not throw an error if URL.canParse is not defined', () => {
const canParse = (URL as any).canParse;
delete (URL as any).canParse;
expect(parseStringToURLObject('https://somedomain.com')).toBeInstanceOf(URL);
(URL as any).canParse = canParse;
});
});
describe('isURLObjectRelative', () => {
it('returns true for relative URLs', () => {
expect(isURLObjectRelative(parseStringToURLObject('/path/to/happiness')!)).toBe(true);
});
it('returns false for absolute URLs', () => {
expect(isURLObjectRelative(parseStringToURLObject('https://somedomain.com')!)).toBe(false);
});
});
describe('getSanitizedUrlStringFromUrlObject', () => {
it.each([
['regular url', 'https://somedomain.com', 'https://somedomain.com/'],
['regular url with a path', 'https://somedomain.com/path/to/happiness', 'https://somedomain.com/path/to/happiness'],
[
'url with standard http port 80',
'http://somedomain.com:80/path/to/happiness',
'http://somedomain.com/path/to/happiness',
],
[
'url with standard https port 443',
'https://somedomain.com:443/path/to/happiness',
'https://somedomain.com/path/to/happiness',
],
[
'url with non-standard port',
'https://somedomain.com:4200/path/to/happiness',
'https://somedomain.com:4200/path/to/happiness',
],
[
'url with query params',
'https://somedomain.com:4200/path/to/happiness?auhtToken=abc123¶m2=bar',
'https://somedomain.com:4200/path/to/happiness',
],
[
'url with a fragment',
'https://somedomain.com/path/to/happiness#somewildfragment123',
'https://somedomain.com/path/to/happiness',
],
[
'url with a fragment and query params',
'https://somedomain.com/path/to/happiness#somewildfragment123?auhtToken=abc123¶m2=bar',
'https://somedomain.com/path/to/happiness',
],
[
'url with authorization',
'https://username:password@somedomain.com',
'https://%filtered%:%filtered%@somedomain.com/',
],
['same-origin url', '/api/v4/users?id=123', '/api/v4/users'],
['url with port 8080', 'http://172.31.12.144:8080/test', 'http://172.31.12.144:8080/test'],
['url with port 4433', 'http://172.31.12.144:4433/test', 'http://172.31.12.144:4433/test'],
['url with port 443', 'http://172.31.12.144:443/test', 'http://172.31.12.144/test'],
['url with IP and port 80', 'http://172.31.12.144:80/test', 'http://172.31.12.144/test'],
['invalid URL', 'invalid-url', '/invalid-url'],
['valid absolute URL with base', 'https://somedomain.com', 'https://somedomain.com/'],
['relative URL', '/path/to/happiness', '/path/to/happiness'],
['relative URL with query', '/path/to/happiness?q=1', '/path/to/happiness'],
['relative URL with hash', '/path/to/happiness#section', '/path/to/happiness'],
['relative URL with query and hash', '/path/to/happiness?q=1#section', '/path/to/happiness'],
[
'URL with special chars',
'https://somedomain.com/path/with spaces/and/special@chars',
'https://somedomain.com/path/with%20spaces/and/special@chars',
],
[
'URL with unicode',
'https://somedomain.com/path/with/unicode/测试',
'https://somedomain.com/path/with/unicode/%E6%B5%8B%E8%AF%95',
],
['URL with multiple query params', 'https://somedomain.com/path?q1=1&q2=2&q3=3', 'https://somedomain.com/path'],
['URL with encoded chars', 'https://somedomain.com/path/%20%2F%3F%23', 'https://somedomain.com/path/%20%2F%3F%23'],
['URL with IPv4', 'https://192.168.1.1/path', 'https://192.168.1.1/path'],
['URL with IPv6', 'https://[2001:db8::1]/path', 'https://[2001:db8::1]/path'],
['URL with subdomain', 'https://sub.somedomain.com/path', 'https://sub.somedomain.com/path'],
['URL with multiple subdomains', 'https://sub1.sub2.somedomain.com/path', 'https://sub1.sub2.somedomain.com/path'],
['URL with trailing slash', 'https://somedomain.com/path/', 'https://somedomain.com/path/'],
['URL with empty path', 'https://somedomain.com', 'https://somedomain.com/'],
['URL with root path', 'https://somedomain.com/', 'https://somedomain.com/'],
['URL with file extension', 'https://somedomain.com/path/file.html', 'https://somedomain.com/path/file.html'],
['URL with custom protocol', 'custom://somedomain.com/path', 'custom://somedomain.com/path'],
[
'URL with query containing special chars',
'https://somedomain.com/path?q=hello+world&x=1/2',
'https://somedomain.com/path',
],
[
'URL with hash containing special chars',
'https://somedomain.com/path#section/1/2',
'https://somedomain.com/path',
],
[
'URL with all components',
'https://user:pass@sub.somedomain.com:8080/path/file.html?q=1#section',
'https://%filtered%:%filtered%@sub.somedomain.com:8080/path/file.html',
],
])('returns a sanitized URL for a %s', (_, rawUrl: string, sanitizedURL: string) => {
const urlObject = parseStringToURLObject(rawUrl);
if (!urlObject) {
throw new Error('Invalid URL');
}
expect(getSanitizedUrlStringFromUrlObject(urlObject)).toEqual(sanitizedURL);
});
});
describe('getHttpSpanDetailsFromUrlObject', () => {
it('handles undefined URL object', () => {
const [name, attributes] = getHttpSpanDetailsFromUrlObject(undefined, 'server', 'test-origin');
expect(name).toBe('GET /');
expect(attributes).toEqual({
'sentry.origin': 'test-origin',
'sentry.source': 'url',
});
});
it('handles relative URL object', () => {
const urlObject = parseStringToURLObject('/api/users')!;
const [name, attributes] = getHttpSpanDetailsFromUrlObject(urlObject, 'server', 'test-origin');
expect(name).toBe('GET /api/users');
expect(attributes).toEqual({
'sentry.origin': 'test-origin',
'sentry.source': 'url',
'url.path': '/api/users',
});
});
it('handles absolute URL object', () => {
const urlObject = parseStringToURLObject('https://example.com/api/users?q=test#section')!;
const [name, attributes] = getHttpSpanDetailsFromUrlObject(urlObject, 'server', 'test-origin');
expect(name).toBe('GET /api/users');
expect(attributes).toEqual({
'sentry.origin': 'test-origin',
'sentry.source': 'url',
'url.path': '/api/users',
'url.query': '?q=test',
'url.fragment': '#section',
'url.full': 'https://example.com/api/users?q=test#section',
'server.address': 'example.com',
'url.scheme': 'https:',
});
});
it('handles URL object with request method', () => {
const urlObject = parseStringToURLObject('https://example.com/api/users')!;
const [name, attributes] = getHttpSpanDetailsFromUrlObject(urlObject, 'server', 'test-origin', { method: 'POST' });
expect(name).toBe('POST /api/users');
expect(attributes).toEqual({
'sentry.origin': 'test-origin',
'sentry.source': 'url',
'url.path': '/api/users',
'url.full': 'https://example.com/api/users',
'server.address': 'example.com',
'url.scheme': 'https:',
'http.request.method': 'POST',
});
});
it('handles URL object with route name', () => {
const urlObject = parseStringToURLObject('https://example.com/api/users')!;
const [name, attributes] = getHttpSpanDetailsFromUrlObject(
urlObject,
'server',
'test-origin',
undefined,
'/api/users/:id',
);
expect(name).toBe('GET /api/users/:id');
expect(attributes).toEqual({
'sentry.origin': 'test-origin',
'sentry.source': 'route',
'url.path': '/api/users',
'url.full': 'https://example.com/api/users',
'server.address': 'example.com',
'url.scheme': 'https:',
'http.route': '/api/users/:id',
});
});
it('handles root path URL', () => {
const urlObject = parseStringToURLObject('https://example.com/')!;
const [name, attributes] = getHttpSpanDetailsFromUrlObject(urlObject, 'server', 'test-origin');
expect(name).toBe('GET /');
expect(attributes).toEqual({
'sentry.origin': 'test-origin',
'sentry.source': 'route',
'url.path': '/',
'url.full': 'https://example.com/',
'server.address': 'example.com',
'url.scheme': 'https:',
});
});
it('handles URL with port', () => {
const urlObject = parseStringToURLObject('https://example.com:8080/api/users')!;
const [name, attributes] = getHttpSpanDetailsFromUrlObject(urlObject, 'server', 'test-origin');
expect(name).toBe('GET /api/users');
expect(attributes).toEqual({
'sentry.origin': 'test-origin',
'sentry.source': 'url',
'url.path': '/api/users',
'url.full': 'https://example.com:8080/api/users',
'server.address': 'example.com',
'url.scheme': 'https:',
'url.port': '8080',
});
});
it('handles URL with non-standard port and request method', () => {
const urlObject = parseStringToURLObject('https://example.com:3000/api/users')!;
const [name, attributes] = getHttpSpanDetailsFromUrlObject(urlObject, 'server', 'test-origin', { method: 'PUT' });
expect(name).toBe('PUT /api/users');
expect(attributes).toEqual({
'sentry.origin': 'test-origin',
'sentry.source': 'url',
'url.path': '/api/users',
'url.full': 'https://example.com:3000/api/users',
'server.address': 'example.com',
'url.scheme': 'https:',
'url.port': '3000',
'http.request.method': 'PUT',
});
});
it('handles URL with route name and request method', () => {
const urlObject = parseStringToURLObject('https://example.com/api/users/123')!;
const [name, attributes] = getHttpSpanDetailsFromUrlObject(
urlObject,
'server',
'test-origin',
{ method: 'PATCH' },
'/api/users/:id',
);
expect(name).toBe('PATCH /api/users/:id');
expect(attributes).toEqual({
'sentry.origin': 'test-origin',
'sentry.source': 'route',
'url.path': '/api/users/123',
'url.full': 'https://example.com/api/users/123',
'server.address': 'example.com',
'url.scheme': 'https:',
'http.route': '/api/users/:id',
'http.request.method': 'PATCH',
});
});
it('handles URL with query params and route name', () => {
const urlObject = parseStringToURLObject('https://example.com/api/search?q=test&page=1')!;
const [name, attributes] = getHttpSpanDetailsFromUrlObject(
urlObject,
'server',
'test-origin',
undefined,
'/api/search',
);
expect(name).toBe('GET /api/search');
expect(attributes).toEqual({
'sentry.origin': 'test-origin',
'sentry.source': 'route',
'url.path': '/api/search',
'url.query': '?q=test&page=1',
'url.full': 'https://example.com/api/search?q=test&page=1',
'server.address': 'example.com',
'url.scheme': 'https:',
'http.route': '/api/search',
});
});
it('handles URL with fragment and route name', () => {
const urlObject = parseStringToURLObject('https://example.com/api/docs#section-1')!;
const [name, attributes] = getHttpSpanDetailsFromUrlObject(
urlObject,
'server',
'test-origin',
undefined,
'/api/docs',
);
expect(name).toBe('GET /api/docs');
expect(attributes).toEqual({
'sentry.origin': 'test-origin',
'sentry.source': 'route',
'url.path': '/api/docs',
'url.fragment': '#section-1',
'url.full': 'https://example.com/api/docs#section-1',
'server.address': 'example.com',
'url.scheme': 'https:',
'http.route': '/api/docs',
});
});
it('handles URL with auth credentials', () => {
const urlObject = parseStringToURLObject('https://user:pass@example.com/api/users')!;
const [name, attributes] = getHttpSpanDetailsFromUrlObject(urlObject, 'server', 'test-origin');
expect(name).toBe('GET /api/users');
expect(attributes).toEqual({
'sentry.origin': 'test-origin',
'sentry.source': 'url',
'url.path': '/api/users',
'url.full': 'https://user:pass@example.com/api/users',
'server.address': 'example.com',
'url.scheme': 'https:',
});
});
it('handles URL with IPv4 address', () => {
const urlObject = parseStringToURLObject('https://192.168.1.1:8080/api/users')!;
const [name, attributes] = getHttpSpanDetailsFromUrlObject(urlObject, 'server', 'test-origin');
expect(name).toBe('GET /api/users');
expect(attributes).toEqual({
'sentry.origin': 'test-origin',
'sentry.source': 'url',
'url.path': '/api/users',
'url.full': 'https://192.168.1.1:8080/api/users',
'server.address': '192.168.1.1',
'url.scheme': 'https:',
'url.port': '8080',
});
});
it('handles URL with IPv6 address', () => {
const urlObject = parseStringToURLObject('https://[2001:db8::1]:8080/api/users')!;
const [name, attributes] = getHttpSpanDetailsFromUrlObject(urlObject, 'server', 'test-origin');
expect(name).toBe('GET /api/users');
expect(attributes).toEqual({
'sentry.origin': 'test-origin',
'sentry.source': 'url',
'url.path': '/api/users',
'url.full': 'https://[2001:db8::1]:8080/api/users',
'server.address': '[2001:db8::1]',
'url.scheme': 'https:',
'url.port': '8080',
});
});
it('handles URL with subdomain', () => {
const urlObject = parseStringToURLObject('https://api.example.com/users')!;
const [name, attributes] = getHttpSpanDetailsFromUrlObject(urlObject, 'server', 'test-origin');
expect(name).toBe('GET /users');
expect(attributes).toEqual({
'sentry.origin': 'test-origin',
'sentry.source': 'url',
'url.path': '/users',
'url.full': 'https://api.example.com/users',
'server.address': 'api.example.com',
'url.scheme': 'https:',
});
});
});
describe('stripDataUrlContent', () => {
it('returns regular URLs unchanged', () => {
expect(stripDataUrlContent('https://example.com/api')).toBe('https://example.com/api');
expect(stripDataUrlContent('http://localhost:3000/test')).toBe('http://localhost:3000/test');
expect(stripDataUrlContent('/relative/path')).toBe('/relative/path');
});
it('strips content from base64 data URLs', () => {
expect(stripDataUrlContent('data:text/javascript;base64,SGVsbG8gV29ybGQ=')).toBe('<data:text/javascript,base64>');
expect(stripDataUrlContent('data:application/json;base64,eyJrZXkiOiJ2YWx1ZSJ9')).toBe(
'<data:application/json,base64>',
);
expect(stripDataUrlContent('data:text/html;base64,PGh0bWw+PC9odG1sPg==')).toBe('<data:text/html,base64>');
});
it('strips content from non-base64 data URLs', () => {
expect(stripDataUrlContent('data:text/plain,Hello%20World')).toBe('<data:text/plain>');
expect(stripDataUrlContent('data:text/html,<h1>Hello</h1>')).toBe('<data:text/html>');
});
it('handles data URLs with various MIME types', () => {
expect(stripDataUrlContent('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA')).toBe('<data:image/png,base64>');
expect(stripDataUrlContent('data:image/svg+xml;base64,PHN2Zz4=')).toBe('<data:image/svg+xml,base64>');
expect(stripDataUrlContent('data:application/octet-stream;base64,AQIDBA==')).toBe(
'<data:application/octet-stream,base64>',
);
});
it('defaults to text/plain for data URLs without MIME type', () => {
expect(stripDataUrlContent('data:,Hello')).toBe('<data:text/plain>');
expect(stripDataUrlContent('data:;base64,SGVsbG8=')).toBe('<data:text/plain,base64>');
});
it('handles empty data URLs', () => {
expect(stripDataUrlContent('data:')).toBe('<data:text/plain>');
});
it('handles very long base64 encoded data URLs', () => {
const longBase64 = 'A'.repeat(10000);
expect(stripDataUrlContent(`data:text/javascript;base64,${longBase64}`)).toBe('<data:text/javascript,base64>');
});
});