|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { once } = require('node:events') |
| 4 | +const { createServer } = require('node:http') |
| 5 | +const { test } = require('node:test') |
| 6 | +const { Agent, Request, fetch } = require('../..') |
| 7 | +const { tspl } = require('@matteo.collina/tspl') |
| 8 | + |
| 9 | +test('issue #2828, RequestInit dispatcher options overrides Request input dispatcher', async (t) => { |
| 10 | + const { strictEqual } = tspl(t, { plan: 2 }) |
| 11 | + |
| 12 | + class CustomAgentA extends Agent { |
| 13 | + dispatch (options, handler) { |
| 14 | + options.headers['x-my-header-a'] = 'hello' |
| 15 | + return super.dispatch(...arguments) |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + class CustomAgentB extends Agent { |
| 20 | + dispatch (options, handler) { |
| 21 | + options.headers['x-my-header-b'] = 'world' |
| 22 | + return super.dispatch(...arguments) |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + const server = createServer((req, res) => { |
| 27 | + strictEqual(req.headers['x-my-header-a'], undefined) |
| 28 | + strictEqual(req.headers['x-my-header-b'], 'world') |
| 29 | + res.end() |
| 30 | + }).listen(0) |
| 31 | + |
| 32 | + t.after(server.close.bind(server)) |
| 33 | + await once(server, 'listening') |
| 34 | + |
| 35 | + const request = new Request(`http://localhost:${server.address().port}`, { |
| 36 | + dispatcher: new CustomAgentA() |
| 37 | + }) |
| 38 | + |
| 39 | + await fetch(request, { |
| 40 | + dispatcher: new CustomAgentB() |
| 41 | + }) |
| 42 | +}) |
0 commit comments