[fix test] Fix examples to use newest version of socket.io and helpers. Added tests for ensuring that examples require as expected with no errors.

This commit is contained in:
indexzero 2012-07-26 04:29:47 -04:00
parent 82da8535c5
commit fd648a5290
8 changed files with 158 additions and 86 deletions

View File

@ -31,8 +31,6 @@ var https = require('https'),
httpProxy = require('../../lib/node-http-proxy'), httpProxy = require('../../lib/node-http-proxy'),
helpers = require('../../test/helpers'); helpers = require('../../test/helpers');
var opts = helpers.loadHttps();
// //
// Create the target HTTPS server // Create the target HTTPS server
// //
@ -46,7 +44,7 @@ http.createServer(function (req, res) {
// Create the proxy server listening on port 443 // Create the proxy server listening on port 443
// //
httpProxy.createServer(8000, 'localhost', { httpProxy.createServer(8000, 'localhost', {
https: opts https: helpers.https
}).listen(8080); }).listen(8080);
util.puts('https proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8080'.yellow); util.puts('https proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8080'.yellow);

View File

@ -31,12 +31,10 @@ var https = require('https'),
httpProxy = require('../../lib/node-http-proxy'), httpProxy = require('../../lib/node-http-proxy'),
helpers = require('../../test/helpers'); helpers = require('../../test/helpers');
var opts = helpers.loadHttps();
// //
// Create the target HTTPS server // Create the target HTTPS server
// //
https.createServer(opts, function (req, res) { https.createServer(helpers.https, function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' }); res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('hello https\n'); res.write('hello https\n');
res.end(); res.end();
@ -46,7 +44,7 @@ https.createServer(opts, function (req, res) {
// Create the proxy server listening on port 443 // Create the proxy server listening on port 443
// //
httpProxy.createServer(8000, 'localhost', { httpProxy.createServer(8000, 'localhost', {
https: opts, https: helpers.https,
target: { target: {
https: true https: true
} }

View File

@ -27,41 +27,31 @@
var util = require('util'), var util = require('util'),
http = require('http'), http = require('http'),
colors = require('colors'), colors = require('colors'),
websocket = require('../../vendor/websocket'),
httpProxy = require('../../lib/node-http-proxy'); httpProxy = require('../../lib/node-http-proxy');
try { try {
var utils = require('socket.io/lib/socket.io/utils'), var io = require('socket.io'),
io = require('socket.io'); client = require('socket.io-client');
} }
catch (ex) { catch (ex) {
console.error('Socket.io is required for this example:'); console.error('Socket.io is required for this example:');
console.error('npm ' + 'install'.green + ' socket.io@0.6.18'.magenta); console.error('npm ' + 'install'.green);
process.exit(1); process.exit(1);
} }
// //
// Create the target HTTP server // Create the target HTTP server and setup
// socket.io on it.
// //
var server = http.createServer(function (req, res) { var server = io.listen(8080);
res.writeHead(200); server.sockets.on('connection', function (client) {
res.end();
});
server.listen(8080);
//
// Setup socket.io on the target HTTP server
//
var socket = io.listen(server);
socket.on('connection', function (client) {
util.debug('Got websocket connection'); util.debug('Got websocket connection');
client.on('message', function (msg) { client.on('message', function (msg) {
util.debug('Got message from client: ' + msg); util.debug('Got message from client: ' + msg);
}); });
socket.broadcast('from server'); client.send('from server');
}); });
// //
@ -73,6 +63,7 @@ var proxy = new httpProxy.HttpProxy({
port: 8080 port: 8080
} }
}); });
var proxyServer = http.createServer(function (req, res) { var proxyServer = http.createServer(function (req, res) {
proxy.proxyRequest(req, res); proxy.proxyRequest(req, res);
}); });
@ -92,14 +83,10 @@ proxyServer.on('upgrade', function (req, socket, head) {
proxyServer.listen(8081); proxyServer.listen(8081);
// //
// Setup the web socket against our proxy // Setup the socket.io client against our proxy
// //
var ws = new websocket.WebSocket('ws://localhost:8081/socket.io/websocket/', 'borf'); var ws = client.connect('ws://localhost:8081');
ws.on('open', function () {
ws.send(utils.encode('from client'));
});
ws.on('message', function (msg) { ws.on('message', function (msg) {
util.debug('Got message: ' + utils.decode(msg)); util.debug('Got message: ' + msg);
}); });

View File

@ -27,41 +27,31 @@
var util = require('util'), var util = require('util'),
http = require('http'), http = require('http'),
colors = require('colors'), colors = require('colors'),
websocket = require('../../vendor/websocket'),
httpProxy = require('../../lib/node-http-proxy'); httpProxy = require('../../lib/node-http-proxy');
try { try {
var utils = require('socket.io/lib/socket.io/utils'), var io = require('socket.io'),
io = require('socket.io'); client = require('socket.io-client');
} }
catch (ex) { catch (ex) {
console.error('Socket.io is required for this example:'); console.error('Socket.io is required for this example:');
console.error('npm ' + 'install'.green + ' socket.io@0.6.18'.magenta); console.error('npm ' + 'install'.green);
process.exit(1); process.exit(1);
} }
// //
// Create the target HTTP server // Create the target HTTP server and setup
// socket.io on it.
// //
var server = http.createServer(function (req, res) { var server = io.listen(8080);
res.writeHead(200); server.sockets.on('connection', function (client) {
res.end();
});
server.listen(8080);
//
// Setup socket.io on the target HTTP server
//
var socket = io.listen(server);
socket.on('connection', function (client) {
util.debug('Got websocket connection'); util.debug('Got websocket connection');
client.on('message', function (msg) { client.on('message', function (msg) {
util.debug('Got message from client: ' + msg); util.debug('Got message from client: ' + msg);
}); });
socket.broadcast('from server'); client.send('from server');
}); });
// //
@ -88,14 +78,10 @@ proxyServer.on('upgrade', function (req, socket, head) {
proxyServer.listen(8081); proxyServer.listen(8081);
// //
// Setup the web socket against our proxy // Setup the socket.io client against our proxy
// //
var ws = new websocket.WebSocket('ws://localhost:8081/socket.io/websocket/', 'borf'); var ws = client.connect('ws://localhost:8081');
ws.on('open', function () {
ws.send(utils.encode('from client'));
});
ws.on('message', function (msg) { ws.on('message', function (msg) {
util.debug('Got message: ' + utils.decode(msg)); util.debug('Got message: ' + msg);
}); });

View File

@ -27,58 +27,43 @@
var util = require('util'), var util = require('util'),
http = require('http'), http = require('http'),
colors = require('colors'), colors = require('colors'),
websocket = require('../../vendor/websocket'),
httpProxy = require('../../lib/node-http-proxy'); httpProxy = require('../../lib/node-http-proxy');
try { try {
var utils = require('socket.io/lib/socket.io/utils'), var io = require('socket.io'),
io = require('socket.io'); client = require('socket.io-client');
} }
catch (ex) { catch (ex) {
console.error('Socket.io is required for this example:'); console.error('Socket.io is required for this example:');
console.error('npm ' + 'install'.green + ' socket.io@0.6.18'.magenta); console.error('npm ' + 'install'.green);
process.exit(1); process.exit(1);
} }
// //
// Create the target HTTP server // Create the target HTTP server and setup
// socket.io on it.
// //
var server = http.createServer(function (req, res) { var server = io.listen(8080);
res.writeHead(200); server.sockets.on('connection', function (client) {
res.end();
});
server.listen(8080);
//
// Setup socket.io on the target HTTP server
//
var socket = io.listen(server);
socket.on('connection', function (client) {
util.debug('Got websocket connection'); util.debug('Got websocket connection');
client.on('message', function (msg) { client.on('message', function (msg) {
util.debug('Got message from client: ' + msg); util.debug('Got message from client: ' + msg);
}); });
socket.broadcast('from server'); client.send('from server');
}); });
// //
// Create a proxy server with node-http-proxy // Create a proxy server with node-http-proxy
// //
var proxy = httpProxy.createServer(8080, 'localhost'); httpProxy.createServer(8080, 'localhost').listen(8081);
proxy.listen(8081);
// //
// Setup the web socket against our proxy // Setup the socket.io client against our proxy
// //
var ws = new websocket.WebSocket('ws://localhost:8081/socket.io/websocket/', 'borf'); var ws = client.connect('ws://localhost:8081');
ws.on('open', function () {
ws.send(utils.encode('from client'));
});
ws.on('message', function (msg) { ws.on('message', function (msg) {
util.debug('Got message: ' + utils.decode(msg)); util.debug('Got message: ' + msg);
}); });

16
test/examples-test.js Normal file
View File

@ -0,0 +1,16 @@
/*
* examples.js: Tests which ensure all examples do not throw errors.
*
* (C) 2010, Charlie Robbins
*
*/
var vows = require('vows')
macros = require('./macros'),
examples = macros.examples;
vows.describe('node-http-proxy/examples').addBatch(
examples.shouldHaveDeps()
).addBatch(
examples.shouldHaveNoErrors()
).export(module);

101
test/macros/examples.js Normal file
View File

@ -0,0 +1,101 @@
/*
* examples.js: Macros for testing code in examples/
*
* (C) 2010 Nodejitsu Inc.
* MIT LICENCE
*
*/
var assert = require('assert'),
fs = require('fs'),
path = require('path'),
spawn = require('child_process').spawn,
async = require('async');
var rootDir = path.join(__dirname, '..', '..'),
examplesDir = path.join(rootDir, 'examples');
//
// ### function shouldHaveDeps ()
//
// Ensures that all `npm` dependencies are installed in `/examples`.
//
exports.shouldHaveDeps = function () {
return {
"Before testing examples": {
topic: function () {
async.waterfall([
//
// 1. Read files in examples dir
//
async.apply(fs.readdir, examplesDir),
//
// 2. If node_modules exists, continue. Otherwise
// exec `npm` to install them
//
function checkNodeModules(files, next) {
if (files.indexOf('node_modules') !== -1) {
return next();
}
var child = spawn('npm', ['install'], {
cwd: examplesDir
});
child.on('exit', function (code) {
return code
? next(new Error('npm install exited with non-zero exit code'))
: next();
});
},
//
// 3. Read files in examples dir again to ensure the install
// worked as expected.
//
async.apply(fs.readdir, examplesDir),
], this.callback);
},
"examples/node_modules should exist": function (err, files) {
assert.notEqual(files.indexOf('node_modules'), -1);
}
}
}
};
//
// ### function shouldRequire (file)
// #### @file {string} File to attempt to require
//
// Returns a test which attempts to require `file`.
//
exports.shouldRequire = function (file) {
return {
"should have no errors": function () {
try { assert.isObject(require(file)) }
catch (ex) { assert.isNull(ex) }
}
};
};
//
// ### function shouldHaveNoErrors ()
//
// Returns a vows context that attempts to require
// every relevant example file in `examples`.
//
exports.shouldHaveNoErrors = function () {
var context = {};
['balancer', 'http', 'middleware', 'websocket'].forEach(function (dir) {
var name = 'examples/' + dir,
files = fs.readdirSync(path.join(rootDir, 'examples', dir));
files.forEach(function (file) {
context[name + '/' + file] = exports.shouldRequire(path.join(
examplesDir, dir, file
));
});
});
return context;
};

View File

@ -6,5 +6,6 @@
* *
*/ */
exports.examples = require('./examples');
exports.http = require('./http'); exports.http = require('./http');
exports.ws = require('./ws'); exports.ws = require('./ws');