Skip to content

Commit

Permalink
Fix test suite for Node.js 19.1
Browse files Browse the repository at this point in the history
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: nodejs/node#45510
  • Loading branch information
luismiramirez committed Nov 23, 2022
1 parent 686d318 commit dde3448
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/__tests__/transmitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
18 changes: 17 additions & 1 deletion src/transmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<any>): 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

Expand Down

0 comments on commit dde3448

Please sign in to comment.