diff --git a/lib/Server.js b/lib/Server.js index c8126e320b..11e5f67d3d 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -307,27 +307,31 @@ class Server { * } */ if (!Array.isArray(options.proxy)) { - options.proxy = Object.keys(options.proxy).map((context) => { - let proxyOptions; - // For backwards compatibility reasons. - const correctedContext = context - .replace(/^\*$/, '**') - .replace(/\/\*$/, ''); - - if (typeof options.proxy[context] === 'string') { - proxyOptions = { - context: correctedContext, - target: options.proxy[context], - }; - } else { - proxyOptions = Object.assign({}, options.proxy[context]); - proxyOptions.context = correctedContext; - } + if (Object.prototype.hasOwnProperty.call(options.proxy, 'target')) { + options.proxy = [options.proxy]; + } else { + options.proxy = Object.keys(options.proxy).map((context) => { + let proxyOptions; + // For backwards compatibility reasons. + const correctedContext = context + .replace(/^\*$/, '**') + .replace(/\/\*$/, ''); + + if (typeof options.proxy[context] === 'string') { + proxyOptions = { + context: correctedContext, + target: options.proxy[context], + }; + } else { + proxyOptions = Object.assign({}, options.proxy[context]); + proxyOptions.context = correctedContext; + } - proxyOptions.logLevel = proxyOptions.logLevel || 'warn'; + proxyOptions.logLevel = proxyOptions.logLevel || 'warn'; - return proxyOptions; - }); + return proxyOptions; + }); + } } const getProxyMiddleware = (proxyConfig) => { diff --git a/test/Proxy.test.js b/test/Proxy.test.js index e19716be19..b01998ccf1 100644 --- a/test/Proxy.test.js +++ b/test/Proxy.test.js @@ -11,7 +11,7 @@ const shouldSkipTestSuite = require('./shouldSkipTestSuite'); const WebSocketServer = WebSocket.Server; const contentBase = path.join(__dirname, 'fixtures/proxy-config'); -const proxyOption = { +const proxyOptionPathsAsProperties = { '/proxy1': { target: 'http://localhost:9000', }, @@ -28,8 +28,13 @@ const proxyOption = { }, }; +const proxyOption = { + context: () => true, + target: 'http://localhost:9000', +}; + const proxyOptionOfArray = [ - { context: '/proxy1', target: proxyOption['/proxy1'].target }, + { context: '/proxy1', target: proxyOption.target }, function proxy() { return { context: '/api/proxy2', @@ -68,7 +73,7 @@ describe('Proxy', () => { return; } - describe('proxy options is a object', () => { + describe('proxy options is an object of paths as properties', () => { let server; let req; let closeProxyServers; @@ -79,7 +84,7 @@ describe('Proxy', () => { config, { contentBase, - proxy: proxyOption, + proxy: proxyOptionPathsAsProperties, }, done ); @@ -120,6 +125,36 @@ describe('Proxy', () => { }); }); + describe('proxy option is an object', () => { + let server; + let req; + let closeProxyServers; + + beforeAll((done) => { + closeProxyServers = startProxyServers(); + server = helper.start( + config, + { + contentBase, + proxy: proxyOption, + }, + done + ); + req = request(server.app); + }); + + afterAll((done) => { + helper.close(() => { + closeProxyServers(); + done(); + }); + }); + + it('respects a proxy option', (done) => { + req.get('/proxy1').expect(200, 'from proxy1', done); + }); + }); + describe('proxy option is an array', () => { let server; let req;