Skip to content

Commit 580e36e

Browse files
authored
chore: fix test breakage in error-reporting (googleapis#8966)
* chore: fix test breakage in error-reporting * rollback to minimum set of changes required * add test whitespace for readability
1 parent 93c7200 commit 580e36e

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

  • handwritten/error-reporting

handwritten/error-reporting/src/request-extractors/hapi.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff 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
/**

handwritten/error-reporting/test/unit/request-extractors/hapi.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff 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
});

0 commit comments

Comments
 (0)