Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions handwritten/error-reporting/src/request-extractors/hapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ function extractRemoteAddressFromRequest(req: hapi.Request) {
/**
* Helper to normalize headers that might be arrays into a single string.
*/
function getSingleHeader(
val: string | string[] | undefined,
): string | undefined {
return Array.isArray(val) ? val[0] : val;
function getSingleHeader(val: unknown): string | undefined {
if (Array.isArray(val)) {
return typeof val[0] === 'string' ? val[0] : undefined;
}
return typeof val === 'string' ? val : undefined;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,26 @@ describe('hapiRequestInformationExtractor behaviour', () => {
EXPECTED,
);
});
it('Should handle array headers correctly', () => {
const REQUEST = {
...FULL_REQ_DERIVATION_VALUE,
headers: {
'x-forwarded-for': ['0.0.0.1', '0.0.0.2'],
'user-agent': ['Mozilla/5.0', 'Chrome/90'],
referrer: ['www.ANOTHER-TEST.com'],
},
};
const EXPECTED = {
...FULL_REQ_EXPECTED_VALUE,
userAgent: 'Mozilla/5.0',
referrer: 'www.ANOTHER-TEST.com',
remoteAddress: '0.0.0.1',
};

deepStrictEqual(
hapiRequestInformationExtractor(REQUEST as {} as hapi.Request),
EXPECTED,
);
});
});
});
Loading