Skip to content

Commit

Permalink
override request dispatcher from init (#2928)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieusieben committed Mar 6, 2024
1 parent 7d20bed commit be8ef8e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/web/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class Request {
// 5. Set fallbackMode to "cors".
fallbackMode = 'cors'
} else {
this[kDispatcher] = input[kDispatcher]
this[kDispatcher] = init.dispatcher || input[kDispatcher]

// 6. Otherwise:

Expand Down
42 changes: 42 additions & 0 deletions test/fetch/issue-2898-comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict'

const { once } = require('node:events')
const { createServer } = require('node:http')
const { test } = require('node:test')
const { Agent, Request, fetch } = require('../..')
const { tspl } = require('@matteo.collina/tspl')

test('issue #2828, RequestInit dispatcher options overrides Request input dispatcher', async (t) => {
const { strictEqual } = tspl(t, { plan: 2 })

class CustomAgentA extends Agent {
dispatch (options, handler) {
options.headers['x-my-header-a'] = 'hello'
return super.dispatch(...arguments)
}
}

class CustomAgentB extends Agent {
dispatch (options, handler) {
options.headers['x-my-header-b'] = 'world'
return super.dispatch(...arguments)
}
}

const server = createServer((req, res) => {
strictEqual(req.headers['x-my-header-a'], undefined)
strictEqual(req.headers['x-my-header-b'], 'world')
res.end()
}).listen(0)

t.after(server.close.bind(server))
await once(server, 'listening')

const request = new Request(`http://localhost:${server.address().port}`, {
dispatcher: new CustomAgentA()
})

await fetch(request, {
dispatcher: new CustomAgentB()
})
})

0 comments on commit be8ef8e

Please sign in to comment.