Skip to content

Commit a483636

Browse files
committed
fix(proxy): proxy to correct port
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.
1 parent 449b0f5 commit a483636

5 files changed

Lines changed: 54 additions & 11 deletions

File tree

lib/middleware/proxy.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ var url = require('url');
22
var httpProxy = require('http-proxy');
33

44
var log = require('../logger').create('proxy');
5-
var constant = require('../constants');
65

7-
var parseProxyConfig = function(proxies) {
6+
var parseProxyConfig = function(proxies, config) {
87
var proxyConfig = {};
98
var endsWithSlash = function(str) {
109
return str.substr(-1) === '/';
@@ -45,8 +44,8 @@ var parseProxyConfig = function(proxies) {
4544

4645
if (!proxyConfig[proxyPath].port) {
4746
if (!proxyConfig[proxyPath].host) {
48-
proxyConfig[proxyPath].host = constant.DEFAULT_HOSTNAME;
49-
proxyConfig[proxyPath].port = constant.DEFAULT_PORT;
47+
proxyConfig[proxyPath].host = config.hostname;
48+
proxyConfig[proxyPath].port = config.port;
5049
} else {
5150
proxyConfig[proxyPath].port = proxyConfig[proxyPath].https ? '443' : '80';
5251
}
@@ -63,8 +62,8 @@ var parseProxyConfig = function(proxies) {
6362
* @param proxies a map of routes to proxy url
6463
* @return {Function} handler function
6564
*/
66-
var createProxyHandler = function(proxy, proxyConfig, proxyValidateSSL, urlRoot) {
67-
var proxies = parseProxyConfig(proxyConfig);
65+
var createProxyHandler = function(proxy, proxyConfig, proxyValidateSSL, urlRoot, config) {
66+
var proxies = parseProxyConfig(proxyConfig, config);
6867
var proxiesList = Object.keys(proxies).sort().reverse();
6968

7069
if (!proxiesList.length) {
@@ -126,5 +125,5 @@ var createProxyHandler = function(proxy, proxyConfig, proxyValidateSSL, urlRoot)
126125
exports.create = function(/* config */ config, /* config.proxies */ proxies,
127126
/* config.proxyValidateSSL */ validateSSL) {
128127
return createProxyHandler(new httpProxy.RoutingProxy({changeOrigin: true}),
129-
proxies, validateSSL, config.urlRoot);
128+
proxies, validateSSL, config.urlRoot, config);
130129
};

test/e2e/proxy/foo.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'/base/foo.js source'

test/e2e/proxy/karma.conf.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module.exports = function(config) {
2+
config.set({
3+
frameworks: ['jasmine'],
4+
5+
files: [
6+
'*.js'
7+
],
8+
9+
// Test local proxying, with non-default port.
10+
port: 9877,
11+
proxies: {
12+
'/foo.js': '/base/foo.js'
13+
},
14+
15+
autoWatch: true,
16+
17+
browsers: [process.env.TRAVIS ? 'Firefox' : 'Chrome'],
18+
19+
reporters: ['dots'],
20+
21+
plugins: [
22+
'karma-jasmine',
23+
'karma-chrome-launcher',
24+
'karma-firefox-launcher'
25+
],
26+
});
27+
};

test/e2e/proxy/test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function httpGet(url) {
2+
var xmlHttp = new XMLHttpRequest();
3+
4+
xmlHttp.open('GET', url, false);
5+
xmlHttp.send(null);
6+
7+
return xmlHttp.responseText;
8+
}
9+
10+
describe('foo', function() {
11+
it('should should serve /foo.js', function() {
12+
expect(httpGet('/foo.js')).toBe("'/base/foo.js source'\n");
13+
});
14+
});

test/unit/middleware/proxy.spec.coffee

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,19 @@ describe 'middleware.proxy', ->
148148

149149
it 'should handle proxy configs with only basepaths', ->
150150
proxy = {'/base': '/proxy/test'}
151-
parsedProxyConfig = m.parseProxyConfig proxy
151+
config = {port: 9877, hostname: 'localhost'}
152+
parsedProxyConfig = m.parseProxyConfig proxy, config
152153
expect(parsedProxyConfig).to.deep.equal {
153-
'/base': {host: c.DEFAULT_HOSTNAME, port: c.DEFAULT_PORT,
154+
'/base': {host: config.hostname, port: config.port,
154155
baseProxyUrl: '/proxy/test', https:false}
155156
}
156157

157158
it 'should normalize proxy url with only basepaths', ->
158159
proxy = {'/base/': '/proxy/test'}
159-
parsedProxyConfig = m.parseProxyConfig proxy
160+
config = {port: 9877, hostname: 'localhost'}
161+
parsedProxyConfig = m.parseProxyConfig proxy, config
160162
expect(parsedProxyConfig).to.deep.equal {
161-
'/base/': {host: c.DEFAULT_HOSTNAME, port: c.DEFAULT_PORT,
163+
'/base/': {host: config.hostname, port: config.port,
162164
baseProxyUrl: '/proxy/test/', https:false}
163165
}
164166

0 commit comments

Comments
 (0)