Skip to content

Commit

Permalink
feat(proxy): Allow proxies configuration to be an object
Browse files Browse the repository at this point in the history
Motivation: allow development of extended configuration per proxy
Example:
  proxies: {
    '/foo': 'http://foo.localhost',
    '/bar': {target: 'http://bar.localhost'}
  }
  • Loading branch information
Volune committed Dec 2, 2015
1 parent 8f61eb2 commit ad94356
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/middleware/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ var parseProxyConfig = function (proxies, config) {
return []
}

return _.sortBy(_.map(proxies, function (proxyUrl, proxyPath) {
return _.sortBy(_.map(proxies, function (proxyConfiguration, proxyPath) {
if (typeof proxyConfiguration === 'string') {
proxyConfiguration = {target: proxyConfiguration}
}
var proxyUrl = proxyConfiguration.target
var proxyDetails = url.parse(proxyUrl)
var pathname = proxyDetails.pathname

Expand Down
18 changes: 17 additions & 1 deletion test/unit/middleware/proxy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe('middleware.proxy', () => {
expect(parsedProxyConfig[0].proxy).to.exist
})

it('should set defualt http port', () => {
it('should set default http port', () => {
var proxy = {'/base/': 'http://localhost/'}
var parsedProxyConfig = m.parseProxyConfig(proxy, {})
expect(parsedProxyConfig).to.have.length(1)
Expand Down Expand Up @@ -291,6 +291,22 @@ describe('middleware.proxy', () => {
expect(parsedProxyConfig[1].proxy).to.exist
})

it('should accept object for proxy config', () => {
var proxy = {
'/base/': {target: 'http://localhost:8000/'}
}
var parsedProxyConfig = m.parseProxyConfig(proxy, {})
expect(parsedProxyConfig).to.have.length(1)
expect(parsedProxyConfig[0]).to.containSubset({
host: 'localhost',
port: '8000',
baseUrl: '/',
path: '/base/',
https: false
})
expect(parsedProxyConfig[0].proxy).to.exist
})

it('should handle empty proxy config', () => {
expect(m.parseProxyConfig({})).to.deep.equal([])
})
Expand Down

0 comments on commit ad94356

Please sign in to comment.