|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as assert from 'assert'; |
| 18 | +import { parseKeyPairsIntoRecord } from '../../../src/baggage/utils'; |
| 19 | + |
| 20 | +describe('parseKeyPairsIntoRecord()', () => { |
| 21 | + it('returns an empty object when value is not a string', () => { |
| 22 | + assert.deepStrictEqual(parseKeyPairsIntoRecord(), {}); |
| 23 | + }); |
| 24 | + |
| 25 | + it('returns an empty object when value is the empty string', () => { |
| 26 | + assert.deepStrictEqual(parseKeyPairsIntoRecord(''), {}); |
| 27 | + }); |
| 28 | + |
| 29 | + it('parses a single key-value pair', () => { |
| 30 | + const value = 'key1=value1'; |
| 31 | + const result = parseKeyPairsIntoRecord(value); |
| 32 | + assert.deepStrictEqual(result, { key1: 'value1' }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('parses multiple key-value pairs', () => { |
| 36 | + const value = 'key1=value1,key2=value2,key3=value3'; |
| 37 | + const result = parseKeyPairsIntoRecord(value); |
| 38 | + assert.deepStrictEqual(result, { |
| 39 | + key1: 'value1', |
| 40 | + key2: 'value2', |
| 41 | + key3: 'value3', |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('ignores leading and trailing whitespace', () => { |
| 46 | + const value = ' key1=value1, key2=value2 '; |
| 47 | + const result = parseKeyPairsIntoRecord(value); |
| 48 | + assert.deepStrictEqual(result, { key1: 'value1', key2: 'value2' }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('filters out invalid pairs', () => { |
| 52 | + assert.deepStrictEqual(parseKeyPairsIntoRecord('key1,key2=value2'), { |
| 53 | + key2: 'value2', |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('filters out empty values', () => { |
| 58 | + assert.deepStrictEqual(parseKeyPairsIntoRecord('key1=,key2=value2'), { |
| 59 | + key2: 'value2', |
| 60 | + }); |
| 61 | + }); |
| 62 | +}); |
0 commit comments