diff --git a/lib/middleware/proxy.js b/lib/middleware/proxy.js index d84c7e4af..1a4f31e93 100644 --- a/lib/middleware/proxy.js +++ b/lib/middleware/proxy.js @@ -40,10 +40,15 @@ var parseProxyConfig = function (proxies, config) { var hostname = proxyDetails.hostname || config.hostname var protocol = proxyDetails.protocol || config.protocol - var port = proxyDetails.port || config.port || - (proxyDetails.protocol === 'https:' ? '443' : '80') var https = proxyDetails.protocol === 'https:' - + var port + if (proxyDetails.port) { + port = proxyDetails.port + } else if (proxyDetails.protocol) { + port = proxyDetails.protocol === 'https:' ? '443' : '80' + } else { + port = config.port + } var changeOrigin = 'changeOrigin' in proxyConfiguration ? proxyConfiguration.changeOrigin : false var proxy = httpProxy.createProxyServer({ target: { diff --git a/test/unit/middleware/proxy.spec.js b/test/unit/middleware/proxy.spec.js index a3eaad4b5..ba5b8dd70 100644 --- a/test/unit/middleware/proxy.spec.js +++ b/test/unit/middleware/proxy.spec.js @@ -253,6 +253,36 @@ describe('middleware.proxy', () => { expect(parsedProxyConfig[0].proxy).to.exist }) + it('should parse right port of proxy target', () => { + var proxy = { '/w': 'http://krinkle.dev/w' } + var config = {port: 9877, hostname: 'localhost'} + var parsedProxyConfig = m.parseProxyConfig(proxy, config) + expect(parsedProxyConfig).to.have.length(1) + expect(parsedProxyConfig[0]).to.containSubset({ + host: 'krinkle.dev', + port: '80', + baseUrl: '/w', + path: '/w', + https: false + }) + expect(parsedProxyConfig[0].proxy).to.exist + }) + + it('should parse right port of proxy target w. https', () => { + var proxy = { '/w': 'https://krinkle.dev/w' } + var config = {port: 9877, hostname: 'localhost'} + var parsedProxyConfig = m.parseProxyConfig(proxy, config) + expect(parsedProxyConfig).to.have.length(1) + expect(parsedProxyConfig[0]).to.containSubset({ + host: 'krinkle.dev', + port: '443', + baseUrl: '/w', + path: '/w', + https: true + }) + expect(parsedProxyConfig[0].proxy).to.exist + }) + it('should normalize proxy url', () => { var proxy = {'/base/': 'http://localhost:8000/proxy/test'} var parsedProxyConfig = m.parseProxyConfig(proxy, {})