Skip to content

Commit

Permalink
[refactor test] Add support for http*-to-http* testing from CLI arg…
Browse files Browse the repository at this point in the history
…uments
  • Loading branch information
indexzero committed Jul 22, 2012
1 parent 55286a7 commit 828dbeb
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 24 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
],
"dependencies": {
"colors": "0.x.x",
"optimist": "0.2.x",
"optimist": "0.3.x",
"pkginfo": "0.2.x"
},
"devDependencies": {
"request": "1.9.x",
"vows": "0.5.x",
"vows": "0.6.x",
"async": "0.1.x",
"socket.io": "0.9.6",
"socket.io-client": "0.9.6",
Expand All @@ -35,8 +35,8 @@
},
"scripts": {
"test": "npm run-script test-http && npm run-script test-https && npm run-script test-core",
"test-http": "vows --spec && vows --spec --target=secure",
"test-https": "vows --spec --source=secure && vows --spec --source=secure --target=secure",
"test-http": "vows --spec && vows --spec --target=https",
"test-https": "vows --spec --proxy=https && vows --spec --proxy=https --target=https",
"test-core": "test/core/run"
},
"engines": {
Expand Down
36 changes: 31 additions & 5 deletions test/helpers/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

var assert = require('assert'),
http = require('http'),
https = require('https'),
url = require('url'),
async = require('async'),
helpers = require('./index'),
protocols = helpers.protocols,
httpProxy = require('../../lib/node-http-proxy');

//
Expand Down Expand Up @@ -48,7 +50,11 @@ exports.createServerPair = function (options, callback) {
// Creates a target server that the tests will proxy to.
//
exports.createServer = function (options, callback) {
http.createServer(function (req, res) {
//
// Request handler to use in either `http`
// or `https` server.
//
function requestHandler(req, res) {
if (options.headers) {
Object.keys(options.headers).forEach(function (key) {
assert.equal(req.headers[key], options.headers[key]);
Expand All @@ -58,7 +64,13 @@ exports.createServer = function (options, callback) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write(options.output || 'hello proxy');
res.end();
}).listen(options.port, function () {
}

var server = protocols.target === 'https'
? https.createServer(helpers.https, requestHandler)
: http.createServer(requestHandler);

server.listen(options.port, function () {
callback(null, this);
});
};
Expand All @@ -76,6 +88,10 @@ exports.createServer = function (options, callback) {
//
exports.createProxyServer = function (options, callback) {
if (!options.latency) {
if (protocols.proxy === 'https') {
options.proxy.https = helpers.https;
}

return httpProxy
.createServer(options.proxy)
.listen(options.port, function () {
Expand All @@ -87,9 +103,13 @@ exports.createProxyServer = function (options, callback) {
? new httpProxy.RoutingProxy(options.proxy)
: new httpProxy.HttpProxy(options.proxy);

http.createServer(function (req, res) {
//
// Request handler to use in either `http`
// or `https` server.
//
function requestHandler(req, res) {
var buffer = httpProxy.buffer(req);

setTimeout(function () {
//
// Setup options dynamically for `RoutingProxy.prototype.proxyRequest`
Expand All @@ -98,7 +118,13 @@ exports.createProxyServer = function (options, callback) {
buffer = options.routing ? { buffer: buffer } : buffer
proxy.proxyRequest(req, res, buffer);
}, options.latency);
}).listen(options.port, function () {
}

var server = protocols.proxy === 'https'
? https.createServer(helpers.https, requestHandler)
: http.createServer(requestHandler);

server.listen(options.port, function () {
callback(null, this);
});
};
Expand Down
67 changes: 67 additions & 0 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,40 @@
*
*/

var fs = require('fs'),
path = require('path');

var fixturesDir = path.join(__dirname, '..', 'fixtures');

//
// @https {Object}
// Returns the necessary `https` credentials.
//
Object.defineProperty(exports, 'https', {
get: function () {
delete this.https;
return this.https = {
key: fs.readFileSync(path.join(fixturesDir, 'agent2-key.pem'), 'utf8'),
cert: fs.readFileSync(path.join(fixturesDir, 'agent2-cert.pem'), 'utf8')
};
}
});

//
// @protocols {Object}
// Returns an object representing the desired protocols
// for the `proxy` and `target` server.
//
Object.defineProperty(exports, 'protocols', {
get: function () {
delete this.protocols;
return this.protocols = {
target: exports.argv.target || 'http',
proxy: exports.argv.proxy || 'http'
};
}
});

//
// @nextPort {number}
// Returns an auto-incrementing port for tests.
Expand All @@ -31,6 +65,39 @@ Object.defineProperty(exports, 'nextPortPair', {
}
});

//
// ### function describe(prefix)
// #### @prefix {string} Prefix to use before the description
//
// Returns a string representing the protocols that this suite
// is testing based on CLI arguments.
//
exports.describe = function (prefix, base) {
prefix = prefix || '';
base = base || 'http'

function protocol(endpoint) {
return exports.protocols[endpoint] === 'https'
? base + 's'
: base;
}

return [
'node-http-proxy',
prefix,
[
protocol('proxy'),
'-to-',
protocol('target')
].join('')
].filter(Boolean).join('/');
};

//
// Expose the CLI arguments
//
exports.argv = require('optimist').argv;

//
// Export additional helpers for `http` and `websockets`.
//
Expand Down
4 changes: 2 additions & 2 deletions test/http/http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ var assert = require('assert'),
request = require('request'),
vows = require('vows'),
macros = require('../macros'),
helpers = require('../helpers/index');
helpers = require('../helpers');

var routeFile = path.join(__dirname, 'config.json');

vows.describe('node-http-proxy/http').addBatch({
vows.describe(helpers.describe()).addBatch({
"With a valid target server": {
"and no latency": {
"and no headers": macros.http.assertProxied(),
Expand Down
5 changes: 3 additions & 2 deletions test/http/routing-table-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var assert = require('assert'),

var routeFile = path.join(__dirname, 'config.json');

vows.describe('node-http-proxy/http/routing-table').addBatch({
vows.describe(helpers.describe('routing-table')).addBatch({
"With a routing table": {
"with latency": macros.http.assertProxiedToRoutes({
latency: 2000,
Expand Down Expand Up @@ -53,6 +53,7 @@ vows.describe('node-http-proxy/http/routing-table').addBatch({
"after the file has been modified": {
topic: function () {
var config = JSON.parse(fs.readFileSync(routeFile, 'utf8')),
protocol = helpers.protocols.proxy,
port = helpers.nextPort,
that = this;

Expand All @@ -72,7 +73,7 @@ vows.describe('node-http-proxy/http/routing-table').addBatch({
)
], function () {
request({
uri: 'http://127.0.0.1:' + that.port,
uri: protocol + '://127.0.0.1:' + that.port,
headers: {
host: 'dynamic.com'
}
Expand Down
35 changes: 24 additions & 11 deletions test/macros/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ exports.assertRequest = function (options) {
request(options.request, this.callback);
},
"should succeed": function (err, res, body) {
assert.isNull(err);
if (options.assert.body) {
assert.equal(body, options.assert.body);
}
Expand All @@ -55,11 +56,12 @@ exports.assertRequest = function (options) {
exports.assertProxied = function (options) {
options = options || {};

var ports = options.ports || helpers.nextPortPair,
output = options.output || 'hello world from ' + ports.target,
req = options.request || {};
var ports = options.ports || helpers.nextPortPair,
output = options.output || 'hello world from ' + ports.target,
protocol = helpers.protocols.proxy,
req = options.request || {};

req.uri = req.uri || 'http://127.0.0.1:' + ports.proxy;
req.uri = req.uri || protocol + '://127.0.0.1:' + ports.proxy;

return {
topic: function () {
Expand All @@ -79,6 +81,7 @@ exports.assertProxied = function (options) {
proxy: {
forward: options.forward,
target: {
https: helpers.protocols.target === 'https',
host: '127.0.0.1',
port: ports.target
}
Expand Down Expand Up @@ -107,10 +110,12 @@ exports.assertProxied = function (options) {
exports.assertInvalidProxy = function (options) {
options = options || {};

var ports = options.ports || helpers.nextPortPair,
req = options.request || {};
var ports = options.ports || helpers.nextPortPair,
req = options.request || {},
protocol = helpers.protocols.proxy;

req.uri = req.uri || 'http://127.0.0.1:' + ports.proxy;

req.uri = req.uri || protocol + '://127.0.0.1:' + ports.proxy;

return {
topic: function () {
Expand Down Expand Up @@ -196,9 +201,10 @@ exports.assertProxiedToRoutes = function (options, nested) {
//
var locations = helpers.http.parseRoutes(options),
port = helpers.nextPort,
protocol = helpers.protocols.proxy,
context,
proxy;

if (options.filename) {
//
// If we've been passed a filename write the routes to it
Expand All @@ -215,7 +221,14 @@ exports.assertProxiedToRoutes = function (options, nested) {
hostnameOnly: options.hostnameOnly,
router: options.routes
};
}
}

//
// Set the https options if necessary
//
if (helpers.protocols.target === 'https') {
proxy.target = { https: true };
}

//
// Create the test context which creates all target
Expand Down Expand Up @@ -271,7 +284,7 @@ exports.assertProxiedToRoutes = function (options, nested) {
"a request to unknown.com": exports.assertRequest({
assert: { statusCode: 404 },
request: {
uri: 'http://127.0.0.1:' + port,
uri: protocol + '://127.0.0.1:' + port,
headers: {
host: 'unknown.com'
}
Expand All @@ -285,7 +298,7 @@ exports.assertProxiedToRoutes = function (options, nested) {
locations.forEach(function (location) {
context[location.source.href] = exports.assertRequest({
request: {
uri: 'http://127.0.0.1:' + port + location.source.path,
uri: protocol + '://127.0.0.1:' + port + location.source.path,
headers: {
host: location.source.hostname
}
Expand Down

0 comments on commit 828dbeb

Please sign in to comment.