From 311b3307d2843a4d9fe571cf7c09bb417f5095e4 Mon Sep 17 00:00:00 2001 From: Jean-Charles Sisk Date: Mon, 16 Jul 2018 18:15:36 -0400 Subject: [PATCH 1/3] fix test for async resolvers - #135 --- test/test_custom_resolver.js | 59 ++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/test/test_custom_resolver.js b/test/test_custom_resolver.js index 4a807ec..c4d24de 100644 --- a/test/test_custom_resolver.js +++ b/test/test_custom_resolver.js @@ -228,30 +228,49 @@ describe("Custom Resolver", function(){ }); it("Should resolve array properly as expected", function () { - var proxy = new Redbird(opts), resolver = function (host, url) { - return url.match(/\/ignore/i) ? null : 'http://172.12.0.1/home' - }, customResolver = function (host, url) { + var proxy = new Redbird(opts); + + var firstResolver = function (host, url) { + if (url.endsWith('/first-resolver')) { + return 'http://first-resolver/'; + } + }; + firstResolver.priority = 2; + + var secondResolver = function (host, url) { return new Promise(function(resolve, reject) { - setTimeout(function(){ - var r; - r = url.match(/\/array/i) ? 'http://172.12.0.2/array' : 'http://172.12.0.1/home'; - resolve(r); - },500); + if (url.endsWith('/second-resolver')) { + resolve('http://second-resolver/'); + } else { + resolve(null); + } }); - }, result; + }; + secondResolver.priority = 1; + proxy.resolvers = []; // remove the defaultResolver + proxy.addResolver(firstResolver); + proxy.addResolver(secondResolver); + const cases = [ + proxy.resolve('mysite.example.com', '/first-resolver') + .then(function(result) { + expect(result.urls.length).to.be.above(0); + expect(result.urls[0].hostname).to.be.eq('first-resolver'); + }), + proxy.resolve('mysite.example.com', '/second-resolver') + .then(function(result) { + expect(result.urls.length).to.be.above(0); + expect(result.urls[0].hostname).to.be.eq('second-resolver'); + }) + ]; - proxy.register('mysite.example.com', 'http://127.0.0.1:9999'); - proxy.addResolver(resolver); - proxy.addResolver(customResolver); - // must match the resolver - return proxy.resolve('randomsite.example.com', '/array') - .then(function(result) { - expect(result.urls.length).to.be.above(0); - expect(result.urls[0].hostname).to.be.eq('172.12.0.2'); - return proxy.resolve('randomsite.example.com', '/anywhere') - proxy.close(); - }); + return Promise.all(cases).then( + () => proxy.close(), + err => { + proxy.close(); + throw err; + } + ); }); }); From 43711f4c607c8a00011c85e00c873cdb5d9a1dca Mon Sep 17 00:00:00 2001 From: Jean-Charles Sisk Date: Mon, 16 Jul 2018 18:47:16 -0400 Subject: [PATCH 2/3] fix test for first async resolvers impl - #126 --- test/test_custom_resolver.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_custom_resolver.js b/test/test_custom_resolver.js index c4d24de..619f0e0 100644 --- a/test/test_custom_resolver.js +++ b/test/test_custom_resolver.js @@ -178,7 +178,7 @@ describe("Custom Resolver", function(){ return proxy.resolve('mysite.example.com', '/ignore'); }) .then(function (result) { - expect(result).to.be.undefined; + expect(result.urls[0].hostname).to.be.eq('127.0.0.1'); // make custom resolver low priority and test. // result should match default resolver From 5b48e38b2a92bcdc26d71abc2c470882af0c2c7d Mon Sep 17 00:00:00 2001 From: Jean-Charles Sisk Date: Mon, 16 Jul 2018 18:48:02 -0400 Subject: [PATCH 3/3] fix custom resolvers loop logic --- lib/proxy.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/proxy.js b/lib/proxy.js index 5a1866c..776bab5 100755 --- a/lib/proxy.js +++ b/lib/proxy.js @@ -533,13 +533,16 @@ ReverseProxy.prototype.resolve = function (host, url, req) { promiseArray.push(this.resolvers[i].call(this,host, url, req)); } - return Promise.all(promiseArray).then(function (data) { - var route = data[0]; - if (route && (route = ReverseProxy.buildRoute(route))) { - // ensure resolved route has path that prefixes URL - // no need to check for native routes. - if (!route.isResolved || route.path === '/' || startsWith(url, route.path)) { - return route; + return Promise.all(promiseArray).then(function (resolverResults) { + while (resolverResults.length) { + var route = resolverResults.shift(); + + if (route && (route = ReverseProxy.buildRoute(route))) { + // ensure resolved route has path that prefixes URL + // no need to check for native routes. + if (!route.isResolved || route.path === '/' || startsWith(url, route.path)) { + return route; + } } } })