Skip to content

Commit 3ab0124

Browse files
authored
Passing correct host header to proxy in ProxyAgent (nodejs#1624)
* slight refactor of test * failing test for nodejs#1623 * ProxyAgent passing correct host headers to proxy, fixes nodejs#1623 * add issue # to test * import fetch for proxy text
1 parent e5ee395 commit 3ab0124

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

lib/proxy-agent.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class ProxyAgent extends DispatcherBase {
6060
}
6161

6262
const resolvedUrl = new URL(opts.uri)
63-
const { origin, port } = resolvedUrl
63+
const { origin, port, host } = resolvedUrl
6464

6565
const connect = buildConnector({ ...opts.proxyTls })
6666
this[kConnectEndpoint] = buildConnector({ ...opts.requestTls })
@@ -80,7 +80,7 @@ class ProxyAgent extends DispatcherBase {
8080
signal: opts.signal,
8181
headers: {
8282
...this[kProxyHeaders],
83-
host: opts.host
83+
host
8484
}
8585
})
8686
if (statusCode !== 200) {

test/proxy-agent.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const { test } = require('tap')
4-
const { request, setGlobalDispatcher, getGlobalDispatcher } = require('..')
4+
const { request, fetch, setGlobalDispatcher, getGlobalDispatcher } = require('..')
55
const { InvalidArgumentError } = require('../lib/core/errors')
66
const { readFileSync } = require('fs')
77
const { join } = require('path')
@@ -240,8 +240,9 @@ test('use proxy-agent with setGlobalDispatcher', async (t) => {
240240
proxyAgent.close()
241241
})
242242

243-
test('ProxyAgent correctly sends headers when using fetch - #1355', { skip: nodeMajor < 16 }, async (t) => {
244-
const { getGlobalDispatcher, setGlobalDispatcher, fetch } = require('../index')
243+
test('ProxyAgent correctly sends headers when using fetch - #1355, #1623', { skip: nodeMajor < 16 }, async (t) => {
244+
t.plan(2)
245+
const defaultDispatcher = getGlobalDispatcher()
245246

246247
const server = await buildServer()
247248
const proxy = await buildProxy()
@@ -250,7 +251,9 @@ test('ProxyAgent correctly sends headers when using fetch - #1355', { skip: node
250251
const proxyUrl = `http://localhost:${proxy.address().port}`
251252

252253
const proxyAgent = new ProxyAgent(proxyUrl)
253-
const oldDispatcher = getGlobalDispatcher()
254+
setGlobalDispatcher(proxyAgent)
255+
256+
t.teardown(() => setGlobalDispatcher(defaultDispatcher))
254257

255258
const expectedHeaders = {
256259
host: `localhost:${server.address().port}`,
@@ -263,16 +266,23 @@ test('ProxyAgent correctly sends headers when using fetch - #1355', { skip: node
263266
'accept-encoding': 'gzip, deflate'
264267
}
265268

269+
const expectedProxyHeaders = {
270+
host: `localhost:${proxy.address().port}`,
271+
connection: 'keep-alive'
272+
}
273+
274+
proxy.on('connect', (req, res) => {
275+
t.same(req.headers, expectedProxyHeaders)
276+
})
277+
266278
server.on('request', (req, res) => {
267279
t.same(req.headers, expectedHeaders)
268280
res.end('goodbye')
269281
})
270282

271-
setGlobalDispatcher(proxyAgent)
272283
await fetch(serverUrl, {
273284
headers: { 'Test-header': 'value' }
274285
})
275-
setGlobalDispatcher(oldDispatcher)
276286

277287
server.close()
278288
proxy.close()

0 commit comments

Comments
 (0)