diff --git a/lib/proxy-agent.js b/lib/proxy-agent.js index 6d879d02216..716a950f8b5 100644 --- a/lib/proxy-agent.js +++ b/lib/proxy-agent.js @@ -60,7 +60,7 @@ class ProxyAgent extends DispatcherBase { } const resolvedUrl = new URL(opts.uri) - const { origin, port } = resolvedUrl + const { origin, port, host } = resolvedUrl const connect = buildConnector({ ...opts.proxyTls }) this[kConnectEndpoint] = buildConnector({ ...opts.requestTls }) @@ -80,7 +80,7 @@ class ProxyAgent extends DispatcherBase { signal: opts.signal, headers: { ...this[kProxyHeaders], - host: opts.host + host } }) if (statusCode !== 200) { diff --git a/test/proxy-agent.js b/test/proxy-agent.js index c573fcc43ce..169871823ca 100644 --- a/test/proxy-agent.js +++ b/test/proxy-agent.js @@ -1,7 +1,7 @@ 'use strict' const { test } = require('tap') -const { request, setGlobalDispatcher, getGlobalDispatcher } = require('..') +const { request, fetch, setGlobalDispatcher, getGlobalDispatcher } = require('..') const { InvalidArgumentError } = require('../lib/core/errors') const { readFileSync } = require('fs') const { join } = require('path') @@ -240,8 +240,9 @@ test('use proxy-agent with setGlobalDispatcher', async (t) => { proxyAgent.close() }) -test('ProxyAgent correctly sends headers when using fetch - #1355', { skip: nodeMajor < 16 }, async (t) => { - const { getGlobalDispatcher, setGlobalDispatcher, fetch } = require('../index') +test('ProxyAgent correctly sends headers when using fetch - #1355, #1623', { skip: nodeMajor < 16 }, async (t) => { + t.plan(2) + const defaultDispatcher = getGlobalDispatcher() const server = await buildServer() const proxy = await buildProxy() @@ -250,7 +251,9 @@ test('ProxyAgent correctly sends headers when using fetch - #1355', { skip: node const proxyUrl = `http://localhost:${proxy.address().port}` const proxyAgent = new ProxyAgent(proxyUrl) - const oldDispatcher = getGlobalDispatcher() + setGlobalDispatcher(proxyAgent) + + t.teardown(() => setGlobalDispatcher(defaultDispatcher)) const expectedHeaders = { host: `localhost:${server.address().port}`, @@ -263,16 +266,23 @@ test('ProxyAgent correctly sends headers when using fetch - #1355', { skip: node 'accept-encoding': 'gzip, deflate' } + const expectedProxyHeaders = { + host: `localhost:${proxy.address().port}`, + connection: 'keep-alive' + } + + proxy.on('connect', (req, res) => { + t.same(req.headers, expectedProxyHeaders) + }) + server.on('request', (req, res) => { t.same(req.headers, expectedHeaders) res.end('goodbye') }) - setGlobalDispatcher(proxyAgent) await fetch(serverUrl, { headers: { 'Test-header': 'value' } }) - setGlobalDispatcher(oldDispatcher) server.close() proxy.close()