diff --git a/lib/middleware/proxy.js b/lib/middleware/proxy.js index c6d302e80..e439c525f 100644 --- a/lib/middleware/proxy.js +++ b/lib/middleware/proxy.js @@ -2,9 +2,8 @@ var url = require('url'); var httpProxy = require('http-proxy'); var log = require('../logger').create('proxy'); -var constant = require('../constants'); -var parseProxyConfig = function(proxies) { +var parseProxyConfig = function(proxies, config) { var proxyConfig = {}; var endsWithSlash = function(str) { return str.substr(-1) === '/'; @@ -45,8 +44,8 @@ var parseProxyConfig = function(proxies) { if (!proxyConfig[proxyPath].port) { if (!proxyConfig[proxyPath].host) { - proxyConfig[proxyPath].host = constant.DEFAULT_HOSTNAME; - proxyConfig[proxyPath].port = constant.DEFAULT_PORT; + proxyConfig[proxyPath].host = config.hostname; + proxyConfig[proxyPath].port = config.port; } else { proxyConfig[proxyPath].port = proxyConfig[proxyPath].https ? '443' : '80'; } @@ -63,8 +62,8 @@ var parseProxyConfig = function(proxies) { * @param proxies a map of routes to proxy url * @return {Function} handler function */ -var createProxyHandler = function(proxy, proxyConfig, proxyValidateSSL, urlRoot) { - var proxies = parseProxyConfig(proxyConfig); +var createProxyHandler = function(proxy, proxyConfig, proxyValidateSSL, urlRoot, config) { + var proxies = parseProxyConfig(proxyConfig, config); var proxiesList = Object.keys(proxies).sort().reverse(); if (!proxiesList.length) { @@ -126,5 +125,5 @@ var createProxyHandler = function(proxy, proxyConfig, proxyValidateSSL, urlRoot) exports.create = function(/* config */ config, /* config.proxies */ proxies, /* config.proxyValidateSSL */ validateSSL) { return createProxyHandler(new httpProxy.RoutingProxy({changeOrigin: true}), - proxies, validateSSL, config.urlRoot); + proxies, validateSSL, config.urlRoot, config); }; diff --git a/test/e2e/proxy/foo.js b/test/e2e/proxy/foo.js new file mode 100644 index 000000000..090813905 --- /dev/null +++ b/test/e2e/proxy/foo.js @@ -0,0 +1 @@ +'/base/foo.js source' diff --git a/test/e2e/proxy/karma.conf.js b/test/e2e/proxy/karma.conf.js new file mode 100644 index 000000000..23f00ad4d --- /dev/null +++ b/test/e2e/proxy/karma.conf.js @@ -0,0 +1,27 @@ +module.exports = function(config) { + config.set({ + frameworks: ['jasmine'], + + files: [ + '*.js' + ], + + // Test local proxying, with non-default port. + port: 9877, + proxies: { + '/foo.js': '/base/foo.js' + }, + + autoWatch: true, + + browsers: [process.env.TRAVIS ? 'Firefox' : 'Chrome'], + + reporters: ['dots'], + + plugins: [ + 'karma-jasmine', + 'karma-chrome-launcher', + 'karma-firefox-launcher' + ], + }); +}; diff --git a/test/e2e/proxy/test.js b/test/e2e/proxy/test.js new file mode 100644 index 000000000..7eed76f19 --- /dev/null +++ b/test/e2e/proxy/test.js @@ -0,0 +1,14 @@ +function httpGet(url) { + var xmlHttp = new XMLHttpRequest(); + + xmlHttp.open('GET', url, false); + xmlHttp.send(null); + + return xmlHttp.responseText; +} + +describe('foo', function() { + it('should should serve /foo.js', function() { + expect(httpGet('/foo.js')).toBe("'/base/foo.js source'\n"); + }); +}); diff --git a/test/unit/middleware/proxy.spec.coffee b/test/unit/middleware/proxy.spec.coffee index 8cbddc337..84213a085 100644 --- a/test/unit/middleware/proxy.spec.coffee +++ b/test/unit/middleware/proxy.spec.coffee @@ -148,17 +148,19 @@ describe 'middleware.proxy', -> it 'should handle proxy configs with only basepaths', -> proxy = {'/base': '/proxy/test'} - parsedProxyConfig = m.parseProxyConfig proxy + config = {port: 9877, hostname: 'localhost'} + parsedProxyConfig = m.parseProxyConfig proxy, config expect(parsedProxyConfig).to.deep.equal { - '/base': {host: c.DEFAULT_HOSTNAME, port: c.DEFAULT_PORT, + '/base': {host: config.hostname, port: config.port, baseProxyUrl: '/proxy/test', https:false} } it 'should normalize proxy url with only basepaths', -> proxy = {'/base/': '/proxy/test'} - parsedProxyConfig = m.parseProxyConfig proxy + config = {port: 9877, hostname: 'localhost'} + parsedProxyConfig = m.parseProxyConfig proxy, config expect(parsedProxyConfig).to.deep.equal { - '/base/': {host: c.DEFAULT_HOSTNAME, port: c.DEFAULT_PORT, + '/base/': {host: config.hostname, port: config.port, baseProxyUrl: '/proxy/test/', https:false} }