Skip to content

Commit

Permalink
fix(proxy): proxy to correct port
Browse files Browse the repository at this point in the history
When port is not defined in the `proxies` config, i.e. when proxying to
Karma server itself, the proxy needs to use the `config.port`
instead of the default port.
  • Loading branch information
vojtajina committed Oct 9, 2014
1 parent 449b0f5 commit a483636
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
13 changes: 6 additions & 7 deletions lib/middleware/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) === '/';
Expand Down Expand Up @@ -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';
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
};
1 change: 1 addition & 0 deletions test/e2e/proxy/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'/base/foo.js source'
27 changes: 27 additions & 0 deletions test/e2e/proxy/karma.conf.js
Original file line number Diff line number Diff line change
@@ -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'
],
});
};
14 changes: 14 additions & 0 deletions test/e2e/proxy/test.js
Original file line number Diff line number Diff line change
@@ -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");
});
});
10 changes: 6 additions & 4 deletions test/unit/middleware/proxy.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}

Expand Down

0 comments on commit a483636

Please sign in to comment.