Skip to content

Commit e41c344

Browse files
add tests for parsing headers
1 parent 972fbd5 commit e41c344

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
});

packages/node-opentelemetry/src/otel.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ const hrtimeToMs = (hrtime: [number, number]) => {
150150
* Parses OTEL_EXPORTER_OTLP_HEADERS environment variable into a structured headers object.
151151
* Format: "key1=value1,key2=value2" -> { key1: "value1", key2: "value2" }
152152
*/
153-
const parseOtlpHeaders = (headersString?: string): Record<string, string> => {
153+
export const parseOtlpHeaders = (
154+
headersString?: string,
155+
): Record<string, string> => {
154156
if (!headersString) {
155157
return {};
156158
}

0 commit comments

Comments
 (0)