diff --git a/index.js b/index.js index 0f290f4..c5a5389 100644 --- a/index.js +++ b/index.js @@ -19,7 +19,8 @@ class HttpProxyAgent extends http.Agent { host: this.proxy.hostname, port: this.proxy.port, path: `${options.host}:${options.port}`, - headers: { connection: this.keepAlive ? 'keep-alive' : 'close' }, + setHost: false, + headers: { connection: this.keepAlive ? 'keep-alive' : 'close', host: `${options.host}:${options.port}` }, agent: false } @@ -63,7 +64,8 @@ class HttpsProxyAgent extends https.Agent { host: this.proxy.hostname, port: this.proxy.port, path: `${options.host}:${options.port}`, - headers: { connection: this.keepAlive ? 'keep-alive' : 'close' }, + setHost: false, + headers: { connection: this.keepAlive ? 'keep-alive' : 'close', host: `${options.host}:${options.port}` }, agent: false } diff --git a/test/http-http.test.js b/test/http-http.test.js index 8ce17dd..b6f0dde 100644 --- a/test/http-http.test.js +++ b/test/http-http.test.js @@ -274,3 +274,41 @@ test('Configure the agent to NOT reuse sockets', async t => { server.close() proxy.close() }) + +test('Test Host Header', async t => { + const server = await createServer() + const proxy = await createProxy() + server.on('request', (req, res) => res.end('ok')) + + proxy.authenticate = function (req, fn) { + t.is(req.headers.host, `${server.address().address}:${server.address().port}`) + fn(null, true) + } + + const response = await request({ + method: 'GET', + hostname: server.address().address, + port: server.address().port, + path: '/', + agent: new HttpProxyAgent({ + keepAlive: true, + keepAliveMsecs: 1000, + maxSockets: 256, + maxFreeSockets: 256, + scheduling: 'lifo', + proxy: `http://${proxy.address().address}:${proxy.address().port}` + }) + }) + + let body = '' + response.setEncoding('utf8') + for await (const chunk of response) { + body += chunk + } + + t.is(body, 'ok') + t.is(response.statusCode, 200) + + server.close() + proxy.close() +})