Skip to content

Commit

Permalink
fix: ignore proxy when bypass return false (#1696)
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic authored and evilebottnawi committed Mar 19, 2019
1 parent 34a4a26 commit aa7de77
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
22 changes: 15 additions & 7 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,22 @@ class Server {
}
}

const bypass = typeof proxyConfig.bypass === 'function';

const bypassUrl =
(bypass && proxyConfig.bypass(req, res, proxyConfig)) || false;

if (bypassUrl) {
// - Check if we have a bypass function defined
// - In case the bypass function is defined we'll retrieve the
// bypassUrl from it otherwise byPassUrl would be null
const isByPassFuncDefined =
typeof proxyConfig.bypass === 'function';
const bypassUrl = isByPassFuncDefined
? proxyConfig.bypass(req, res, proxyConfig)
: null;

if (typeof bypassUrl === 'boolean') {
// skip the proxy
req.url = null;
next();
} else if (typeof bypassUrl === 'string') {
// byPass to that url
req.url = bypassUrl;

next();
} else if (proxyMiddleware) {
return proxyMiddleware(req, res, next);
Expand Down
13 changes: 13 additions & 0 deletions test/Proxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ const proxyOptionPathsAsProperties = {
if (/\.html$/.test(req.path)) {
return '/index.html';
}

return null;
},
},
'/proxyfalse': {
bypass(req) {
if (/\/proxyfalse$/.test(req.path)) {
return false;
}
},
},
};
Expand Down Expand Up @@ -116,6 +125,10 @@ describe('Proxy', () => {
it('should pass through a proxy when a bypass function returns null', (done) => {
req.get('/foo.js').expect(200, /Hey/, done);
});

it('should not pass through a proxy when a bypass function returns false', (done) => {
req.get('/proxyfalse').expect(404, done);
});
});
});

Expand Down

0 comments on commit aa7de77

Please sign in to comment.