Skip to content

Commit 252ea4f

Browse files
lukebroevilebottnawi
authored andcommitted
fix: allow single object proxy config (#1633)
1 parent 017dc3d commit 252ea4f

File tree

2 files changed

+62
-23
lines changed

2 files changed

+62
-23
lines changed

lib/Server.js

+23-19
Original file line numberDiff line numberDiff line change
@@ -307,27 +307,31 @@ class Server {
307307
* }
308308
*/
309309
if (!Array.isArray(options.proxy)) {
310-
options.proxy = Object.keys(options.proxy).map((context) => {
311-
let proxyOptions;
312-
// For backwards compatibility reasons.
313-
const correctedContext = context
314-
.replace(/^\*$/, '**')
315-
.replace(/\/\*$/, '');
316-
317-
if (typeof options.proxy[context] === 'string') {
318-
proxyOptions = {
319-
context: correctedContext,
320-
target: options.proxy[context],
321-
};
322-
} else {
323-
proxyOptions = Object.assign({}, options.proxy[context]);
324-
proxyOptions.context = correctedContext;
325-
}
310+
if (Object.prototype.hasOwnProperty.call(options.proxy, 'target')) {
311+
options.proxy = [options.proxy];
312+
} else {
313+
options.proxy = Object.keys(options.proxy).map((context) => {
314+
let proxyOptions;
315+
// For backwards compatibility reasons.
316+
const correctedContext = context
317+
.replace(/^\*$/, '**')
318+
.replace(/\/\*$/, '');
319+
320+
if (typeof options.proxy[context] === 'string') {
321+
proxyOptions = {
322+
context: correctedContext,
323+
target: options.proxy[context],
324+
};
325+
} else {
326+
proxyOptions = Object.assign({}, options.proxy[context]);
327+
proxyOptions.context = correctedContext;
328+
}
326329

327-
proxyOptions.logLevel = proxyOptions.logLevel || 'warn';
330+
proxyOptions.logLevel = proxyOptions.logLevel || 'warn';
328331

329-
return proxyOptions;
330-
});
332+
return proxyOptions;
333+
});
334+
}
331335
}
332336

333337
const getProxyMiddleware = (proxyConfig) => {

test/Proxy.test.js

+39-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const shouldSkipTestSuite = require('./shouldSkipTestSuite');
1111
const WebSocketServer = WebSocket.Server;
1212
const contentBase = path.join(__dirname, 'fixtures/proxy-config');
1313

14-
const proxyOption = {
14+
const proxyOptionPathsAsProperties = {
1515
'/proxy1': {
1616
target: 'http://localhost:9000',
1717
},
@@ -28,8 +28,13 @@ const proxyOption = {
2828
},
2929
};
3030

31+
const proxyOption = {
32+
context: () => true,
33+
target: 'http://localhost:9000',
34+
};
35+
3136
const proxyOptionOfArray = [
32-
{ context: '/proxy1', target: proxyOption['/proxy1'].target },
37+
{ context: '/proxy1', target: proxyOption.target },
3338
function proxy() {
3439
return {
3540
context: '/api/proxy2',
@@ -68,7 +73,7 @@ describe('Proxy', () => {
6873
return;
6974
}
7075

71-
describe('proxy options is a object', () => {
76+
describe('proxy options is an object of paths as properties', () => {
7277
let server;
7378
let req;
7479
let closeProxyServers;
@@ -79,7 +84,7 @@ describe('Proxy', () => {
7984
config,
8085
{
8186
contentBase,
82-
proxy: proxyOption,
87+
proxy: proxyOptionPathsAsProperties,
8388
},
8489
done
8590
);
@@ -120,6 +125,36 @@ describe('Proxy', () => {
120125
});
121126
});
122127

128+
describe('proxy option is an object', () => {
129+
let server;
130+
let req;
131+
let closeProxyServers;
132+
133+
beforeAll((done) => {
134+
closeProxyServers = startProxyServers();
135+
server = helper.start(
136+
config,
137+
{
138+
contentBase,
139+
proxy: proxyOption,
140+
},
141+
done
142+
);
143+
req = request(server.app);
144+
});
145+
146+
afterAll((done) => {
147+
helper.close(() => {
148+
closeProxyServers();
149+
done();
150+
});
151+
});
152+
153+
it('respects a proxy option', (done) => {
154+
req.get('/proxy1').expect(200, 'from proxy1', done);
155+
});
156+
});
157+
123158
describe('proxy option is an array', () => {
124159
let server;
125160
let req;

0 commit comments

Comments
 (0)