Skip to content

Commit

Permalink
fix: allow single object proxy config (#1633)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebro authored and evilebottnawi committed Feb 19, 2019
1 parent 017dc3d commit 252ea4f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 23 deletions.
42 changes: 23 additions & 19 deletions lib/Server.js
Expand Up @@ -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) => {
Expand Down
43 changes: 39 additions & 4 deletions test/Proxy.test.js
Expand Up @@ -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',
},
Expand All @@ -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',
Expand Down Expand Up @@ -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;
Expand All @@ -79,7 +84,7 @@ describe('Proxy', () => {
config,
{
contentBase,
proxy: proxyOption,
proxy: proxyOptionPathsAsProperties,
},
done
);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 252ea4f

Please sign in to comment.