2 * routing-table-test.js: Tests for the proxying using the ProxyTable object.
4 * (C) 2010, Charlie Robbins
8 var assert = require('assert'),
10 path = require('path'),
11 async = require('async'),
12 request = require('request'),
13 vows = require('vows'),
14 macros = require('../macros'),
15 helpers = require('../helpers');
17 var routeFile = path.join(__dirname, 'config.json');
19 vows.describe(helpers.describe('routing-table')).addBatch({
20 "With a routing table": {
21 "with latency": macros.http.assertProxiedToRoutes({
24 "icanhaz.com": "127.0.0.1:{PORT}",
25 "latency.com": "127.0.0.1:{PORT}"
28 "addHost() / removeHost()": macros.http.assertDynamicProxy({
31 "static.com": "127.0.0.1:{PORT}",
32 "removed.com": "127.0.0.1:{PORT}"
35 add: [{ host: 'dynamic1.com', target: '127.0.0.1:' }],
38 "using RegExp": macros.http.assertProxiedToRoutes({
40 "foo.com": "127.0.0.1:{PORT}",
41 "bar.com": "127.0.0.1:{PORT}",
42 "baz.com/taco": "127.0.0.1:{PORT}",
43 "pizza.com/taco/muffins": "127.0.0.1:{PORT}",
44 "blah.com/me": "127.0.0.1:{PORT}/remapped",
45 "bleh.com/remap/this": "127.0.0.1:{PORT}/remap/remapped",
46 "test.com/double/tap": "127.0.0.1:{PORT}/remap"
49 "using hostnameOnly": macros.http.assertProxiedToRoutes({
52 "foo.com": "127.0.0.1:{PORT}",
53 "bar.com": "127.0.0.1:{PORT}"
56 "using pathnameOnly": macros.http.assertProxiedToRoutes({
59 "/foo": "127.0.0.1:{PORT}",
60 "/bar": "127.0.0.1:{PORT}",
61 "/pizza": "127.0.0.1:{PORT}"
64 "using a routing file": macros.http.assertProxiedToRoutes({
67 "foo.com": "127.0.0.1:{PORT}",
68 "bar.com": "127.0.0.1:{PORT}"
71 "after the file has been modified": {
73 var config = JSON.parse(fs.readFileSync(routeFile, 'utf8')),
74 protocol = helpers.protocols.proxy,
75 port = helpers.nextPort,
78 config.router['dynamic.com'] = "127.0.0.1:" + port;
79 fs.writeFileSync(routeFile, JSON.stringify(config));
82 function waitForRoutes(next) {
83 that.proxyServer.on('routes', next);
86 helpers.http.createServer,
89 output: 'hello from dynamic.com'
94 uri: protocol + '://127.0.0.1:' + that.port,
101 "should receive 'hello from dynamic.com'": function (err, res, body) {
102 assert.equal(body, 'hello from dynamic.com');