<commit_before>/*
 * proxy-table-test.js: Tests for the ProxyTable object.
 *
 * (C) 2010, <NAME>
 *
 */

var fs = require('fs'),
    vows = require('vows'),
    util = require('util'),
    path = require('path'),
    request = require('request'),
    assert = require('assert'),
    helpers = require('./helpers'),
    TestRunner = helpers.TestRunner;
    
var runner = new TestRunner(),
    routeFile = path.join(__dirname, 'config.json'),
    assertProxiedWithTarget = helpers.assertProxiedWithTarget,
    assertProxiedWithNoTarget = helpers.assertProxiedWithNoTarget;

var fileOptions = {
  router: {
    "foo.com": "127.0.0.1:8101",
    "bar.com": "127.0.0.1:8102"
  }
};

var defaultOptions = {
  router: {
    "foo.com": "127.0.0.1:8091",
    "bar.com": "127.0.0.1:8092"
  }
};

var hostnameOptions = {
  hostnameOnly: true,
  router: {
    "foo.com": "127.0.0.1:8091",
    "bar.com": "127.0.0.1:8092"
  },
}

vows.describe('node-http-proxy/proxy-table').addBatch({
  "When using server created by httpProxy.createServer()": {
    "when passed a routing table": {
      "and routing by RegExp": {
        topic: function () {
          this.server = runner.startProxyServerWithTable(8090, defaultOptions, this.callback);
        },
        "an incoming request to foo.com": assertProxiedWithTarget(runner, 'foo.com', 8090, 8091),
        "an incoming request to bar.com": assertProxiedWithTarget(runner, 'bar.com', 8090, 8092),
        "an incoming request to unknown.com": assertProxiedWithNoTarget(runner, 8090, 404)
      },
      "and routing by Hostname": {
        topic: function () {
          this.server = runner.startProxyServerWithTable(8093, hostnameOptions, this.callback);
        },
        "an incoming request to foo.com": assertProxiedWithTarget(runner, 'foo.com', 8093, 8094),
        "an incoming request to bar.com": assertProxiedWithTarget(runner, 'bar.com', 8093, 8095),
        "an incoming request to unknown.com": assertProxiedWithNoTarget(runner, 8093, 404)
      }
    },
    "when passed a routing file": {
      topic: function () {
        fs.writeFileSync(routeFile, JSON.stringify(fileOptions));
        this.server = runner.startProxyServerWithTable(8100, {
          router: routeFile
        }, this.callback);
      },
      "an incoming request to foo.com": assertProxiedWithTarget(runner, 'foo.com', 8100, 8101),
      "an incoming request to bar.com": assertProxiedWithTarget(runner, 'bar.com', 8100, 8102),
      "an incoming request to unknown.com": assertProxiedWithNoTarget(runner, 8100, 404),
      "an incoming request to dynamic.com": {
        "after the file has been modified": {
          topic: function () {
            var that = this,
                data = fs.readFileSync(routeFile),
                config = JSON.parse(data);

            this.output = 'hello dynamic.com';
            config.router['dynamic.com'] = "127.0.0.1:8103"
            fs.writeFileSync(routeFile, JSON.stringify(config));
            
            this.server.on('routes', function () {
              var options = {
                method: 'GET', 
                uri: 'http://localhost:8100',
                headers: {
                  host: 'dynamic.com'
                }
              };

              runner.startTargetServer(8103, that.output, function () {
                request(options, that.callback);
              });
            });
          },
          "should receive 'hello dynamic.com'": function (err, res, body) {
            assert.equal(body, this.output);
          }
        }
      }
    }
  }
}).addBatch({<commit_msg>[test] Small update to proxy-table-test.js<commit_after>/*
 * proxy-table-test.js: Tests for the ProxyTable object.
 *
 * (C) 2010, <NAME>
 *
 */

var fs = require('fs'),
    vows = require('vows'),
    util = require('util'),
    path = require('path'),
    request = require('request'),
    assert = require('assert'),
    helpers = require('./helpers'),
    TestRunner = helpers.TestRunner;
    
var runner = new TestRunner(),
    routeFile = path.join(__dirname, 'config.json'),
    assertProxiedWithTarget = helpers.assertProxiedWithTarget,
    assertProxiedWithNoTarget = helpers.assertProxiedWithNoTarget;

var fileOptions = {
  router: {
    "foo.com": "127.0.0.1:8101",
    "bar.com": "127.0.0.1:8102"
  }
};

var defaultOptions = {
  router: {
    "foo.com": "127.0.0.1:8091",
    "bar.com": "127.0.0.1:8092"
  }
};

var hostnameOptions = {
  hostnameOnly: true,
  router: {
    "foo.com": "127.0.0.1:8091",
    "bar.com": "127.0.0.1:8092"
  },
}

vows.describe('node-http-proxy/proxy-table').addBatch({
  "When using server created by httpProxy.createServer()": {
    "when passed a routing table": {
      "and routing by RegExp": {
        topic: function () {
          this.server = runner.startProxyServerWithTable(8090, defaultOptions, this.callback);
        },
        "an incoming request to foo.com": assertProxiedWithTarget(runner, 'foo.com', 8090, 8091),
        "an incoming request to bar.com": assertProxiedWithTarget(runner, 'bar.com', 8090, 8092),
        "an incoming request to unknown.com": assertProxiedWithNoTarget(runner, 8090, 404)
      },
      "and routing by Hostname": {
        topic: function () {
          this.server = runner.startProxyServerWithTable(8093, hostnameOptions, this.callback);
        },
        "an incoming request to foo.com": assertProxiedWithTarget(runner, 'foo.com', 8093, 8094),
        "an incoming request to bar.com": assertProxiedWithTarget(runner, 'bar.com', 8093, 8095),
        "an incoming request to unknown.com": assertProxiedWithNoTarget(runner, 8093, 404)
      }
    },
    "when passed a routing file": {
      topic: function () {
        fs.writeFileSync(routeFile, JSON.stringify(fileOptions));
        this.server = runner.startProxyServerWithTable(8100, {
          router: routeFile
        }, this.callback);
      },
      "an incoming request to foo.com": assertProxiedWithTarget(runner, 'foo.com', 8100, 8101),
      "an incoming request to bar.com": assertProxiedWithTarget(runner, 'bar.com', 8100, 8102),
      "an incoming request to unknown.com": assertProxiedWithNoTarget(runner, 8100, 404),
      "an incoming request to dynamic.com": {
        "after the file has been modified": {
          topic: function () {
            var that = this,
                data = fs.readFileSync(routeFile),
                config = JSON.parse(data);

            config.router['dynamic.com'] = "127.0.0.1:8103"
            fs.writeFileSync(routeFile, JSON.stringify(config));
            
            this.server.on('routes', function () {
              var options = {
                method: 'GET', 
                uri: 'http://localhost:8100',
                headers: {
                  host: 'dynamic.com'
                }
              };

              runner.startTargetServer(8103, 'hello dynamic.com', function () {
                request(options, that.callback);
              });
            });
          },
          "should receive 'hello dynamic.com'": function (err, res, body) {
            assert.equal(body, 'hello dynamic.com');
          }
        }
      }
    }
  }
}).addBatch({
{'fraction_non_alphanumeric': 0.14076858813700918, 'fraction_numerical': 0.045391255917571705, 'mean_word_length': 2.37546992481203, 'pattern_counts': {'":': 46, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 14, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2454207', 'n_tokens_mistral': 2415, 'n_tokens_neox': 2193, 'n_words': 480}