|
| 1 | +import { parseOtlpHeaders } from '../otel'; |
| 2 | + |
| 3 | +describe('Parse OTLP Headers', () => { |
| 4 | + it('should return an empty object when no headers string is provided', () => { |
| 5 | + expect(parseOtlpHeaders()).toEqual({}); |
| 6 | + }); |
| 7 | + |
| 8 | + it('should correctly parse a single header', () => { |
| 9 | + expect(parseOtlpHeaders('key1=value1')).toEqual({ key1: 'value1' }); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should correctly parse multiple headers', () => { |
| 13 | + expect(parseOtlpHeaders('key1=value1,key2=value2')).toEqual({ |
| 14 | + key1: 'value1', |
| 15 | + key2: 'value2', |
| 16 | + }); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should handle spaces around keys and values', () => { |
| 20 | + expect(parseOtlpHeaders(' key1 = value1 , key2 = value2 ')).toEqual({ |
| 21 | + key1: 'value1', |
| 22 | + key2: 'value2', |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should ignore malformed headers without "="', () => { |
| 27 | + expect(parseOtlpHeaders('key1=value1,malformedHeader,key2=value2')).toEqual( |
| 28 | + { |
| 29 | + key1: 'value1', |
| 30 | + key2: 'value2', |
| 31 | + }, |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should handle empty values', () => { |
| 36 | + expect(parseOtlpHeaders('key1=,key2=value2')).toEqual({ |
| 37 | + key1: '', |
| 38 | + key2: 'value2', |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should handle special characters in values', () => { |
| 43 | + expect( |
| 44 | + parseOtlpHeaders( |
| 45 | + 'Authorization=Bearer token123,Content-Type=application/json', |
| 46 | + ), |
| 47 | + ).toEqual({ |
| 48 | + Authorization: 'Bearer token123', |
| 49 | + 'Content-Type': 'application/json', |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should handle trailing comma', () => { |
| 54 | + expect(parseOtlpHeaders('key1=value1,')).toEqual({ |
| 55 | + key1: 'value1', |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should handle leading comma', () => { |
| 60 | + expect(parseOtlpHeaders(',key1=value1')).toEqual({ |
| 61 | + key1: 'value1', |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should handle multiple consecutive commas', () => { |
| 66 | + expect(parseOtlpHeaders('key1=value1,,key2=value2')).toEqual({ |
| 67 | + key1: 'value1', |
| 68 | + key2: 'value2', |
| 69 | + }); |
| 70 | + }); |
| 71 | +}); |
0 commit comments