[refactor test] Add support for http*-to-http* testing from CLI arguments

This commit is contained in:
indexzero 2012-07-22 01:03:41 -04:00
parent 55286a7c49
commit 828dbebcaa
6 changed files with 131 additions and 24 deletions

View File

@ -18,12 +18,12 @@
], ],
"dependencies": { "dependencies": {
"colors": "0.x.x", "colors": "0.x.x",
"optimist": "0.2.x", "optimist": "0.3.x",
"pkginfo": "0.2.x" "pkginfo": "0.2.x"
}, },
"devDependencies": { "devDependencies": {
"request": "1.9.x", "request": "1.9.x",
"vows": "0.5.x", "vows": "0.6.x",
"async": "0.1.x", "async": "0.1.x",
"socket.io": "0.9.6", "socket.io": "0.9.6",
"socket.io-client": "0.9.6", "socket.io-client": "0.9.6",
@ -35,8 +35,8 @@
}, },
"scripts": { "scripts": {
"test": "npm run-script test-http && npm run-script test-https && npm run-script test-core", "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-http": "vows --spec && vows --spec --target=https",
"test-https": "vows --spec --source=secure && vows --spec --source=secure --target=secure", "test-https": "vows --spec --proxy=https && vows --spec --proxy=https --target=https",
"test-core": "test/core/run" "test-core": "test/core/run"
}, },
"engines": { "engines": {

View File

@ -8,9 +8,11 @@
var assert = require('assert'), var assert = require('assert'),
http = require('http'), http = require('http'),
https = require('https'),
url = require('url'), url = require('url'),
async = require('async'), async = require('async'),
helpers = require('./index'), helpers = require('./index'),
protocols = helpers.protocols,
httpProxy = require('../../lib/node-http-proxy'); httpProxy = require('../../lib/node-http-proxy');
// //
@ -48,7 +50,11 @@ exports.createServerPair = function (options, callback) {
// Creates a target server that the tests will proxy to. // Creates a target server that the tests will proxy to.
// //
exports.createServer = function (options, callback) { 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) { if (options.headers) {
Object.keys(options.headers).forEach(function (key) { Object.keys(options.headers).forEach(function (key) {
assert.equal(req.headers[key], options.headers[key]); assert.equal(req.headers[key], options.headers[key]);
@ -58,7 +64,13 @@ exports.createServer = function (options, callback) {
res.writeHead(200, { 'Content-Type': 'text/plain' }); res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write(options.output || 'hello proxy'); res.write(options.output || 'hello proxy');
res.end(); 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); callback(null, this);
}); });
}; };
@ -76,6 +88,10 @@ exports.createServer = function (options, callback) {
// //
exports.createProxyServer = function (options, callback) { exports.createProxyServer = function (options, callback) {
if (!options.latency) { if (!options.latency) {
if (protocols.proxy === 'https') {
options.proxy.https = helpers.https;
}
return httpProxy return httpProxy
.createServer(options.proxy) .createServer(options.proxy)
.listen(options.port, function () { .listen(options.port, function () {
@ -87,7 +103,11 @@ exports.createProxyServer = function (options, callback) {
? new httpProxy.RoutingProxy(options.proxy) ? new httpProxy.RoutingProxy(options.proxy)
: new httpProxy.HttpProxy(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); var buffer = httpProxy.buffer(req);
setTimeout(function () { setTimeout(function () {
@ -98,7 +118,13 @@ exports.createProxyServer = function (options, callback) {
buffer = options.routing ? { buffer: buffer } : buffer buffer = options.routing ? { buffer: buffer } : buffer
proxy.proxyRequest(req, res, buffer); proxy.proxyRequest(req, res, buffer);
}, options.latency); }, 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); callback(null, this);
}); });
}; };

View File

@ -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} // @nextPort {number}
// Returns an auto-incrementing port for tests. // Returns an auto-incrementing port for tests.
@ -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`. // Export additional helpers for `http` and `websockets`.
// //

View File

@ -31,11 +31,11 @@ var assert = require('assert'),
request = require('request'), request = require('request'),
vows = require('vows'), vows = require('vows'),
macros = require('../macros'), macros = require('../macros'),
helpers = require('../helpers/index'); helpers = require('../helpers');
var routeFile = path.join(__dirname, 'config.json'); var routeFile = path.join(__dirname, 'config.json');
vows.describe('node-http-proxy/http').addBatch({ vows.describe(helpers.describe()).addBatch({
"With a valid target server": { "With a valid target server": {
"and no latency": { "and no latency": {
"and no headers": macros.http.assertProxied(), "and no headers": macros.http.assertProxied(),

View File

@ -16,7 +16,7 @@ var assert = require('assert'),
var routeFile = path.join(__dirname, 'config.json'); 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 a routing table": {
"with latency": macros.http.assertProxiedToRoutes({ "with latency": macros.http.assertProxiedToRoutes({
latency: 2000, latency: 2000,
@ -53,6 +53,7 @@ vows.describe('node-http-proxy/http/routing-table').addBatch({
"after the file has been modified": { "after the file has been modified": {
topic: function () { topic: function () {
var config = JSON.parse(fs.readFileSync(routeFile, 'utf8')), var config = JSON.parse(fs.readFileSync(routeFile, 'utf8')),
protocol = helpers.protocols.proxy,
port = helpers.nextPort, port = helpers.nextPort,
that = this; that = this;
@ -72,7 +73,7 @@ vows.describe('node-http-proxy/http/routing-table').addBatch({
) )
], function () { ], function () {
request({ request({
uri: 'http://127.0.0.1:' + that.port, uri: protocol + '://127.0.0.1:' + that.port,
headers: { headers: {
host: 'dynamic.com' host: 'dynamic.com'
} }

View File

@ -30,6 +30,7 @@ exports.assertRequest = function (options) {
request(options.request, this.callback); request(options.request, this.callback);
}, },
"should succeed": function (err, res, body) { "should succeed": function (err, res, body) {
assert.isNull(err);
if (options.assert.body) { if (options.assert.body) {
assert.equal(body, options.assert.body); assert.equal(body, options.assert.body);
} }
@ -57,9 +58,10 @@ exports.assertProxied = function (options) {
var ports = options.ports || helpers.nextPortPair, var ports = options.ports || helpers.nextPortPair,
output = options.output || 'hello world from ' + ports.target, output = options.output || 'hello world from ' + ports.target,
protocol = helpers.protocols.proxy,
req = options.request || {}; 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 { return {
topic: function () { topic: function () {
@ -79,6 +81,7 @@ exports.assertProxied = function (options) {
proxy: { proxy: {
forward: options.forward, forward: options.forward,
target: { target: {
https: helpers.protocols.target === 'https',
host: '127.0.0.1', host: '127.0.0.1',
port: ports.target port: ports.target
} }
@ -108,9 +111,11 @@ exports.assertInvalidProxy = function (options) {
options = options || {}; options = options || {};
var ports = options.ports || helpers.nextPortPair, var ports = options.ports || helpers.nextPortPair,
req = options.request || {}; 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 { return {
topic: function () { topic: function () {
@ -196,6 +201,7 @@ exports.assertProxiedToRoutes = function (options, nested) {
// //
var locations = helpers.http.parseRoutes(options), var locations = helpers.http.parseRoutes(options),
port = helpers.nextPort, port = helpers.nextPort,
protocol = helpers.protocols.proxy,
context, context,
proxy; proxy;
@ -217,6 +223,13 @@ exports.assertProxiedToRoutes = function (options, nested) {
}; };
} }
//
// Set the https options if necessary
//
if (helpers.protocols.target === 'https') {
proxy.target = { https: true };
}
// //
// Create the test context which creates all target // Create the test context which creates all target
// servers for all routes and a proxy server. // servers for all routes and a proxy server.
@ -271,7 +284,7 @@ exports.assertProxiedToRoutes = function (options, nested) {
"a request to unknown.com": exports.assertRequest({ "a request to unknown.com": exports.assertRequest({
assert: { statusCode: 404 }, assert: { statusCode: 404 },
request: { request: {
uri: 'http://127.0.0.1:' + port, uri: protocol + '://127.0.0.1:' + port,
headers: { headers: {
host: 'unknown.com' host: 'unknown.com'
} }
@ -285,7 +298,7 @@ exports.assertProxiedToRoutes = function (options, nested) {
locations.forEach(function (location) { locations.forEach(function (location) {
context[location.source.href] = exports.assertRequest({ context[location.source.href] = exports.assertRequest({
request: { request: {
uri: 'http://127.0.0.1:' + port + location.source.path, uri: protocol + '://127.0.0.1:' + port + location.source.path,
headers: { headers: {
host: location.source.hostname host: location.source.hostname
} }