forked from getsentry/sentry-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaggage.test.ts
More file actions
152 lines (129 loc) · 6.22 KB
/
baggage.test.ts
File metadata and controls
152 lines (129 loc) · 6.22 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
import { describe, expect, it } from 'vitest';
import { mergeBaggageHeaders } from '../../src/utils/baggage';
describe('mergeBaggageHeaders', () => {
it('returns new baggage when existing is undefined', () => {
const result = mergeBaggageHeaders(undefined, 'sentry-environment=production');
expect(result).toBe('sentry-environment=production');
});
it('returns existing baggage when new baggage is invalid', () => {
const existing = 'custom-key=value';
const result = mergeBaggageHeaders(existing, '');
expect(result).toBe(existing);
});
it('handles empty existing baggage', () => {
const result = mergeBaggageHeaders('', 'foo=bar,sentry-release=1.0.0');
expect(result).toBe('foo=bar,sentry-release=1.0.0');
});
it('preserves existing non-Sentry entries', () => {
const result = mergeBaggageHeaders('foo=bar,other=vendor', 'foo=newvalue,third=party');
const entries = result?.split(',');
expect(entries).toContain('foo=bar');
expect(entries).toContain('other=vendor');
expect(entries).toContain('third=party');
expect(entries).not.toContain('foo=newvalue');
});
it('overwrites existing Sentry entries with new ones', () => {
const result = mergeBaggageHeaders(
'sentry-release=1.0.0,sentry-environment=prod',
'sentry-release=2.0.0,sentry-environment=staging',
);
const entries = result?.split(',');
expect(entries).toContain('sentry-release=2.0.0');
expect(entries).toContain('sentry-environment=staging');
expect(entries).not.toContain('sentry-release=1.0.0');
expect(entries).not.toContain('sentry-environment=prod');
});
it('merges Sentry and non-Sentry entries correctly', () => {
const result = mergeBaggageHeaders('foo=bar,sentry-release=1.0.0,other=vendor', 'sentry-release=2.0.0,third=party');
const entries = result?.split(',');
expect(entries).toContain('foo=bar');
expect(entries).toContain('other=vendor');
expect(entries).toContain('third=party');
expect(entries).toContain('sentry-release=2.0.0');
expect(entries).not.toContain('sentry-release=1.0.0');
});
it('handles third-party baggage with Sentry entries', () => {
const result = mergeBaggageHeaders(
'other=vendor,foo=bar,third=party,sentry-release=9.9.9,sentry-environment=staging,sentry-sample_rate=0.54,last=item',
'sentry-release=2.1.0,sentry-environment=myEnv',
);
const entries = result?.split(',');
expect(entries).toContain('foo=bar');
expect(entries).toContain('last=item');
expect(entries).toContain('other=vendor');
expect(entries).toContain('third=party');
expect(entries).toContain('sentry-environment=myEnv');
expect(entries).toContain('sentry-release=2.1.0');
expect(entries).toContain('sentry-sample_rate=0.54');
expect(entries).not.toContain('sentry-environment=staging');
expect(entries).not.toContain('sentry-release=9.9.9');
});
it('adds new Sentry entries when they do not exist', () => {
const result = mergeBaggageHeaders('foo=bar,other=vendor', 'sentry-release=1.0.0,sentry-environment=prod');
const entries = result?.split(',');
expect(entries).toContain('foo=bar');
expect(entries).toContain('other=vendor');
expect(entries).toContain('sentry-release=1.0.0');
expect(entries).toContain('sentry-environment=prod');
});
it('handles array-type existing baggage', () => {
const result = mergeBaggageHeaders(['foo=bar', 'other=vendor'], 'sentry-release=1.0.0');
const entries = (result as string)?.split(',');
expect(entries).toContain('foo=bar');
expect(entries).toContain('other=vendor');
expect(entries).toContain('sentry-release=1.0.0');
});
it('preserves order of existing entries', () => {
const result = mergeBaggageHeaders('first=1,second=2,third=3', 'fourth=4');
expect(result).toBe('first=1,second=2,third=3,fourth=4');
});
it('handles complex scenario with multiple Sentry keys', () => {
const result = mergeBaggageHeaders(
'foo=bar,sentry-release=old,sentry-environment=old,other=vendor',
'sentry-release=new,sentry-environment=new,sentry-transaction=test,new=entry',
);
const entries = result?.split(',');
expect(entries).toContain('foo=bar');
expect(entries).toContain('other=vendor');
expect(entries).toContain('sentry-release=new');
expect(entries).toContain('sentry-environment=new');
expect(entries).toContain('sentry-transaction=test');
expect(entries).toContain('new=entry');
expect(entries).not.toContain('sentry-release=old');
expect(entries).not.toContain('sentry-environment=old');
});
it('overwrites existing Sentry entries with new SDK values', () => {
const result = mergeBaggageHeaders(
'sentry-trace_id=abc123,sentry-sampled=false,non-sentry=keep',
'sentry-trace_id=xyz789,sentry-sampled=true',
);
const entries = result?.split(',');
expect(entries).toContain('sentry-trace_id=xyz789');
expect(entries).toContain('sentry-sampled=true');
expect(entries).toContain('non-sentry=keep');
expect(entries).not.toContain('sentry-trace_id=abc123');
expect(entries).not.toContain('sentry-sampled=false');
});
it('merges non-conflicting baggage entries', () => {
const existing = 'custom-key=value';
const newBaggage = 'sentry-environment=production';
const result = mergeBaggageHeaders(existing, newBaggage);
expect(result).toContain('custom-key=value');
expect(result).toContain('sentry-environment=production');
});
it('overwrites existing Sentry entries when keys conflict', () => {
const existing = 'sentry-environment=staging';
const newBaggage = 'sentry-environment=production';
const result = mergeBaggageHeaders(existing, newBaggage);
expect(result).toBe('sentry-environment=production');
});
it('handles multiple entries with Sentry conflicts', () => {
const existing = 'custom-key=value1,sentry-environment=staging';
const newBaggage = 'sentry-environment=production,sentry-trace_id=123';
const result = mergeBaggageHeaders(existing, newBaggage);
expect(result).toContain('custom-key=value1');
expect(result).toContain('sentry-environment=production');
expect(result).toContain('sentry-trace_id=123');
expect(result).not.toContain('sentry-environment=staging');
});
});