Commit f7010e51 authored by indexzero's avatar indexzero

[merge] Merge from significant internal refactor in v0.7.x. No external API changes

parents 38315f6b 0182ba37
test/config.json
config.json
node_modules/
npm-debug.log
......@@ -114,7 +114,7 @@ httpProxy.createServer(function (req, res, proxy) {
// Buffer the request so that `data` and `end` events
// are not lost during async operation(s).
//
var buffer = proxy.buffer(req);
var buffer = httpProxy.buffer(req);
//
// Wait for two seconds then respond: this simulates
......@@ -357,6 +357,13 @@ server.on('upgrade', function(req, socket, head) {
});
```
### Configuring your Socket limits
By default, `node-http-proxy` will set a 100 socket limit for all `host:port` proxy targets. If you wish to change this you can two it in two ways:
1. By passing the `maxSockets` option to `httpProxy.createServer()`
2. By calling `httpProxy.setMaxSockets(n)`, where `n` is the number of sockets you with to use.
## Using node-http-proxy from the command line
When you install this package with npm, a node-http-proxy binary will become available to you. Using this binary is easy with some simple options:
......
......@@ -4,7 +4,7 @@ var path = require('path'),
fs = require('fs'),
util = require('util'),
argv = require('optimist').argv,
httpProxy = require('./../lib/node-http-proxy');
httpProxy = require('../lib/node-http-proxy');
var help = [
"usage: node-http-proxy [options] ",
......
module.exports = Store
//
// just to make these example a little bit interesting,
// make a little key value store with an http interface
......@@ -20,12 +19,10 @@ module.exports = Store
//
// TODO: cached map-reduce views and auto-magic sharding.
//
var Store = module.exports = function Store () {
this.store = {};
};
function Store () {
this.store = {}
}
Store.prototype = {
get: function (key) {
return this.store[key]
......
......@@ -27,16 +27,17 @@
var util = require('util'),
colors = require('colors'),
http = require('http'),
httpProxy = require('./../lib/node-http-proxy');
// ascii art from http://github.com/marak/asciimo
var welcome = '\
# # ##### ##### ##### ##### ##### #### # # # # \n\
# # # # # # # # # # # # # # # # \n\
###### # # # # ##### # # # # # # ## # \n\
# # # # ##### ##### ##### # # ## # \n\
# # # # # # # # # # # # # \n\
# # # # # # # # #### # # # \n';
httpProxy = require('../../lib/node-http-proxy');
var welcome = [
'# # ##### ##### ##### ##### ##### #### # # # #',
'# # # # # # # # # # # # # # # # ',
'###### # # # # ##### # # # # # # ## # ',
'# # # # ##### ##### ##### # # ## # ',
'# # # # # # # # # # # # # ',
'# # # # # # # # #### # # # '
].join('\n');
util.puts(welcome.rainbow.bold);
//
......
......@@ -27,7 +27,7 @@
var util = require('util'),
colors = require('colors'),
http = require('http'),
httpProxy = require('./../lib/node-http-proxy');
httpProxy = require('../../lib/node-http-proxy');
//
// Basic Http Proxy Server
......@@ -42,23 +42,24 @@ httpProxy.createServer(9000, 'localhost').listen(8000);
//
var connections = []
, go
var connections = [],
go;
http.createServer(function (req, res) {
connections.push (function (){
connections.push(function () {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
})
process.stdout.write(connections.length + ', ')
});
process.stdout.write(connections.length + ', ');
if (connections.length > 110 || go) {
go = true
while(connections.length)
connections.shift()()
go = true;
while (connections.length) {
connections.shift()();
}
}
}).listen(9000);
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
......
......@@ -27,17 +27,12 @@
var util = require('util'),
colors = require('colors'),
http = require('http'),
httpProxy = require('./../lib/node-http-proxy');
httpProxy = require('../../lib/node-http-proxy');
//
// Http Proxy Server with Latency
//
var server = httpProxy.createServer(function (req, res, proxy) {
proxy.proxyRequest(req, res, {
port: 9000,
host: 'localhost'
});
})
var server = httpProxy.createServer(9000, 'localhost');
//
// Tell the server to listen on port 8002
......
......@@ -27,7 +27,7 @@
var util = require('util'),
colors = require('colors'),
http = require('http'),
httpProxy = require('./../lib/node-http-proxy');
httpProxy = require('../../lib/node-http-proxy');
//
// Setup proxy server with forwarding
......@@ -60,4 +60,4 @@ http.createServer(function (req, res) {
util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8003 '.yellow + 'with forward proxy'.magenta.underline);
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow);
util.puts('http forward server '.blue + 'started '.green.bold + 'on port '.blue + '9001 '.yellow);
util.puts('http forward server '.blue + 'started '.green.bold + 'on port '.blue + '9001 '.yellow);
\ No newline at end of file
......@@ -27,13 +27,13 @@
var util = require('util'),
colors = require('colors'),
http = require('http'),
httpProxy = require('./../lib/node-http-proxy');
httpProxy = require('../../lib/node-http-proxy');
//
// Http Proxy Server with Latency
//
httpProxy.createServer(function (req, res, proxy) {
var buffer = proxy.buffer(req);
var buffer = httpProxy.buffer(req);
setTimeout(function() {
proxy.proxyRequest(req, res, {
port: 9000,
......
......@@ -28,7 +28,7 @@ var https = require('https'),
http = require('http'),
util = require('util'),
colors = require('colors'),
httpProxy = require('./../lib/node-http-proxy'),
httpProxy = require('../../lib/node-http-proxy'),
helpers = require('./../test/helpers');
var opts = helpers.loadHttps();
......
......@@ -28,7 +28,7 @@ var https = require('https'),
http = require('http'),
util = require('util'),
colors = require('colors'),
httpProxy = require('./../lib/node-http-proxy'),
httpProxy = require('../../lib/node-http-proxy'),
helpers = require('./../test/helpers');
var opts = helpers.loadHttps();
......
......@@ -27,7 +27,7 @@
var util = require('util'),
colors = require('colors'),
http = require('http'),
httpProxy = require('./../lib/node-http-proxy');
httpProxy = require('../../lib/node-http-proxy');
//
// Http Proxy Server with Proxy Table
......
......@@ -27,14 +27,14 @@
var util = require('util'),
colors = require('colors'),
http = require('http'),
httpProxy = require('./../lib/node-http-proxy');
httpProxy = require('../../lib/node-http-proxy');
//
// Http Server with proxyRequest Handler and Latency
//
var proxy = new httpProxy.HttpProxy();
http.createServer(function (req, res) {
var buffer = proxy.buffer(req);
var buffer = httpProxy.buffer(req);
setTimeout(function() {
proxy.proxyRequest(req, res, {
port: 9000,
......
......@@ -27,7 +27,7 @@
var util = require('util'),
colors = require('colors'),
http = require('http'),
httpProxy = require('./../lib/node-http-proxy');
httpProxy = require('../../lib/node-http-proxy');
//
// Basic Http Proxy Server
......
......@@ -27,4 +27,4 @@ http.createServer(new Store().handler()).listen(7531)
require('http-proxy').createServer(
require('connect-jsonp')(true),
'localhost', 7531
).listen(1337)
).listen(1337)
\ No newline at end of file
......@@ -37,7 +37,7 @@ httpProxy.createServer(
// This is where our middlewares go, with any options desired - in this case,
// the list of routes/URLs and their destinations.
//
require('proxy-by-url')({
require('proxy-by-url')({
'/hello': { port: 9000, host: 'localhost' },
'/charlie': { port: 80, host: 'charlieistheman.com' },
'/google': { port: 80, host: 'google.com' }
......
......@@ -19,12 +19,11 @@ httpProxy.createServer(
//
// Target Http Server (to listen for requests on 'localhost')
//
http.createServer(
function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
// And finally, some colored startup output.
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
......
{
"name": "http-proxy-examples"
, "description": "packages required to run the examples"
, "version": "0.0.0"
, "dependencies": {
"connect": "1.6"
, "connect-gzip": "0.1"
, "connect-jsonp": "0.0.5"
, "connect-restreamer": "1"
, "proxy-by-url": ">= 0.0.1"
"name": "http-proxy-examples",
"description": "packages required to run the examples",
"version": "0.0.0",
"dependencies": {
"connect": "1.6",
"connect-gzip": "0.1",
"connect-jsonp": "0.0.5",
"connect-restreamer": "1",
"proxy-by-url": ">= 0.0.1"
}
}
\ No newline at end of file
......@@ -28,7 +28,7 @@ var sys = require('sys'),
http = require('http'),
colors = require('colors'),
websocket = require('./../vendor/websocket'),
httpProxy = require('./../lib/node-http-proxy');
httpProxy = require('../../lib/node-http-proxy');
try {
var utils = require('socket.io/lib/socket.io/utils'),
......@@ -80,7 +80,7 @@ var proxyServer = http.createServer(function (req, res) {
// WebSocket requests as well.
//
proxyServer.on('upgrade', function (req, socket, head) {
var buffer = proxy.buffer(socket);
var buffer = httpProxy.buffer(socket);
setTimeout(function () {
proxy.proxyWebSocketRequest(req, socket, head, {
......
......@@ -28,7 +28,7 @@ var sys = require('sys'),
http = require('http'),
colors = require('colors'),
websocket = require('./../vendor/websocket'),
httpProxy = require('./../lib/node-http-proxy');
httpProxy = require('../../lib/node-http-proxy');
try {
var utils = require('socket.io/lib/socket.io/utils'),
......
......@@ -28,7 +28,7 @@ var sys = require('sys'),
http = require('http'),
colors = require('colors'),
websocket = require('./../vendor/websocket'),
httpProxy = require('./../lib/node-http-proxy');
httpProxy = require('../../lib/node-http-proxy');
try {
var utils = require('socket.io/lib/socket.io/utils'),
......
This diff is collapsed.
This diff is collapsed.
/*
node-http-proxy.js: Lookup table for proxy targets in node.js
Copyright (c) 2010 Charlie Robbins
Copyright (c) 2010 Charlie Robbins
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
......@@ -29,7 +29,7 @@ var util = require('util'),
fs = require('fs');
//
// ### function ProxyTable (router, silent)
// ### function ProxyTable (router, silent)
// #### @router {Object} Object containing the host based routes
// #### @silent {Boolean} Value indicating whether we should suppress logs
// #### @hostnameOnly {Boolean} Value indicating if we should route based on __hostname string only__
......@@ -37,31 +37,34 @@ var util = require('util'),
// locations of proxy targets based on ServerRequest headers; specifically
// the HTTP host header.
//
var ProxyTable = exports.ProxyTable = function (router, silent, hostnameOnly) {
var ProxyTable = exports.ProxyTable = function (options) {
events.EventEmitter.call(this);
this.silent = typeof silent !== 'undefined' ? silent : true;
this.hostnameOnly = typeof hostnameOnly !== 'undefined' ? hostnameOnly : false;
if (typeof router === 'object') {
this.silent = options.silent || options.silent !== true;
this.hostnameOnly = options.hostnameOnly === true;
if (typeof options.router === 'object') {
//
// If we are passed an object literal setup
// the routes with RegExps from the router
// If we are passed an object literal setup
// the routes with RegExps from the router
//
this.setRoutes(router);
this.setRoutes(options.router);
}
else if (typeof router === 'string') {
else if (typeof options.router === 'string') {
//
// If we are passed a string then assume it is a
// If we are passed a string then assume it is a
// file path, parse that file and watch it for changes
//
var self = this;
this.routeFile = router;
this.setRoutes(JSON.parse(fs.readFileSync(router)).router);
this.routeFile = options.router;
this.setRoutes(JSON.parse(fs.readFileSync(options.router)).router);
fs.watchFile(this.routeFile, function () {
fs.readFile(self.routeFile, function (err, data) {
if (err) throw err;
if (err) {
self.emit('error', err);
}
self.setRoutes(JSON.parse(data).router);
self.emit('routes', self.hostnameOnly === false ? self.routes : self.router);
});
......@@ -72,19 +75,23 @@ var ProxyTable = exports.ProxyTable = function (router, silent, hostnameOnly) {
}
};
// Inherit from events.EventEmitter
//
// Inherit from `events.EventEmitter`
//
util.inherits(ProxyTable, events.EventEmitter);
//
// ### function setRoutes (router)
// ### function setRoutes (router)
// #### @router {Object} Object containing the host based routes
// Sets the host-based routes to be used by this instance.
// Sets the host-based routes to be used by this instance.
//
ProxyTable.prototype.setRoutes = function (router) {
if (!router) throw new Error('Cannot update ProxyTable routes without router.');
if (!router) {
throw new Error('Cannot update ProxyTable routes without router.');
}
this.router = router;
if (this.hostnameOnly === false) {
var self = this;
this.routes = [];
......@@ -101,7 +108,7 @@ ProxyTable.prototype.setRoutes = function (router) {
};
//
// ### function getProxyLocation (req)
// ### function getProxyLocation (req)
// #### @req {ServerRequest} The incoming server request to get proxy information about.
// Returns the proxy location based on the HTTP Headers in the ServerRequest `req`
// available to this instance.
......@@ -110,14 +117,14 @@ ProxyTable.prototype.getProxyLocation = function (req) {
if (!req || !req.headers || !req.headers.host) {
return null;
}
var target = req.headers.host.split(':')[0];
if (this.hostnameOnly == true) {
if (this.router.hasOwnProperty(target)) {
var location = this.router[target].split(':'),
host = location[0],
port = location.length === 1 ? 80 : location[1];
return {
port: port,
host: host
......@@ -127,7 +134,9 @@ ProxyTable.prototype.getProxyLocation = function (req) {
else {
target += req.url;
for (var i in this.routes) {
var match, route = this.routes[i];
var route = this.routes[i],
match;
if (match = target.match(route.route)) {
var location = route.target.split(':'),
host = location[0],
......@@ -140,7 +149,7 @@ ProxyTable.prototype.getProxyLocation = function (req) {
}
}
}
return null;
};
......
/*
* routing-proxy.js: A routing proxy consuming a RoutingTable and multiple HttpProxy instances
*
* (C) 2011 Nodejitsu Inc.
* MIT LICENCE
*
*/
var events = require('events'),
util = require('util'),
HttpProxy = require('./http-proxy').HttpProxy,
ProxyTable = require('./proxy-table').ProxyTable;
//
// ### function RoutingProxy (options)
// #### @options {Object} Options for this instance
// Constructor function for the RoutingProxy object, a higher level
// reverse proxy Object which can proxy to multiple hosts and also interface
// easily with a RoutingTable instance.
//
var RoutingProxy = exports.RoutingProxy = function (options) {
events.EventEmitter.call(this);
var self = this;
options = options || {};
if (options.router) {
this.proxyTable = new ProxyTable(options);
this.proxyTable.on('routes', function (routes) {
self.emit('routes', routes);
});
}
//
// Create a set of `HttpProxy` objects to be used later on calls
// to `.proxyRequest()` and `.proxyWebSocketRequest()`.
//
this.proxies = {};
//
// Setup default target options (such as `https`).
//
this.target  = {};
this.target.https = options.target && options.target.https;
//
// Setup other default options to be used for instances of
// `HttpProxy` created by this `RoutingProxy` instance.
//
this.source = options.source || { host: 'localhost', port: 8000 };
this.https = this.source.https || options.https;
this.enable = options.enable;
this.forward = options.forward;
};
//
// Inherit from `events.EventEmitter`.
//
util.inherits(RoutingProxy, events.EventEmitter);
//
// ### function add (options)
// #### @options {Object} Options for the `HttpProxy` to add.
// Adds a new instance of `HttpProxy` to this `RoutingProxy` instance
// for the specified `options.host` and `options.port`.
//
RoutingProxy.prototype.add = function (options) {
var self = this,
key = this._getKey(options);
//
// TODO: Consume properties in `options` related to the `ProxyTable`.
//
options.target = options.target || {};
options.target.host = options.target.host || options.host;
options.target.port = options.target.port || options.port;
options.target.https = this.target && this.target.https ||
options.target && options.target.https ||
options.https;
//
// Setup options to pass-thru to the new `HttpProxy` instance
// for the specified `options.host` and `options.port` pair.
//
['https', 'enable', 'forward'].forEach(function (key) {
if (options[key] !== false && self[key]) {
options[key] = self[key];
}
});
this.proxies[key] = new HttpProxy(options);
};
//
// ### function remove (options)
// #### @options {Object} Options mapping to the `HttpProxy` to remove.
// Removes an instance of `HttpProxy` from this `RoutingProxy` instance
// for the specified `options.host` and `options.port` (if they exist).
//
RoutingProxy.prototype.remove = function (options) {
var key = this._getKey(options);
};
//
// ### function close()
// Cleans up any state left behind (sockets, timeouts, etc)
// associated with this instance.
//
RoutingProxy.prototype.close = function () {
var self = this;
if (this.proxyTable) {
//
// Close the `RoutingTable` associated with
// this instance (if any).
//
this.proxyTable.close();
}
//
// Close all sockets for all `HttpProxy` object(s)
// associated with this instance.
//
Object.keys(this.proxies).forEach(function (key) {
self.proxies[key].close();
});
};
//
// ### function proxyRequest (req, res, [port, host, paused])
// #### @req {ServerRequest} Incoming HTTP Request to proxy.
// #### @res {ServerResponse} Outgoing HTTP Request to write proxied data to.
// #### @options {Object} Options for the outgoing proxy request.
//
// options.port {number} Port to use on the proxy target host.
// options.host {string} Host of the proxy target.
// options.buffer {Object} Result from `httpProxy.buffer(req)`
// options.https {Object|boolean} Settings for https.
//
RoutingProxy.prototype.proxyRequest = function (req, res, options) {
options = options || {};
//
// Check the proxy table for this instance to see if we need
// to get the proxy location for the request supplied. We will
// always ignore the proxyTable if an explicit `port` and `host`
// arguments are supplied to `proxyRequest`.
//
if (this.proxyTable && !options.host) {
location = this.proxyTable.getProxyLocation(req);
//
// If no location is returned from the ProxyTable instance
// then respond with `404` since we do not have a valid proxy target.
//
if (!location) {
try {
res.writeHead(404);
res.end();
}
catch (er) {
console.error("res.writeHead/res.end error: %s", er.message);
}
return;
}
//
// When using the ProxyTable in conjunction with an HttpProxy instance
// only the following arguments are valid:
//
// * `proxy.proxyRequest(req, res, { host: 'localhost' })`: This will be skipped
// * `proxy.proxyRequest(req, res, { buffer: buffer })`: Buffer will get updated appropriately
// * `proxy.proxyRequest(req, res)`: Options will be assigned appropriately.
//
options.port = location.port;
options.host = location.host;
}
var key = options.host + ':' + options.port,
proxy;
if (!this.proxies[key]) {
this.add(options);
}
proxy = this.proxies[key];
proxy.proxyRequest(req, res, options.buffer);
};
//
// ### function proxyWebSocketRequest (req, socket, head, options)
// #### @req {ServerRequest} Websocket request to proxy.
// #### @socket {net.Socket} Socket for the underlying HTTP request
// #### @head {string} Headers for the Websocket request.
// #### @options {Object} Options to use when proxying this request.
//
// options.port {number} Port to use on the proxy target host.
// options.host {string} Host of the proxy target.
// options.buffer {Object} Result from `httpProxy.buffer(req)`
// options.https {Object|boolean} Settings for https.
//
RoutingProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options) {
options = options || {};
if (this.proxyTable && !options.host) {
location = this.proxyTable.getProxyLocation(req);
if (!location) {
return socket.destroy();
}
options.port = location.port;
options.host = location.host;
}
var key = options.host + ':' + options.port,
proxy;
if (!this.proxies[key]) {
this.add(options);
}
proxy = this.proxies[key];
proxy.proxyWebSocketRequest(req, socket, head, options.buffer);
};
//
// ### @private function _getKey (options)
// #### @options {Object} Options to extract the key from
// Ensures that the appropriate options are present in the `options`
// provided and responds with a string key representing the `host`, `port`
// combination contained within.
//
RoutingProxy.prototype._getKey = function (options) {
if (!options || ((!options.host || !options.port)
&& (!options.target || !options.target.host || !options.target.port))) {
throw new Error('options.host and options.port or options.target are required.');
return;
}
return [
options.host || options.target.host,
options.port || options.target.port
].join(':');
}
\ No newline at end of file
{
"name": "http-proxy",
"description": "A full-featured http reverse proxy for node.js",
"version": "0.6.6",
"version": "0.7.0",
"author": "Charlie Robbins <charlie.robbins@gmail.com>",
"contributors": [
{ "name": "Mikeal Rogers", "email": "mikeal.rogers@gmail.com" },
{ "name": "Marak Squires", "email": "marak.squires@gmail.com" },
{ "name": "Fedor Indutny", "email": "fedor.indutny@gmail.com" }
{ "name": "Fedor Indutny", "email": "fedor.indutny@gmail.com" },
{ "name": "Dominic Tarr", "email": "dominic@nodejitsu.com" }
],
"repository": {
"type": "git",
......@@ -25,6 +26,10 @@
},
"main": "./lib/node-http-proxy",
"bin": { "node-http-proxy": "./bin/node-http-proxy" },
"scripts": { "test": "vows test/*-test.js --spec && vows test/*-test.js --spec --https" },
"scripts": {
"test": "npm run-script test-http && npm run-script test-https",
"test-http": "vows --spec && vows --spec --target=secure",
"test-https": "vows --spec --source=secure && vows --spec --source=secure --target=secure"
},
"engines": { "node": "0.4.x || 0.5.x" }
}
......@@ -5,27 +5,16 @@
*
*/
var fs = require('fs'),
var assert = require('assert'),
fs = require('fs'),
http = require('http'),
https = require('https'),
path = require('path'),
vows = require('vows'),
assert = require('assert'),
argv = require('optimist').argv,
request = require('request'),
websocket = require('./../vendor/websocket'),
httpProxy = require('./../lib/node-http-proxy');
function merge (target) {
var objs = Array.prototype.slice.call(arguments, 1);
objs.forEach(function(o) {
Object.keys(o).forEach(function (attr) {
if (! o.__lookupGetter__(attr)) {
target[attr] = o[attr];
}
});
});
return target;
}
vows = require('vows'),
websocket = require('../vendor/websocket'),
httpProxy = require('../lib/node-http-proxy');
var loadHttps = exports.loadHttps = function () {
return {
......@@ -34,21 +23,35 @@ var loadHttps = exports.loadHttps = function () {
};
};
var TestRunner = exports.TestRunner = function (protocol, target) {
this.options = {};
this.options.target = {};
this.protocol = protocol;
this.target = target;
this.testServers = [];
var parseProtocol = exports.parseProtocol = function () {
function setupProtocol (secure) {
return {
secure: secure,
protocols: {
http: secure ? 'https' : 'http',
ws: secure ? 'wss' : 'ws'
}
}
}
return {
source: setupProtocol(argv.source === 'secure'),
target: setupProtocol(argv.target === 'secure')
};
}
var TestRunner = exports.TestRunner = function (options) {
options = options || {};
this.source = options.source || {};
this.target = options.target || {};
this.testServers = [];
if (protocol === 'https') {
this.options.https = loadHttps();
if (this.source.secure) {
this.source.https = loadHttps();
}
if (target === 'https') {
this.options.target = {
https: loadHttps()
};
if (this.target.secure) {
this.target.https = loadHttps();
}
};
......@@ -59,13 +62,17 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
var test = {
topic: function () {
var that = this, options = {
var that = this,
options;
options = {
method: 'GET',
uri: self.protocol + '://localhost:' + proxyPort,
uri: self.source.protocols.http + '://localhost:' + proxyPort,
headers: {
host: host
}
};
function startTest () {
if (port) {
......@@ -91,7 +98,7 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, createProxy) {
var assertion = "should receive " + statusCode + " responseCode",
protocol = this.protocol;
protocol = this.source.protocols.http;
var test = {
topic: function () {
......@@ -124,7 +131,6 @@ TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, creat
//
// WebSocketTest
//
TestRunner.prototype.webSocketTest = function (options) {
var self = this;
......@@ -161,7 +167,6 @@ TestRunner.prototype.webSocketTest = function (options) {
//
// WebSocketTestWithTable
//
TestRunner.prototype.webSocketTestWithTable = function (options) {
var self = this;
......@@ -172,9 +177,9 @@ TestRunner.prototype.webSocketTestWithTable = function (options) {
options.onListen(socket);
}
self.startProxyServerWithTable(
options.ports.proxy,
{router: options.router},
self.startProxyServerWithTable(
options.ports.proxy,
{ router: options.router },
function (err, proxy) {
if (options.onServer) { options.onServer(proxy) }
......@@ -199,8 +204,7 @@ TestRunner.prototype.webSocketTestWithTable = function (options) {
//
TestRunner.prototype.startProxyServer = function (port, targetPort, host, callback) {
var that = this,
options = that.options,
proxyServer = httpProxy.createServer(targetPort, host, options);
proxyServer = httpProxy.createServer(host, targetPort, this.getOptions());
proxyServer.listen(port, function () {
that.testServers.push(proxyServer);
......@@ -212,18 +216,19 @@ TestRunner.prototype.startProxyServer = function (port, targetPort, host, callba
// Creates the reverse proxy server with a specified latency
//
TestRunner.prototype.startLatentProxyServer = function (port, targetPort, host, latency, callback) {
//
// Initialize the nodeProxy and start proxying the request
var that = this, proxyServer = httpProxy.createServer(function (req, res, proxy) {
var buffer = proxy.buffer(req);
//
var that = this,
proxyServer;
proxyServer = httpProxy.createServer(host, targetPort, function (req, res, proxy) {
var buffer = httpProxy.buffer(req);
setTimeout(function () {
proxy.proxyRequest(req, res, {
port: targetPort,
host: host,
buffer: buffer
});
proxy.proxyRequest(req, res, buffer);
}, latency);
}, this.options);
}, this.getOptions());
proxyServer.listen(port, function () {
that.testServers.push(proxyServer);
......@@ -235,7 +240,9 @@ TestRunner.prototype.startLatentProxyServer = function (port, targetPort, host,
// Creates the reverse proxy server with a ProxyTable
//
TestRunner.prototype.startProxyServerWithTable = function (port, options, callback) {
var that = this, proxyServer = httpProxy.createServer(merge({}, options, this.options));
var that = this,
proxyServer = httpProxy.createServer(merge({}, options, this.getOptions()));
proxyServer.listen(port, function () {
that.testServers.push(proxyServer);
callback();
......@@ -248,13 +255,15 @@ TestRunner.prototype.startProxyServerWithTable = function (port, options, callba
// Creates a latent reverse proxy server using a ProxyTable
//
TestRunner.prototype.startProxyServerWithTableAndLatency = function (port, latency, options, callback) {
//
// Initialize the nodeProxy and start proxying the request
var proxyServer,
that = this,
proxy = new httpProxy.HttpProxy(merge({}, options, that.options));
//
var that = this,
proxy = new httpProxy.RoutingProxy(merge({}, options, this.getOptions())),
proxyServer;
var handler = function (req, res) {
var buffer = proxy.buffer(req);
var buffer = httpProxy.buffer(req);
setTimeout(function () {
proxy.proxyRequest(req, res, {
buffer: buffer
......@@ -262,9 +271,9 @@ TestRunner.prototype.startProxyServerWithTableAndLatency = function (port, laten
}, latency);
};
proxyServer = that.options.https
? https.createServer(that.options.https, handler, that.options)
: http.createServer(handler, that.options);
proxyServer = this.source.https
? https.createServer(this.source.https, handler)
: http.createServer(handler);
proxyServer.listen(port, function () {
that.testServers.push(proxyServer);
......@@ -278,7 +287,9 @@ TestRunner.prototype.startProxyServerWithTableAndLatency = function (port, laten
// Creates proxy server forwarding to the specified options
//
TestRunner.prototype.startProxyServerWithForwarding = function (port, targetPort, host, options, callback) {
var that = this, proxyServer = httpProxy.createServer(targetPort, host, merge({}, options, this.options));
var that = this,
proxyServer = httpProxy.createServer(targetPort, host, merge({}, options, this.getOptions()));
proxyServer.listen(port, function () {
that.testServers.push(proxyServer);
callback(null, proxyServer);
......@@ -289,14 +300,18 @@ TestRunner.prototype.startProxyServerWithForwarding = function (port, targetPort
// Creates the 'hellonode' server
//
TestRunner.prototype.startTargetServer = function (port, output, callback) {
var that = this, targetServer, handler = function (req, res) {
var that = this,
targetServer,
handler;
handler = function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write(output);
res.end();
};
targetServer = this.options.target.https
? https.createServer(this.options.target.https, handler)
targetServer = this.target.https
? https.createServer(this.target.https, handler)
: http.createServer(handler);
targetServer.listen(port, function () {
......@@ -315,3 +330,42 @@ TestRunner.prototype.closeServers = function () {
return this.testServers;
};
//
// Creates a new instance of the options to
// pass to `httpProxy.createServer()`
//
TestRunner.prototype.getOptions = function () {
return {
https: clone(this.source.https),
target: {
https: clone(this.target.https)
}
};
};
//
// ### @private function clone (object)
// #### @object {Object} Object to clone
// Shallow clones the specified object.
//
function clone (object) {
if (!object) { return null }
return Object.keys(object).reduce(function (obj, k) {
obj[k] = object[k];
return obj;
}, {});
}
function merge (target) {
var objs = Array.prototype.slice.call(arguments, 1);
objs.forEach(function(o) {
Object.keys(o).forEach(function (attr) {
if (! o.__lookupGetter__(attr)) {
target[attr] = o[attr];
}
});
});
return target;
}
\ No newline at end of file
......@@ -26,10 +26,9 @@
var assert = require('assert'),
util = require('util'),
argv = require('optimist').argv,
request = require('request'),
vows = require('vows'),
helpers = require('./helpers');
helpers = require('../helpers');
var forwardOptions = {
forward: {
......@@ -45,11 +44,11 @@ var badForwardOptions = {
}
};
var protocol = argv.https ? 'https' : 'http',
target = argv.target ? argv.target : 'http',
runner = new helpers.TestRunner(protocol, target);
var options = helpers.parseProtocol(),
testName = [options.source.protocols.http, options.target.protocols.http].join('-to-'),
runner = new helpers.TestRunner(options);
vows.describe('node-http-proxy/' + protocol).addBatch({
vows.describe('node-http-proxy/http-proxy/' + testName).addBatch({
"When using server created by httpProxy.createServer()": {
"with no latency" : {
"and a valid target server": runner.assertProxied('localhost', 8080, 8081, function (callback) {
......
......@@ -12,11 +12,11 @@ var assert = require('assert'),
argv = require('optimist').argv,
request = require('request'),
vows = require('vows'),
helpers = require('./helpers'),
TestRunner = helpers.TestRunner;
helpers = require('../helpers');
var protocol = argv.https ? 'https' : 'http',
runner = new TestRunner(protocol),
var options = helpers.parseProtocol(),
testName = [options.source.protocols.http, options.target.protocols.http].join('-to-'),
runner = new helpers.TestRunner(options),
routeFile = path.join(__dirname, 'config.json');
var fileOptions = {
......@@ -41,7 +41,7 @@ var hostnameOptions = {
}
};
vows.describe('node-http-proxy/proxy-table/' + protocol).addBatch({
vows.describe('node-http-proxy/routing-proxy/' + testName).addBatch({
"When using server created by httpProxy.createServer()": {
"when passed a routing table": {
"and routing by RegExp": {
......@@ -82,16 +82,14 @@ vows.describe('node-http-proxy/proxy-table/' + protocol).addBatch({
fs.writeFileSync(routeFile, JSON.stringify(config));
this.server.on('routes', function () {
var options = {
method: 'GET',
uri: protocol + '://localhost:8100',
headers: {
host: 'dynamic.com'
}
};
runner.startTargetServer(8103, 'hello dynamic.com', function () {
request(options, that.callback);
request({
method: 'GET',
uri: options.source.protocols.http + '://localhost:8100',
headers: {
host: 'dynamic.com'
}
}, that.callback);
});
});
},
......
......@@ -30,8 +30,8 @@ var util = require('util'),
colors = require('colors'),
request = require('request'),
vows = require('vows'),
websocket = require('./../vendor/websocket'),
helpers = require('./helpers');
websocket = require('../../vendor/websocket'),
helpers = require('../helpers');
try {
var utils = require('socket.io/lib/socket.io/utils'),
......@@ -43,55 +43,11 @@ catch (ex) {
process.exit(1);
}
var protocol = argv.https ? 'https' : 'http',
wsprotocol = argv.https ? 'wss' : 'ws',
runner = new helpers.TestRunner(protocol);
var options = helpers.parseProtocol(),
testName = [options.source.protocols.ws, options.target.protocols.ws].join('-to-'),
runner = new helpers.TestRunner(options);
vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
"when using proxy table":{
"with no latency" : {
"when an inbound message is sent from a WebSocket client": {
topic: function () {
var that = this
headers = {};
runner.webSocketTestWithTable({
io: io,
host: 'localhost',
wsprotocol: wsprotocol,
protocol: protocol,
router: {'localhost':'localhost:8230'},
ports: {
target: 8230,
proxy: 8231
},
onListen: function (socket) {
socket.on('connection', function (client) {
client.on('message', function (msg) {
that.callback(null, msg, headers);
});
});
},
onWsupgrade: function (req, res) {
headers.request = req;
headers.response = res.headers;
},
onOpen: function (ws) {
ws.send(utils.encode('from client'));
}
});
},
"the target server should receive the message": function (err, msg, headers) {
assert.equal(msg, 'from client');
},
"the origin and sec-websocket-origin headers should match": function (err, msg, headers) {
assert.isString(headers.response['sec-websocket-location']);
assert.isTrue(headers.response['sec-websocket-location'].indexOf(wsprotocol) !== -1);
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
}
}
}
},
vows.describe('node-http-proxy/http-proxy/' + testName).addBatch({
"When using server created by httpProxy.createServer()": {
"with no latency" : {
"when an inbound message is sent from a WebSocket client": {
......@@ -102,8 +58,8 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
runner.webSocketTest({
io: io,
host: 'localhost',
wsprotocol: wsprotocol,
protocol: protocol,
wsprotocol: options.source.protocols.ws,
protocol: options.source.protocols.http,
ports: {
target: 8130,
proxy: 8131
......@@ -129,7 +85,7 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
},
"the origin and sec-websocket-origin headers should match": function (err, msg, headers) {
assert.isString(headers.response['sec-websocket-location']);
assert.isTrue(headers.response['sec-websocket-location'].indexOf(wsprotocol) !== -1);
assert.isTrue(headers.response['sec-websocket-location'].indexOf(options.source.protocols.ws) !== -1);
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
}
},
......@@ -141,8 +97,8 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
runner.webSocketTest({
io: io,
host: 'localhost',
wsprotocol: wsprotocol,
protocol: protocol,
wsprotocol: options.source.protocols.ws,
protocol: options.source.protocols.http,
ports: {
target: 8132,
proxy: 8133
......@@ -170,8 +126,8 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
runner.webSocketTest({
io: io,
host: 'localhost',
wsprotocol: wsprotocol,
protocol: protocol,
wsprotocol: options.source.protocols.ws,
protocol: options.source.protocols.http,
ports: {
target: 8134,
proxy: 8135
......@@ -198,7 +154,7 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
},
"the origin and sec-websocket-origin headers should match": function (err, msg, headers) {
assert.isString(headers.response['sec-websocket-location']);
assert.isTrue(headers.response['sec-websocket-location'].indexOf(wsprotocol) !== -1);
assert.isTrue(headers.response['sec-websocket-location'].indexOf(options.source.protocols.ws) !== -1);
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
}
}
......
/*
node-http-proxy-test.js: http proxy for node.js
Copyright (c) 2010 Charlie Robbins, Marak Squires and Fedor Indutny
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var util = require('util'),
assert = require('assert'),
argv = require('optimist').argv,
colors = require('colors'),
request = require('request'),
vows = require('vows'),
websocket = require('../../vendor/websocket'),
helpers = require('../helpers');
try {
var utils = require('socket.io/lib/socket.io/utils'),
io = require('socket.io');
}
catch (ex) {
console.error('Socket.io is required for this example:');
console.error('npm ' + 'install'.green + ' socket.io@0.6.18'.magenta);
process.exit(1);
}
var options = helpers.parseProtocol(),
testName = [options.source.protocols.ws, options.target.protocols.ws].join('-to-'),
runner = new helpers.TestRunner(options);
vows.describe('node-http-proxy/routing-proxy/' + testName).addBatch({
"When using server created by httpProxy.createServer()": {
"using proxy table with no latency": {
"when an inbound message is sent from a WebSocket client": {
topic: function () {
var that = this
headers = {};
runner.webSocketTestWithTable({
io: io,
host: 'localhost',
wsprotocol: options.source.protocols.ws,
protocol: options.source.protocols.http,
router: { 'localhost' : 'localhost:8230' },
ports: {
target: 8230,
proxy: 8231
},
onListen: function (socket) {
socket.on('connection', function (client) {
client.on('message', function (msg) {
that.callback(null, msg, headers);
});
});
},
onWsupgrade: function (req, res) {
headers.request = req;
headers.response = res.headers;
},
onOpen: function (ws) {
ws.send(utils.encode('from client'));
}
});
},
"the target server should receive the message": function (err, msg, headers) {
assert.equal(msg, 'from client');
},
"the origin and sec-websocket-origin headers should match": function (err, msg, headers) {
assert.isString(headers.response['sec-websocket-location']);
assert.isTrue(headers.response['sec-websocket-location'].indexOf(options.source.protocols.ws) !== -1);
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
}
}
}
}
}).addBatch({
"When the tests are over": {
topic: function () {
return runner.closeServers();
},
"the servers should clean up": function () {
assert.isTrue(true);
}
}
}).export(module);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment