File tree Expand file tree Collapse file tree
handwritten/error-reporting
test/unit/request-extractors Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -65,10 +65,11 @@ function extractRemoteAddressFromRequest(req: hapi.Request) {
6565/**
6666 * Helper to normalize headers that might be arrays into a single string.
6767 */
68- function getSingleHeader (
69- val : string | string [ ] | undefined ,
70- ) : string | undefined {
71- return Array . isArray ( val ) ? val [ 0 ] : val ;
68+ function getSingleHeader ( val : unknown ) : string | undefined {
69+ if ( Array . isArray ( val ) ) {
70+ return typeof val [ 0 ] === 'string' ? val [ 0 ] : undefined ;
71+ }
72+ return typeof val === 'string' ? val : undefined ;
7273}
7374
7475/**
Original file line number Diff line number Diff line change @@ -138,5 +138,26 @@ describe('hapiRequestInformationExtractor behaviour', () => {
138138 EXPECTED ,
139139 ) ;
140140 } ) ;
141+ it ( 'Should handle array headers correctly' , ( ) => {
142+ const REQUEST = {
143+ ...FULL_REQ_DERIVATION_VALUE ,
144+ headers : {
145+ 'x-forwarded-for' : [ '0.0.0.1' , '0.0.0.2' ] ,
146+ 'user-agent' : [ 'Mozilla/5.0' , 'Chrome/90' ] ,
147+ referrer : [ 'www.ANOTHER-TEST.com' ] ,
148+ } ,
149+ } ;
150+ const EXPECTED = {
151+ ...FULL_REQ_EXPECTED_VALUE ,
152+ userAgent : 'Mozilla/5.0' ,
153+ referrer : 'www.ANOTHER-TEST.com' ,
154+ remoteAddress : '0.0.0.1' ,
155+ } ;
156+
157+ deepStrictEqual (
158+ hapiRequestInformationExtractor ( REQUEST as { } as hapi . Request ) ,
159+ EXPECTED ,
160+ ) ;
161+ } ) ;
141162 } ) ;
142163} ) ;
You can’t perform that action at this time.
0 commit comments