|
| 1 | +import { createBaggage, getBaggageValue, parseBaggageString, serializeBaggage, setBaggageValue } from '../src/baggage'; |
| 2 | + |
| 3 | +describe('Baggage', () => { |
| 4 | + describe('createBaggage', () => { |
| 5 | + it.each([ |
| 6 | + ['creates an empty baggage instance', {}, [{}, '']], |
| 7 | + [ |
| 8 | + 'creates a baggage instance with initial values', |
| 9 | + { environment: 'production', anyKey: 'anyValue' }, |
| 10 | + [{ environment: 'production', anyKey: 'anyValue' }, ''], |
| 11 | + ], |
| 12 | + ])('%s', (_: string, input, output) => { |
| 13 | + expect(createBaggage(input)).toEqual(output); |
| 14 | + }); |
| 15 | + }); |
| 16 | + |
| 17 | + describe('getBaggageValue', () => { |
| 18 | + it.each([ |
| 19 | + [ |
| 20 | + 'gets a baggage item', |
| 21 | + createBaggage({ environment: 'production', anyKey: 'anyValue' }), |
| 22 | + 'environment', |
| 23 | + 'production', |
| 24 | + ], |
| 25 | + ['finds undefined items', createBaggage({}), 'environment', undefined], |
| 26 | + ])('%s', (_: string, baggage, key, value) => { |
| 27 | + expect(getBaggageValue(baggage, key)).toEqual(value); |
| 28 | + }); |
| 29 | + }); |
| 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 | + }); |
| 92 | +}); |
0 commit comments