Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow single object proxy config #1633

Merged
merged 3 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 23 additions & 19 deletions lib/Server.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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