From aa7de773bf363d437f96fdf1fba39f02c0abb805 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Tue, 19 Mar 2019 09:47:22 +0000 Subject: [PATCH] fix: ignore proxy when bypass return false (#1696) --- lib/Server.js | 22 +++++++++++++++------- test/Proxy.test.js | 13 +++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/Server.js b/lib/Server.js index 07c19ff54c..cea529db1c 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -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); diff --git a/test/Proxy.test.js b/test/Proxy.test.js index 5791f868a3..45090458a6 100644 --- a/test/Proxy.test.js +++ b/test/Proxy.test.js @@ -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; + } }, }, }; @@ -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); + }); }); });