From dde344853969cbd155b452f5fe207337d06eeaa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luismi=20Ram=C3=ADrez?= Date: Wed, 23 Nov 2022 10:43:12 +0100 Subject: [PATCH] Fix test suite for Node.js 19.1 The header setters are now no-op functions since Node.js 19.1. Nock, the http mocking library we use for our transmitter tests uses these setters so our tests break. This commit uses the `rawHeaders` attribute instead to get green tests until the issue is fixed in either side. More info: https://github.com/nodejs/node/issues/45510 --- src/__tests__/transmitter.test.ts | 3 ++- src/transmitter.ts | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/__tests__/transmitter.test.ts b/src/__tests__/transmitter.test.ts index d91b04db..fd8cb1c4 100644 --- a/src/__tests__/transmitter.test.ts +++ b/src/__tests__/transmitter.test.ts @@ -267,7 +267,8 @@ describe("Transmitter", () => { nock("http://example.invalid") .get("/301") .reply(301, undefined, { - Location: "http://example.invalid/302" + Location: "http://example.invalid/302", + FooHeader: "Location" }) .get("/302") .reply(302, undefined, { diff --git a/src/transmitter.ts b/src/transmitter.ts index e3665ebd..62e56273 100644 --- a/src/transmitter.ts +++ b/src/transmitter.ts @@ -137,7 +137,7 @@ export class Transmitter { return stream => { const responseStatus = stream.statusCode ?? 999 const isRedirect = REDIRECT_STATUS_CODES.indexOf(responseStatus) !== -1 - const newURL = stream.headers?.location + const newURL = this.getLocationHeader(stream.rawHeaders || []) if (isRedirect && typeof newURL !== "undefined") { const redirectCount = callback[REDIRECT_COUNT] ?? 0 @@ -169,6 +169,22 @@ export class Transmitter { } } + // Temporary fix to deal with the header setter removal in Node.js 19 + // https://github.com/nodejs/node/issues/45510 + private getLocationHeader(rawHeaders: Array): string | undefined { + let location + rawHeaders.forEach((element, index) => { + // Skip odd indices as rawHeaders are represented as an array of pairs (key, value) + if (Math.abs(index % 2) == 1) return + + if (element == "Location") { + location = rawHeaders[index + 1] + } + }) + + return location + } + private configParams(): URLSearchParams { const config_data = this.#config.data