FirstCommit
This commit is contained in:
219
test/test_custom_resolver.js
Normal file
219
test/test_custom_resolver.js
Normal file
@ -0,0 +1,219 @@
|
||||
"use strict";
|
||||
|
||||
var Redbird = require('../');
|
||||
var expect = require('chai').expect;
|
||||
var _ = require('lodash');
|
||||
|
||||
var opts = {
|
||||
bunyan: false,
|
||||
port: 10000 + Math.ceil(Math.random() * 55535)
|
||||
/* {
|
||||
name: 'test',
|
||||
streams: [{
|
||||
path: '/dev/null',
|
||||
}]
|
||||
} */
|
||||
};
|
||||
|
||||
|
||||
describe("Custom Resolver", function(){
|
||||
|
||||
it("Should contain one resolver by default", function () {
|
||||
|
||||
var redbird = Redbird(opts);
|
||||
expect(redbird.resolvers).to.be.an('array');
|
||||
expect(redbird.resolvers.length).to.be.eq(1);
|
||||
expect(redbird.resolvers[0]).to.be.eq(redbird._defaultResolver);
|
||||
|
||||
redbird.close();
|
||||
});
|
||||
|
||||
it("Should register resolver with right priority", function(){
|
||||
var resolver = function () {
|
||||
return 'http://127.0.0.1:8080';
|
||||
};
|
||||
|
||||
resolver.priority = 1;
|
||||
|
||||
var options = _.extend({
|
||||
resolvers: resolver
|
||||
}, opts);
|
||||
|
||||
var redbird = Redbird(options);
|
||||
|
||||
expect(redbird.resolvers.length).to.be.eq(2);
|
||||
expect(redbird.resolvers[0]).to.be.eql(resolver);
|
||||
|
||||
redbird.close();
|
||||
|
||||
|
||||
// test when an array is sent in as resolvers.
|
||||
options.resolvers = [resolver];
|
||||
redbird = new Redbird(options);
|
||||
expect(redbird.resolvers.length).to.be.eq(2);
|
||||
expect(redbird.resolvers[0]).to.be.eql(resolver);
|
||||
redbird.close();
|
||||
|
||||
resolver.priority = -1;
|
||||
redbird = new Redbird(options);
|
||||
expect(redbird.resolvers.length).to.be.eq(2);
|
||||
expect(redbird.resolvers[1]).to.be.eql(resolver);
|
||||
redbird.close();
|
||||
|
||||
|
||||
// test when invalid resolver is added
|
||||
options.resolvers = {};
|
||||
expect(function () {
|
||||
new Redbird(options)
|
||||
}).to.throw(Error);
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
it('Should add and remove resolver after launch', function () {
|
||||
|
||||
var resolver = function () {};
|
||||
resolver.priority = 1;
|
||||
|
||||
var redbird = Redbird(opts);
|
||||
redbird.addResolver(resolver);
|
||||
expect(redbird.resolvers.length).to.be.eq(2);
|
||||
expect(redbird.resolvers[0]).to.be.eq(resolver);
|
||||
|
||||
redbird.addResolver(resolver);
|
||||
expect(redbird.resolvers.length, 'Only allows uniques.').to.be.eq(2);
|
||||
|
||||
|
||||
redbird.removeResolver(resolver);
|
||||
expect(redbird.resolvers.length).to.be.eq(1);
|
||||
expect(redbird.resolvers[0]).to.be.eq(redbird._defaultResolver);
|
||||
|
||||
redbird.close();
|
||||
|
||||
});
|
||||
|
||||
|
||||
it('Should properly convert and cache route to routeObject', function () {
|
||||
|
||||
var builder = Redbird.buildRoute;
|
||||
|
||||
// invalid input
|
||||
expect(builder(function () {})).to.be.null;
|
||||
expect(builder([])).to.be.null;
|
||||
expect(builder(2016)).to.be.null;
|
||||
|
||||
var testRoute = {urls: [], path: '/'};
|
||||
var testRouteResult = builder(testRoute);
|
||||
expect(testRouteResult, 'For route in the default format').to.be.eq(testRoute);
|
||||
expect(testRouteResult.isResolved).to.be.undefined;
|
||||
|
||||
|
||||
// case string:
|
||||
var testString = 'http://127.0.0.1:8888';
|
||||
var result = builder(testString);
|
||||
expect(result.path).to.be.eq('/');
|
||||
expect(result.urls).to.be.an('array');
|
||||
expect(result.urls.length).to.be.eq(1);
|
||||
expect(result.urls[0].hostname).to.be.eq('127.0.0.1');
|
||||
expect(result.isResolved).to.be.true;
|
||||
|
||||
|
||||
var result2 = builder(testString);
|
||||
expect(result2).to.be.eq(result);
|
||||
|
||||
// case with object
|
||||
|
||||
var testObject_1= {path:'/api', url: 'http://127.0.0.1'},
|
||||
testObjectResult_1 = builder(testObject_1);
|
||||
|
||||
expect(testObjectResult_1.path).to.be.eq('/api');
|
||||
expect(testObjectResult_1.urls).to.be.an('array');
|
||||
expect(testObjectResult_1.urls.length).to.be.eq(1);
|
||||
expect(testObjectResult_1.urls[0].hostname).to.be.eq('127.0.0.1');
|
||||
expect(testObjectResult_1.isResolved).to.be.true;
|
||||
|
||||
|
||||
// test object caching.
|
||||
var testObjectResult_2 = builder(testObject_1);
|
||||
expect(testObjectResult_1).to.be.eq(testObjectResult_2);
|
||||
|
||||
var testObject_2= {url: ['http://127.0.0.1', 'http://123.1.1.1']}
|
||||
var testResult2 = builder(testObject_2);
|
||||
expect(testResult2.urls).to.not.be.undefined;
|
||||
expect(testResult2.urls.length).to.be.eq(testObject_2.url.length);
|
||||
expect(testResult2.urls[0].hostname).to.be.eq('127.0.0.1');
|
||||
expect(testResult2.urls[1].hostname).to.be.eq('123.1.1.1');
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
it("Should resolve 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'
|
||||
}, result;
|
||||
|
||||
resolver.priority = 1;
|
||||
|
||||
proxy.register('mysite.example.com', 'http://127.0.0.1:9999');
|
||||
proxy.addResolver(resolver);
|
||||
|
||||
result = proxy.resolve('randomsite.example.com', '/anywhere');
|
||||
|
||||
// must match the resolver
|
||||
expect(result).to.not.be.null;
|
||||
expect(result).to.not.be.undefined;
|
||||
expect(result.urls.length).to.be.above(0);
|
||||
expect(result.urls[0].hostname).to.be.eq('172.12.0.1');
|
||||
|
||||
// expect route to match resolver even though it matches registered address
|
||||
result = proxy.resolve('mysite.example.com', '/somewhere');
|
||||
expect(result.urls[0].hostname).to.be.eq('172.12.0.1');
|
||||
|
||||
// use default resolver, as custom resolver should ignore input.
|
||||
result = proxy.resolve('mysite.example.com', '/ignore');
|
||||
expect(result.urls[0].hostname).to.be.eq('127.0.0.1');
|
||||
|
||||
|
||||
// make custom resolver low priority and test.
|
||||
// result should match default resolver
|
||||
resolver.priority = -1;
|
||||
proxy.addResolver(resolver);
|
||||
result = proxy.resolve('mysite.example.com', '/somewhere');
|
||||
expect(result.urls[0].hostname).to.be.eq('127.0.0.1');
|
||||
|
||||
|
||||
// both custom and default resolvers should ignore
|
||||
result = proxy.resolve('somesite.example.com', '/ignore');
|
||||
expect(result).to.be.undefined;
|
||||
|
||||
proxy.removeResolver(resolver);
|
||||
// for path-based routing
|
||||
// when resolver path doesn't match that of url, skip
|
||||
|
||||
resolver = function () {
|
||||
return {
|
||||
path: '/notme',
|
||||
url: 'http://172.12.0.1/home'
|
||||
}
|
||||
};
|
||||
resolver.priority = 1;
|
||||
proxy.addResolver(resolver);
|
||||
|
||||
result = proxy.resolve('somesite.example.com', '/notme');
|
||||
expect(result).to.not.be.undefined;
|
||||
expect(result.urls[0].hostname).to.be.eq('172.12.0.1');
|
||||
|
||||
result = proxy.resolve('somesite.example.com', '/notme/somewhere');
|
||||
expect(result.urls[0].hostname).to.be.eq('172.12.0.1');
|
||||
|
||||
result = proxy.resolve('somesite.example.com', '/itsme/somewhere');
|
||||
expect(result).to.be.undefined;
|
||||
|
||||
|
||||
proxy.close();
|
||||
});
|
||||
|
||||
});
|
141
test/test_hostheader.js
Normal file
141
test/test_hostheader.js
Normal file
@ -0,0 +1,141 @@
|
||||
"use strict";
|
||||
|
||||
var Redbird = require('../');
|
||||
var Promise = require('bluebird');
|
||||
var http = require('http');
|
||||
var expect = require('chai').expect;
|
||||
|
||||
var TEST_PORT = 54674
|
||||
var PROXY_PORT = 53433
|
||||
|
||||
var opts = {
|
||||
port: PROXY_PORT,
|
||||
bunyan: false
|
||||
}
|
||||
|
||||
describe("Target with a hostname", function(){
|
||||
|
||||
it("Should have the host header passed to the target", function(done){
|
||||
var redbird = Redbird(opts);
|
||||
|
||||
expect(redbird.routing).to.be.an("object");
|
||||
|
||||
redbird.register('127.0.0.1', '127.0.0.1.xip.io:'+TEST_PORT, {
|
||||
useTargetHostHeader: true
|
||||
});
|
||||
|
||||
expect(redbird.routing).to.have.property("127.0.0.1");
|
||||
|
||||
testServer().then(function(req){
|
||||
expect(req.headers['host']).to.be.eql('127.0.0.1.xip.io:'+TEST_PORT)
|
||||
})
|
||||
|
||||
http.get('http://127.0.0.1:'+PROXY_PORT, function(res) {
|
||||
redbird.close();
|
||||
done();
|
||||
});
|
||||
|
||||
})
|
||||
|
||||
it("Should not have the host header passed to the target", function(done){
|
||||
var redbird = Redbird(opts);
|
||||
|
||||
expect(redbird.routing).to.be.an("object");
|
||||
|
||||
redbird.register('127.0.0.1', '127.0.0.1.xip.io:'+TEST_PORT);
|
||||
|
||||
expect(redbird.routing).to.have.property("127.0.0.1");
|
||||
|
||||
testServer().then(function(req){
|
||||
expect(req.headers['host']).to.be.eql('127.0.0.1:'+PROXY_PORT)
|
||||
})
|
||||
|
||||
http.get('http://127.0.0.1:'+PROXY_PORT, function(res) {
|
||||
redbird.close();
|
||||
done();
|
||||
});
|
||||
|
||||
})
|
||||
|
||||
it("Should return 404 after route is unregister", function(done){
|
||||
var redbird = Redbird(opts);
|
||||
|
||||
expect(redbird.routing).to.be.an("object");
|
||||
|
||||
redbird.register('127.0.0.1', '127.0.0.1.xip.io:'+TEST_PORT);
|
||||
redbird.unregister('127.0.0.1', '127.0.0.1.xip.io:'+TEST_PORT);
|
||||
|
||||
expect(redbird.routing).to.have.property("127.0.0.1");
|
||||
|
||||
testServer().then(function(req){
|
||||
expect(req.headers['host']).to.be.eql('127.0.0.1:'+PROXY_PORT)
|
||||
})
|
||||
|
||||
http.get('http://127.0.0.1:'+PROXY_PORT, function(res) {
|
||||
expect(res.statusCode).to.be.eql(404);
|
||||
|
||||
redbird.close();
|
||||
done();
|
||||
});
|
||||
|
||||
})
|
||||
|
||||
it("Should return 502 after route with no backend", function(done){
|
||||
var redbird = Redbird(opts);
|
||||
|
||||
expect(redbird.routing).to.be.an("object");
|
||||
|
||||
redbird.register('127.0.0.1', '127.0.0.1.xip.io:502');
|
||||
|
||||
expect(redbird.routing).to.have.property("127.0.0.1");
|
||||
|
||||
http.get('http://127.0.0.1:'+PROXY_PORT, function(res) {
|
||||
expect(res.statusCode).to.be.eql(502);
|
||||
|
||||
redbird.close();
|
||||
done();
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
describe("Request with forwarded host header", function () {
|
||||
it("should prefer forwarded hostname if desired", function () {
|
||||
var redbird = Redbird({
|
||||
bunyan: false,
|
||||
preferForwardedHost: true
|
||||
});
|
||||
|
||||
expect(redbird.routing).to.be.an("object");
|
||||
var req = { headers: {host: '127.0.0.1', "x-forwarded-host": "subdomain.example.com"} }
|
||||
|
||||
var source = redbird._getSource(req);
|
||||
expect(source).to.be.eql('subdomain.example.com')
|
||||
|
||||
redbird.close();
|
||||
});
|
||||
|
||||
it("should use original host if not further specified", function () {
|
||||
var redbird = Redbird(opts);
|
||||
|
||||
expect(redbird.routing).to.be.an("object");
|
||||
var req = { headers: { host: '127.0.0.1', "x-forwarded-host": "subdomain.example.com"} }
|
||||
|
||||
var source = redbird._getSource(req);
|
||||
expect(source).to.be.eql('127.0.0.1')
|
||||
|
||||
redbird.close();
|
||||
});
|
||||
});
|
||||
|
||||
function testServer(){
|
||||
return new Promise(function(resolve, reject){
|
||||
var server = http.createServer(function(req, res){
|
||||
res.write("");
|
||||
res.end();
|
||||
resolve(req);
|
||||
server.close();
|
||||
});
|
||||
|
||||
server.listen(TEST_PORT);
|
||||
})
|
||||
}
|
75
test/test_pathnames.js
Normal file
75
test/test_pathnames.js
Normal file
@ -0,0 +1,75 @@
|
||||
"use strict";
|
||||
|
||||
var Redbird = require('../');
|
||||
var Promise = require('bluebird');
|
||||
var http = require('http');
|
||||
var expect = require('chai').expect;
|
||||
|
||||
var TEST_PORT = 54673
|
||||
var PROXY_PORT = 53432
|
||||
|
||||
var opts = {
|
||||
port: PROXY_PORT,
|
||||
bunyan: false /* {
|
||||
name: 'test',
|
||||
streams: [{
|
||||
path: '/dev/null',
|
||||
}]
|
||||
} */
|
||||
}
|
||||
|
||||
describe("Target with pathnames", function(){
|
||||
|
||||
it("Should be proxyed to target with pathname and source pathname concatenated", function(done){
|
||||
var redbird = Redbird(opts);
|
||||
|
||||
expect(redbird.routing).to.be.an("object");
|
||||
|
||||
redbird.register('127.0.0.1', '127.0.0.1:'+TEST_PORT+'/foo/bar/qux');
|
||||
|
||||
expect(redbird.routing).to.have.property("127.0.0.1");
|
||||
|
||||
testServer().then(function(req){
|
||||
expect(req.url).to.be.eql('/foo/bar/qux/a/b/c')
|
||||
})
|
||||
|
||||
http.get('http://127.0.0.1:'+PROXY_PORT+'/a/b/c', function(res) {
|
||||
redbird.close();
|
||||
done();
|
||||
});
|
||||
|
||||
})
|
||||
|
||||
it("Should be proxyed to target with pathname and source pathname concatenated case 2", function(done){
|
||||
var redbird = new Redbird(opts);
|
||||
|
||||
expect(redbird.routing).to.be.an("object");
|
||||
|
||||
redbird.register('127.0.0.1/path', '127.0.0.1:'+TEST_PORT+'/foo/bar/qux');
|
||||
|
||||
expect(redbird.routing).to.have.property("127.0.0.1");
|
||||
|
||||
testServer().then(function(req){
|
||||
expect(req.url).to.be.eql('/foo/bar/qux/a/b/c')
|
||||
})
|
||||
|
||||
http.get('http://127.0.0.1:'+PROXY_PORT+'/path/a/b/c', function(err, res) {
|
||||
redbird.close();
|
||||
done();
|
||||
});
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
function testServer(){
|
||||
return new Promise(function(resolve, reject){
|
||||
var server = http.createServer(function(req, res){
|
||||
res.write("");
|
||||
res.end();
|
||||
resolve(req);
|
||||
server.close();
|
||||
});
|
||||
server.listen(TEST_PORT);
|
||||
})
|
||||
}
|
421
test/test_register.js
Normal file
421
test/test_register.js
Normal file
@ -0,0 +1,421 @@
|
||||
"use strict";
|
||||
|
||||
var Redbird = require('../');
|
||||
var expect = require('chai').expect;
|
||||
|
||||
var opts = {
|
||||
bunyan: false /* {
|
||||
name: 'test',
|
||||
streams: [{
|
||||
path: '/dev/null',
|
||||
}]
|
||||
} */
|
||||
}
|
||||
|
||||
describe("Route registration", function () {
|
||||
it("should register a simple route", function () {
|
||||
var redbird = Redbird(opts);
|
||||
|
||||
expect(redbird.routing).to.be.an("object");
|
||||
|
||||
redbird.register('example.com', '192.168.1.2:8080');
|
||||
|
||||
expect(redbird.routing).to.have.property("example.com")
|
||||
|
||||
expect(redbird.resolve('example.com')).to.be.an("object");
|
||||
|
||||
var host = redbird.routing["example.com"];
|
||||
expect(host).to.be.an("array");
|
||||
expect(host[0]).to.have.property('path')
|
||||
expect(host[0].path).to.be.eql('/');
|
||||
expect(host[0].urls).to.be.an('array');
|
||||
expect(host[0].urls.length).to.be.eql(1);
|
||||
expect(host[0].urls[0].href).to.be.eql('http://192.168.1.2:8080/');
|
||||
|
||||
redbird.unregister('example.com', '192.168.1.2:8080');
|
||||
expect(redbird.resolve('example.com')).to.be.an("undefined")
|
||||
redbird.close();
|
||||
});
|
||||
|
||||
it("should resolve domains as case insensitive", function () {
|
||||
var redbird = Redbird(opts);
|
||||
|
||||
expect(redbird.routing).to.be.an("object");
|
||||
|
||||
redbird.register('example.com', '192.168.1.2:8080');
|
||||
|
||||
var target = redbird.resolve('Example.com');
|
||||
expect(target).to.be.an("object");
|
||||
expect(target.urls[0].hostname).to.be.equal('192.168.1.2');
|
||||
|
||||
redbird.close();
|
||||
});
|
||||
|
||||
|
||||
it("should register multiple routes", function () {
|
||||
var redbird = Redbird(opts);
|
||||
|
||||
expect(redbird.routing).to.be.an("object");
|
||||
|
||||
redbird.register('example1.com', '192.168.1.2:8080');
|
||||
redbird.register('example2.com', '192.168.1.3:8081');
|
||||
redbird.register('example3.com', '192.168.1.4:8082');
|
||||
redbird.register('example4.com', '192.168.1.5:8083');
|
||||
redbird.register('example5.com', '192.168.1.6:8084');
|
||||
|
||||
expect(redbird.routing).to.have.property("example1.com")
|
||||
expect(redbird.routing).to.have.property("example2.com")
|
||||
expect(redbird.routing).to.have.property("example3.com")
|
||||
expect(redbird.routing).to.have.property("example4.com")
|
||||
expect(redbird.routing).to.have.property("example5.com")
|
||||
|
||||
var host;
|
||||
|
||||
host = redbird.routing["example1.com"];
|
||||
expect(host[0].path).to.be.eql('/');
|
||||
expect(host[0].urls[0].href).to.be.eql('http://192.168.1.2:8080/');
|
||||
|
||||
host = redbird.routing["example2.com"];
|
||||
expect(host[0].path).to.be.eql('/');
|
||||
expect(host[0].urls[0].href).to.be.eql('http://192.168.1.3:8081/');
|
||||
|
||||
host = redbird.routing["example3.com"];
|
||||
expect(host[0].path).to.be.eql('/');
|
||||
expect(host[0].urls[0].href).to.be.eql('http://192.168.1.4:8082/');
|
||||
|
||||
host = redbird.routing["example4.com"];
|
||||
expect(host[0].path).to.be.eql('/');
|
||||
expect(host[0].urls[0].href).to.be.eql('http://192.168.1.5:8083/');
|
||||
|
||||
host = redbird.routing["example5.com"];
|
||||
expect(host[0].path).to.be.eql('/');
|
||||
expect(host[0].urls[0].href).to.be.eql('http://192.168.1.6:8084/');
|
||||
|
||||
redbird.unregister('example1.com');
|
||||
expect(redbird.resolve('example1.com')).to.be.an("undefined")
|
||||
|
||||
redbird.unregister('example2.com');
|
||||
expect(redbird.resolve('example2.com')).to.be.an("undefined")
|
||||
|
||||
redbird.unregister('example3.com');
|
||||
expect(redbird.resolve('example3.com')).to.be.an("undefined")
|
||||
|
||||
redbird.unregister('example4.com');
|
||||
expect(redbird.resolve('example4.com')).to.be.an("undefined")
|
||||
|
||||
redbird.unregister('example5.com');
|
||||
expect(redbird.resolve('example5.com')).to.be.an("undefined")
|
||||
|
||||
|
||||
redbird.close();
|
||||
})
|
||||
it("should register several pathnames within a route", function () {
|
||||
var redbird = Redbird(opts);
|
||||
|
||||
expect(redbird.routing).to.be.an("object");
|
||||
|
||||
redbird.register('example.com', '192.168.1.2:8080');
|
||||
redbird.register('example.com/qux/baz', '192.168.1.5:8080');
|
||||
redbird.register('example.com/foo', '192.168.1.3:8080');
|
||||
redbird.register('example.com/bar', '192.168.1.4:8080');
|
||||
|
||||
expect(redbird.routing).to.have.property("example.com")
|
||||
|
||||
var host = redbird.routing["example.com"];
|
||||
expect(host).to.be.an("array");
|
||||
expect(host[0]).to.have.property('path')
|
||||
expect(host[0].path).to.be.eql('/qux/baz');
|
||||
expect(host[0].urls).to.be.an('array');
|
||||
expect(host[0].urls.length).to.be.eql(1);
|
||||
expect(host[0].urls[0].href).to.be.eql('http://192.168.1.5:8080/');
|
||||
|
||||
expect(host[0].path.length).to.be.least(host[1].path.length)
|
||||
expect(host[1].path.length).to.be.least(host[2].path.length)
|
||||
expect(host[2].path.length).to.be.least(host[3].path.length)
|
||||
|
||||
redbird.unregister('example.com');
|
||||
expect(redbird.resolve('example.com')).to.be.an("undefined")
|
||||
|
||||
expect(redbird.resolve('example.com', '/foo')).to.be.an("object")
|
||||
|
||||
redbird.unregister('example.com/foo');
|
||||
expect(redbird.resolve('example.com', '/foo')).to.be.an("undefined")
|
||||
|
||||
redbird.close();
|
||||
})
|
||||
it("shouldnt crash process in unregister of unregisted host", function (done) {
|
||||
var redbird = Redbird(opts);
|
||||
|
||||
redbird.unregister('example.com');
|
||||
|
||||
done()
|
||||
|
||||
redbird.close();
|
||||
})
|
||||
})
|
||||
|
||||
describe("Route resolution", function () {
|
||||
it("should resolve to a correct route", function () {
|
||||
var redbird = Redbird(opts);
|
||||
|
||||
expect(redbird.routing).to.be.an("object");
|
||||
|
||||
redbird.register('example.com', '192.168.1.2:8080');
|
||||
redbird.register('example.com/qux/baz', '192.168.1.5:8080');
|
||||
redbird.register('example.com/foo', '192.168.1.3:8080');
|
||||
redbird.register('example.com/bar', '192.168.1.4:8080');
|
||||
redbird.register('example.com/foo/baz', '192.168.1.3:8080');
|
||||
|
||||
var route = redbird.resolve('example.com', '/foo/asd/1/2');
|
||||
expect(route.path).to.be.eql('/foo')
|
||||
expect(route.urls.length).to.be.eql(1);
|
||||
expect(route.urls[0].href).to.be.eql('http://192.168.1.3:8080/');
|
||||
|
||||
redbird.close();
|
||||
})
|
||||
|
||||
it("should resolve to a correct route with complex path", function () {
|
||||
var redbird = Redbird(opts);
|
||||
|
||||
expect(redbird.routing).to.be.an("object");
|
||||
|
||||
redbird.register('example.com', '192.168.1.2:8080');
|
||||
redbird.register('example.com/qux/baz', '192.168.1.5:8080');
|
||||
redbird.register('example.com/foo', '192.168.1.3:8080');
|
||||
redbird.register('example.com/bar', '192.168.1.4:8080');
|
||||
redbird.register('example.com/foo/baz', '192.168.1.7:8080');
|
||||
|
||||
var route = redbird.resolve('example.com', '/foo/baz/a/b/c');
|
||||
|
||||
expect(route.path).to.be.eql('/foo/baz')
|
||||
expect(route.urls.length).to.be.eql(1);
|
||||
expect(route.urls[0].href).to.be.eql('http://192.168.1.7:8080/');
|
||||
|
||||
redbird.close();
|
||||
})
|
||||
|
||||
it("should resolve to undefined if route not available", function () {
|
||||
var redbird = Redbird(opts);
|
||||
|
||||
expect(redbird.routing).to.be.an("object");
|
||||
|
||||
redbird.register('example.com', '192.168.1.2:8080');
|
||||
redbird.register('example.com/qux/baz', '192.168.1.5:8080');
|
||||
redbird.register('example.com/foo', '192.168.1.3:8080');
|
||||
redbird.register('foobar.com/bar', '192.168.1.4:8080');
|
||||
redbird.register('foobar.com/foo/baz', '192.168.1.3:8080');
|
||||
|
||||
var route = redbird.resolve('wrong.com');
|
||||
expect(route).to.be.an('undefined')
|
||||
|
||||
var route = redbird.resolve('foobar.com');
|
||||
expect(route).to.be.an('undefined')
|
||||
|
||||
redbird.close();
|
||||
})
|
||||
|
||||
it("should get a target if route available", function () {
|
||||
var redbird = Redbird(opts);
|
||||
|
||||
expect(redbird.routing).to.be.an("object");
|
||||
|
||||
redbird.register('example.com', '192.168.1.2:8080');
|
||||
redbird.register('example.com/qux/baz', '192.168.1.5:8080');
|
||||
redbird.register('example.com/foo', '192.168.1.3:8080');
|
||||
redbird.register('foobar.com/bar', '192.168.1.4:8080');
|
||||
redbird.register('foobar.com/foo/baz', '192.168.1.7:8080');
|
||||
redbird.register('foobar.com/media', '192.168.1.7:8080');
|
||||
|
||||
var route = redbird.resolve('example.com', '/qux/a/b/c');
|
||||
expect(route.path).to.be.eql('/');
|
||||
|
||||
var route = redbird.resolve('foobar.com', '/medias/');
|
||||
expect(route).to.be.undefined;
|
||||
|
||||
var route = redbird.resolve('foobar.com', '/mediasa');
|
||||
expect(route).to.be.undefined;
|
||||
|
||||
var route = redbird.resolve('foobar.com', '/media/sa');
|
||||
expect(route.path).to.be.eql('/media');
|
||||
|
||||
var target = redbird._getTarget('example.com', { url: '/foo/baz/a/b/c' });
|
||||
expect(target.href).to.be.eql('http://192.168.1.3:8080/')
|
||||
|
||||
redbird.close();
|
||||
})
|
||||
|
||||
it("should get a target with path when necessary", function () {
|
||||
var redbird = Redbird(opts);
|
||||
|
||||
expect(redbird.routing).to.be.an("object");
|
||||
|
||||
redbird.register('example.com', '192.168.1.2:8080');
|
||||
redbird.register('example.com/qux/baz', '192.168.1.5:8080');
|
||||
redbird.register('example.com/foo', '192.168.1.3:8080/a/b');
|
||||
redbird.register('foobar.com/bar', '192.168.1.4:8080');
|
||||
redbird.register('foobar.com/foo/baz', '192.168.1.7:8080');
|
||||
|
||||
var route = redbird.resolve('example.com', '/qux/a/b/c');
|
||||
expect(route.path).to.be.eql('/');
|
||||
|
||||
var req = { url: '/foo/baz/a/b/c' }
|
||||
var target = redbird._getTarget('example.com', req);
|
||||
expect(target.href).to.be.eql('http://192.168.1.3:8080/a/b')
|
||||
expect(req.url).to.be.eql('/a/b/baz/a/b/c')
|
||||
|
||||
redbird.close();
|
||||
})
|
||||
})
|
||||
|
||||
describe("TLS/SSL", function () {
|
||||
it("should allow TLS/SSL certificates", function () {
|
||||
var redbird = Redbird({
|
||||
ssl: {
|
||||
port: 4430
|
||||
},
|
||||
bunyan: false
|
||||
});
|
||||
|
||||
expect(redbird.routing).to.be.an("object");
|
||||
redbird.register('example.com', '192.168.1.1:8080', {
|
||||
ssl: {
|
||||
key: 'dummy',
|
||||
cert: 'dummy'
|
||||
}
|
||||
});
|
||||
|
||||
redbird.register('example.com', '192.168.1.2:8080');
|
||||
|
||||
expect(redbird.certs).to.be.an("object");
|
||||
expect(redbird.certs['example.com']).to.be.an("object");
|
||||
|
||||
redbird.unregister('example.com', '192.168.1.1:8080');
|
||||
expect(redbird.resolve('example.com')).to.not.be.an("undefined")
|
||||
expect(redbird.certs['example.com']).to.not.be.an("undefined");
|
||||
|
||||
redbird.unregister('example.com', '192.168.1.2:8080');
|
||||
expect(redbird.resolve('example.com')).to.be.an("undefined")
|
||||
expect(redbird.certs['example.com']).to.be.an("undefined");
|
||||
|
||||
})
|
||||
it('Should bind https servers to different ip addresses', function(testDone) {
|
||||
|
||||
var isPortTaken = function(port, ip, done) {
|
||||
var net = require('net')
|
||||
var tester = net.createServer()
|
||||
.once('error', function (err) {
|
||||
if (err.code != 'EADDRINUSE') return done(err)
|
||||
done(null, true)
|
||||
})
|
||||
.once('listening', function() {
|
||||
tester.once('close', function() { done(null, false) })
|
||||
.close()
|
||||
})
|
||||
.listen(port, ip)
|
||||
}
|
||||
|
||||
var redbird = Redbird({
|
||||
bunyan: false,
|
||||
port: 8080,
|
||||
|
||||
// Specify filenames to default SSL certificates (in case SNI is not supported by the
|
||||
// user's browser)
|
||||
ssl: [
|
||||
{
|
||||
port: 4433,
|
||||
key: 'dummy',
|
||||
cert: 'dummy',
|
||||
ip: '127.0.0.1'
|
||||
},
|
||||
{
|
||||
port: 4434,
|
||||
key: 'dummy',
|
||||
cert: 'dummy',
|
||||
ip: '127.0.0.1'
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
redbird.register('mydomain.com', 'http://127.0.0.1:8001', {
|
||||
ssl: {
|
||||
key: 'dummy',
|
||||
cert: 'dummy',
|
||||
ca: 'dummym'
|
||||
}
|
||||
});
|
||||
|
||||
var portsTaken = 0;
|
||||
var portsChecked = 0;
|
||||
|
||||
function portsTakenDone(err, taken) {
|
||||
portsChecked++;
|
||||
if (err) { throw err; }
|
||||
if (taken) { portsTaken++; }
|
||||
if ( portsChecked == 2 ) {
|
||||
portsCheckDone();
|
||||
}
|
||||
}
|
||||
|
||||
function portsCheckDone() {
|
||||
expect(portsTaken).to.be.eql(2);
|
||||
redbird.close();
|
||||
testDone();
|
||||
}
|
||||
|
||||
isPortTaken(4433, '127.0.0.1', portsTakenDone);
|
||||
isPortTaken(4434, '127.0.0.1', portsTakenDone);
|
||||
});
|
||||
})
|
||||
|
||||
|
||||
describe("Load balancing", function () {
|
||||
it("should load balance between several targets", function () {
|
||||
var redbird = Redbird(opts);
|
||||
|
||||
expect(redbird.routing).to.be.an("object");
|
||||
|
||||
redbird.register('example.com', '192.168.1.1:8080');
|
||||
redbird.register('example.com', '192.168.1.2:8080');
|
||||
redbird.register('example.com', '192.168.1.3:8080');
|
||||
redbird.register('example.com', '192.168.1.4:8080');
|
||||
|
||||
expect(redbird.routing['example.com'][0].urls.length).to.be.eql(4);
|
||||
expect(redbird.routing['example.com'][0].rr).to.be.eql(0);
|
||||
|
||||
var route = redbird.resolve('example.com', '/foo/qux/a/b/c');
|
||||
expect(route.urls.length).to.be.eql(4);
|
||||
|
||||
for (var i = 0; i < 1000; i++) {
|
||||
var target = redbird._getTarget('example.com', { url: '/a/b/c' });
|
||||
expect(target.href).to.be.eql('http://192.168.1.1:8080/')
|
||||
expect(redbird.routing['example.com'][0].rr).to.be.eql(1);
|
||||
|
||||
var target = redbird._getTarget('example.com', { url: '/x/y' });
|
||||
expect(target.href).to.be.eql('http://192.168.1.2:8080/')
|
||||
expect(redbird.routing['example.com'][0].rr).to.be.eql(2);
|
||||
|
||||
var target = redbird._getTarget('example.com', { url: '/j' });
|
||||
expect(target.href).to.be.eql('http://192.168.1.3:8080/')
|
||||
expect(redbird.routing['example.com'][0].rr).to.be.eql(3);
|
||||
|
||||
var target = redbird._getTarget('example.com', { url: '/k/' });
|
||||
expect(target.href).to.be.eql('http://192.168.1.4:8080/')
|
||||
expect(redbird.routing['example.com'][0].rr).to.be.eql(0);
|
||||
}
|
||||
|
||||
redbird.unregister('example.com', '192.168.1.1:8080');
|
||||
expect(redbird.resolve('example.com')).to.not.be.an("undefined")
|
||||
|
||||
redbird.unregister('example.com', '192.168.1.2:8080');
|
||||
expect(redbird.resolve('example.com')).to.not.be.an("undefined")
|
||||
|
||||
redbird.unregister('example.com', '192.168.1.3:8080');
|
||||
expect(redbird.resolve('example.com')).to.not.be.an("undefined")
|
||||
|
||||
redbird.unregister('example.com', '192.168.1.4:8080');
|
||||
expect(redbird.resolve('example.com')).to.be.an("undefined")
|
||||
|
||||
|
||||
redbird.close();
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user