Skip to content

Commit

Permalink
fix: mock fetch headers shouldn't be an array (#2080)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Apr 19, 2023
1 parent 472c40e commit 9041e9f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
15 changes: 8 additions & 7 deletions lib/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'use strict'

const { kHeadersList } = require('../core/symbols')
const { kGuard, kHeadersCaseInsensitive } = require('./symbols')
const { kGuard } = require('./symbols')
const { kEnumerableProperty } = require('../core/util')
const {
makeIterator,
Expand Down Expand Up @@ -173,15 +173,16 @@ class HeadersList {
}
}

get [kHeadersCaseInsensitive] () {
/** @type {string[]} */
const flatList = []
get entries () {
const headers = {}

for (const { name, value } of this[kHeadersMap].values()) {
flatList.push(name, value)
if (this[kHeadersMap].size) {
for (const { name, value } of this[kHeadersMap].values()) {
headers[name] = value
}
}

return flatList
return headers
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const {
urlIsHttpHttpsScheme,
urlHasHttpsScheme
} = require('./util')
const { kState, kHeaders, kGuard, kRealm, kHeadersCaseInsensitive } = require('./symbols')
const { kState, kHeaders, kGuard, kRealm } = require('./symbols')
const assert = require('assert')
const { safelyExtractBody } = require('./body')
const {
Expand Down Expand Up @@ -1950,7 +1950,7 @@ async function httpNetworkFetch (
origin: url.origin,
method: request.method,
body: fetchParams.controller.dispatcher.isMockActive ? request.body && request.body.source : body,
headers: request.headersList[kHeadersCaseInsensitive],
headers: request.headersList.entries,
maxRedirections: 0,
upgrade: request.mode === 'websocket' ? 'websocket' : undefined
},
Expand Down
3 changes: 1 addition & 2 deletions lib/fetch/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ module.exports = {
kSignal: Symbol('signal'),
kState: Symbol('state'),
kGuard: Symbol('guard'),
kRealm: Symbol('realm'),
kHeadersCaseInsensitive: Symbol('headers case insensitive')
kRealm: Symbol('realm')
}
30 changes: 30 additions & 0 deletions test/issue-2078.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

const { test, skip } = require('tap')
const { nodeMajor, nodeMinor } = require('../lib/core/util')
const { MockAgent, getGlobalDispatcher, setGlobalDispatcher, fetch } = require('..')

if (nodeMajor < 16 || (nodeMajor === 16 && nodeMinor < 8)) {
skip('fetch is not supported in node < v16.8.0')
process.exit()
}

test('MockPool.reply headers are an object, not an array - issue #2078', async (t) => {
const global = getGlobalDispatcher()
const mockAgent = new MockAgent()
const mockPool = mockAgent.get('http://localhost')

t.teardown(() => setGlobalDispatcher(global))
setGlobalDispatcher(mockAgent)

mockPool.intercept({
path: '/foo',
method: 'GET'
}).reply((options) => {
t.ok(!Array.isArray(options.headers))

return { statusCode: 200 }
})

await t.resolves(fetch('http://localhost/foo'))
})

0 comments on commit 9041e9f

Please sign in to comment.