diff --git a/lib/proxy-agent.js b/lib/proxy-agent.js index c2e7f268b70..128daddbef2 100644 --- a/lib/proxy-agent.js +++ b/lib/proxy-agent.js @@ -53,7 +53,7 @@ class ProxyAgent extends DispatcherBase { this[kRequestTls] = opts.requestTls this[kProxyTls] = opts.proxyTls - this[kProxyHeaders] = {} + this[kProxyHeaders] = opts.headers || {} if (opts.auth && opts.token) { throw new InvalidArgumentError('opts.auth cannot be used in combination with opts.token') diff --git a/test/proxy-agent.js b/test/proxy-agent.js index 03156395eff..8c6b5999278 100644 --- a/test/proxy-agent.js +++ b/test/proxy-agent.js @@ -197,6 +197,39 @@ test('use proxy-agent with token', async (t) => { proxyAgent.close() }) +test('use proxy-agent with custom headers', async (t) => { + t.plan(2) + const server = await buildServer() + const proxy = await buildProxy() + + const serverUrl = `http://localhost:${server.address().port}` + const proxyUrl = `http://localhost:${proxy.address().port}` + const proxyAgent = new ProxyAgent({ + uri: proxyUrl, + headers: { + 'User-Agent': 'Foobar/1.0.0' + } + }) + + proxy.on('connect', (req) => { + t.equal(req.headers['user-agent'], 'Foobar/1.0.0') + }) + + server.on('request', (req, res) => { + t.equal(req.headers['user-agent'], 'BarBaz/1.0.0') + res.end() + }) + + await request(serverUrl + '/hello?foo=bar', { + headers: { 'user-agent': 'BarBaz/1.0.0' }, + dispatcher: proxyAgent + }) + + server.close() + proxy.close() + proxyAgent.close() +}) + test('sending proxy-authorization in request headers should throw', async (t) => { t.plan(3) const server = await buildServer() diff --git a/types/proxy-agent.d.ts b/types/proxy-agent.d.ts index f5874716f72..55058734f78 100644 --- a/types/proxy-agent.d.ts +++ b/types/proxy-agent.d.ts @@ -1,3 +1,5 @@ +import { IncomingHttpHeaders } from 'http' + import Agent from './agent' import buildConnector from './connector'; import Dispatcher from './dispatcher' @@ -19,6 +21,7 @@ declare namespace ProxyAgent { */ auth?: string; token?: string; + headers?: IncomingHttpHeaders; requestTls?: buildConnector.BuildOptions; proxyTls?: buildConnector.BuildOptions; }