Skip to content

Commit

Permalink
Merge 15590a7 into c22c297
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 committed May 25, 2020
2 parents c22c297 + 15590a7 commit d60c75b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/middleware/proxy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const url = require('url')
const { Agent: httpAgent } = require('http')
const { Agent: httpsAgent } = require('https')
const httpProxy = require('http-proxy')
const _ = require('lodash')

Expand Down Expand Up @@ -42,11 +44,14 @@ function parseProxyConfig (proxies, config) {
port = config.port
}
const changeOrigin = proxyConfiguration.changeOrigin || false
const Agent = https ? httpsAgent : httpAgent
const keepAliveAgent = new Agent({ keepAlive: true })
const proxy = httpProxy.createProxyServer({
target: { host: hostname, port, https, protocol },
xfwd: true,
changeOrigin: changeOrigin,
secure: config.proxyValidateSSL
secure: config.proxyValidateSSL,
agent: keepAliveAgent
})

;['proxyReq', 'proxyRes'].forEach(function (name) {
Expand Down
20 changes: 20 additions & 0 deletions test/unit/middleware/proxy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,24 @@ describe('middleware.proxy', () => {
it('should handle empty proxy config', () => {
expect(m.parseProxyConfig({})).to.deep.equal([])
})

it('should use http agent with keepAlive=true', () => {
const proxy = { '/base': 'http://localhost:8000/proxy' }
const parsedProxyConfig = m.parseProxyConfig(proxy, {})
expect(parsedProxyConfig).to.have.length(1)
expect(parsedProxyConfig[0].proxy.options.agent).to.containSubset({
keepAlive: true,
protocol: 'http:'
})
})

it('should use https agent with keepAlive=true', () => {
const proxy = { '/base': 'https://localhost:8000/proxy' }
const parsedProxyConfig = m.parseProxyConfig(proxy, {})
expect(parsedProxyConfig).to.have.length(1)
expect(parsedProxyConfig[0].proxy.options.agent).to.containSubset({
keepAlive: true,
protocol: 'https:'
})
})
})

0 comments on commit d60c75b

Please sign in to comment.