Skip to content

Commit

Permalink
Passing correct host header to proxy in ProxyAgent (nodejs#1624)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
protometa authored and metcoder95 committed Dec 26, 2022
1 parent 2e4f909 commit dd8e78a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand All @@ -80,7 +80,7 @@ class ProxyAgent extends DispatcherBase {
signal: opts.signal,
headers: {
...this[kProxyHeaders],
host: opts.host
host
}
})
if (statusCode !== 200) {
Expand Down
22 changes: 16 additions & 6 deletions test/proxy-agent.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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()
Expand All @@ -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}`,
Expand All @@ -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()
Expand Down

0 comments on commit dd8e78a

Please sign in to comment.