Skip to content

Commit

Permalink
fix(proxy): use https agent for https protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 committed May 25, 2020
1 parent 96c4351 commit 15590a7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/middleware/proxy.js
@@ -1,5 +1,6 @@
const url = require('url')
const http = require('http')
const { Agent: httpAgent } = require('http')
const { Agent: httpsAgent } = require('https')
const httpProxy = require('http-proxy')
const _ = require('lodash')

Expand Down Expand Up @@ -43,7 +44,8 @@ function parseProxyConfig (proxies, config) {
port = config.port
}
const changeOrigin = proxyConfiguration.changeOrigin || false
const keepAliveAgent = new http.Agent({ keepAlive: true })
const Agent = https ? httpsAgent : httpAgent
const keepAliveAgent = new Agent({ keepAlive: true })
const proxy = httpProxy.createProxyServer({
target: { host: hostname, port, https, protocol },
xfwd: true,
Expand Down
15 changes: 13 additions & 2 deletions test/unit/middleware/proxy.spec.js
Expand Up @@ -353,12 +353,23 @@ describe('middleware.proxy', () => {
expect(m.parseProxyConfig({})).to.deep.equal([])
})

it('should use agent with keepAlive=true', () => {
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
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 15590a7

Please sign in to comment.