Skip to content

Commit

Permalink
Merge pull request #164 from jasisk/jcs/fix-custom-resolvers
Browse files Browse the repository at this point in the history
fix broken custom resolver logic introduced with async resolvers
  • Loading branch information
manast authored Jul 17, 2018
2 parents ccbf1e0 + 5b48e38 commit e4d3e02
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
17 changes: 10 additions & 7 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
})
Expand Down
61 changes: 40 additions & 21 deletions test/test_custom_resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
);
});
});

0 comments on commit e4d3e02

Please sign in to comment.