mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
89 lines
2.6 KiB
JavaScript
89 lines
2.6 KiB
JavaScript
/*
|
|
* routing-table-test.js: Tests for the proxying using the ProxyTable object.
|
|
*
|
|
* (C) 2010, Charlie Robbins
|
|
*
|
|
*/
|
|
|
|
var assert = require('assert'),
|
|
fs = require('fs'),
|
|
path = require('path'),
|
|
async = require('async'),
|
|
request = require('request'),
|
|
vows = require('vows'),
|
|
macros = require('../macros'),
|
|
helpers = require('../helpers/index');
|
|
|
|
var routeFile = path.join(__dirname, 'config.json');
|
|
|
|
vows.describe('node-http-proxy/http/routing-table').addBatch({
|
|
"With a routing table": {
|
|
"with latency": macros.http.assertProxiedToRoutes({
|
|
latency: 2000,
|
|
routes: {
|
|
"icanhaz.com": "127.0.0.1:{PORT}",
|
|
"latency.com": "127.0.0.1:{PORT}"
|
|
}
|
|
}),
|
|
"using RegExp": macros.http.assertProxiedToRoutes({
|
|
routes: {
|
|
"foo.com": "127.0.0.1:{PORT}",
|
|
"bar.com": "127.0.0.1:{PORT}",
|
|
"baz.com/taco": "127.0.0.1:{PORT}",
|
|
"pizza.com/taco/muffins": "127.0.0.1:{PORT}",
|
|
"blah.com/me": "127.0.0.1:{PORT}/remapped",
|
|
"bleh.com/remap/this": "127.0.0.1:{PORT}/remap/remapped",
|
|
"test.com/double/tap": "127.0.0.1:{PORT}/remap"
|
|
}
|
|
}),
|
|
"using hostnameOnly": macros.http.assertProxiedToRoutes({
|
|
hostnameOnly: true,
|
|
routes: {
|
|
"foo.com": "127.0.0.1:{PORT}",
|
|
"bar.com": "127.0.0.1:{PORT}"
|
|
}
|
|
}),
|
|
"using a routing file": macros.http.assertProxiedToRoutes({
|
|
filename: routeFile,
|
|
routes: {
|
|
"foo.com": "127.0.0.1:{PORT}",
|
|
"bar.com": "127.0.0.1:{PORT}"
|
|
}
|
|
}, {
|
|
"after the file has been modified": {
|
|
topic: function () {
|
|
var config = JSON.parse(fs.readFileSync(routeFile, 'utf8')),
|
|
port = helpers.nextPort,
|
|
that = this;
|
|
|
|
config.router['dynamic.com'] = "127.0.0.1:" + port;
|
|
fs.writeFileSync(routeFile, JSON.stringify(config));
|
|
|
|
async.parallel([
|
|
function waitForRoutes(next) {
|
|
that.proxyServer.on('routes', next);
|
|
},
|
|
async.apply(
|
|
helpers.http.createServer,
|
|
{
|
|
port: port,
|
|
output: 'hello from dynamic.com'
|
|
}
|
|
)
|
|
], function () {
|
|
request({
|
|
uri: 'http://localhost:' + that.port,
|
|
headers: {
|
|
host: 'dynamic.com'
|
|
}
|
|
}, that.callback);
|
|
});
|
|
},
|
|
"should receive 'hello from dynamic.com'": function (err, res, body) {
|
|
assert.equal(body, 'hello from dynamic.com');
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}).export(module);
|