Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test suite for Node.js 19.1 #805

Merged
merged 1 commit into from
Nov 23, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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