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; + } } } }) diff --git a/test/test_custom_resolver.js b/test/test_custom_resolver.js index 4a807ec..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 @@ -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; + } + ); }); });