1- import { createBaggage , getBaggageValue } from '../src/baggage' ;
1+ import { createBaggage , getBaggageValue , parseBaggageString , serializeBaggage , setBaggageValue } from '../src/baggage' ;
22
33describe ( 'Baggage' , ( ) => {
44 describe ( 'createBaggage' , ( ) => {
55 it . each ( [
6- [ 'creates an empty baggage instance' , { } , { } ] ,
6+ [ 'creates an empty baggage instance' , { } , [ { } , '' ] ] ,
77 [
88 'creates a baggage instance with initial values' ,
99 { environment : 'production' , anyKey : 'anyValue' } ,
10- { environment : 'production' , anyKey : 'anyValue' } ,
10+ [ { environment : 'production' , anyKey : 'anyValue' } , '' ] ,
1111 ] ,
1212 ] ) ( '%s' , ( _ : string , input , output ) => {
1313 expect ( createBaggage ( input ) ) . toEqual ( output ) ;
@@ -16,10 +16,77 @@ describe('Baggage', () => {
1616
1717 describe ( 'getBaggageValue' , ( ) => {
1818 it . each ( [
19- [ 'gets a baggage item' , { environment : 'production' , anyKey : 'anyValue' } , 'environment' , 'production' ] ,
20- [ 'finds undefined items' , { } , 'environment' , undefined ] ,
19+ [
20+ 'gets a baggage item' ,
21+ createBaggage ( { environment : 'production' , anyKey : 'anyValue' } ) ,
22+ 'environment' ,
23+ 'production' ,
24+ ] ,
25+ [ 'finds undefined items' , createBaggage ( { } ) , 'environment' , undefined ] ,
2126 ] ) ( '%s' , ( _ : string , baggage , key , value ) => {
2227 expect ( getBaggageValue ( baggage , key ) ) . toEqual ( value ) ;
2328 } ) ;
2429 } ) ;
30+
31+ describe ( 'setBaggageValue' , ( ) => {
32+ it . each ( [
33+ [ 'sets a baggage item' , createBaggage ( { } ) , 'environment' , 'production' ] ,
34+ [ 'overwrites a baggage item' , createBaggage ( { environment : 'development' } ) , 'environment' , 'production' ] ,
35+ ] ) ( '%s' , ( _ : string , baggage , key , value ) => {
36+ setBaggageValue ( baggage , key , value ) ;
37+ expect ( getBaggageValue ( baggage , key ) ) . toEqual ( value ) ;
38+ } ) ;
39+ } ) ;
40+
41+ describe ( 'serializeBaggage' , ( ) => {
42+ it . each ( [
43+ [ 'serializes empty baggage' , createBaggage ( { } ) , '' ] ,
44+ [
45+ 'serializes baggage with a single value' ,
46+ createBaggage ( { environment : 'production' } ) ,
47+ 'sentry-environment=production' ,
48+ ] ,
49+ [
50+ 'serializes baggage with multiple values' ,
51+ createBaggage ( { environment : 'production' , release : '10.0.2' } ) ,
52+ 'sentry-environment=production,sentry-release=10.0.2' ,
53+ ] ,
54+ [
55+ 'keeps non-sentry prefixed baggage items' ,
56+ createBaggage (
57+ { environment : 'production' , release : '10.0.2' } ,
58+ 'userId=alice,serverNode=DF%2028,isProduction=false' ,
59+ ) ,
60+ 'userId=alice,serverNode=DF%2028,isProduction=false,sentry-environment=production,sentry-release=10.0.2' ,
61+ ] ,
62+ [
63+ 'can only use non-sentry prefixed baggage items' ,
64+ createBaggage ( { } , 'userId=alice,serverNode=DF%2028,isProduction=false' ) ,
65+ 'userId=alice,serverNode=DF%2028,isProduction=false' ,
66+ ] ,
67+ ] ) ( '%s' , ( _ : string , baggage , serializedBaggage ) => {
68+ expect ( serializeBaggage ( baggage ) ) . toEqual ( serializedBaggage ) ;
69+ } ) ;
70+ } ) ;
71+
72+ describe ( 'parseBaggageString' , ( ) => {
73+ it . each ( [
74+ [ 'parses an empty string' , '' , createBaggage ( { } ) ] ,
75+ [
76+ 'parses sentry values into baggage' ,
77+ 'sentry-environment=production,sentry-release=10.0.2' ,
78+ createBaggage ( { environment : 'production' , release : '10.0.2' } ) ,
79+ ] ,
80+ [
81+ 'parses arbitrary baggage headers' ,
82+ 'userId=alice,serverNode=DF%2028,isProduction=false,sentry-environment=production,sentry-release=10.0.2' ,
83+ createBaggage (
84+ { environment : 'production' , release : '10.0.2' } ,
85+ 'userId=alice,serverNode=DF%2028,isProduction=false' ,
86+ ) ,
87+ ] ,
88+ ] ) ( '%s' , ( _ : string , baggageString , baggage ) => {
89+ expect ( parseBaggageString ( baggageString ) ) . toEqual ( baggage ) ;
90+ } ) ;
91+ } ) ;
2592} ) ;
0 commit comments