Skip to content

Commit

Permalink
[fix test] Fix examples to use newest version of socket.io and helper…
Browse files Browse the repository at this point in the history
…s. Added tests for ensuring that examples require as expected with no errors.
  • Loading branch information
indexzero committed Jul 26, 2012
1 parent 82da853 commit fd648a5
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 86 deletions.
4 changes: 1 addition & 3 deletions examples/http/proxy-https-to-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ var https = require('https'),
httpProxy = require('../../lib/node-http-proxy'),
helpers = require('../../test/helpers');

var opts = helpers.loadHttps();

//
// Create the target HTTPS server
//
Expand All @@ -46,7 +44,7 @@ http.createServer(function (req, res) {
// Create the proxy server listening on port 443
//
httpProxy.createServer(8000, 'localhost', {
https: opts
https: helpers.https
}).listen(8080);

util.puts('https proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8080'.yellow);
Expand Down
6 changes: 2 additions & 4 deletions examples/http/proxy-https-to-https.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ var https = require('https'),
httpProxy = require('../../lib/node-http-proxy'),
helpers = require('../../test/helpers');

var opts = helpers.loadHttps();

//
// 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.write('hello https\n');
res.end();
Expand All @@ -46,7 +44,7 @@ https.createServer(opts, function (req, res) {
// Create the proxy server listening on port 443
//
httpProxy.createServer(8000, 'localhost', {
https: opts,
https: helpers.https,
target: {
https: true
}
Expand Down
37 changes: 12 additions & 25 deletions examples/websocket/latent-websocket-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,31 @@
var util = require('util'),
http = require('http'),
colors = require('colors'),
websocket = require('../../vendor/websocket'),
httpProxy = require('../../lib/node-http-proxy');

try {
var utils = require('socket.io/lib/socket.io/utils'),
io = require('socket.io');
var io = require('socket.io'),
client = require('socket.io-client');
}
catch (ex) {
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);
}

//
// Create the target HTTP server
// Create the target HTTP server and setup
// socket.io on it.
//
var server = http.createServer(function (req, res) {
res.writeHead(200);
res.end();
});

server.listen(8080);

//
// Setup socket.io on the target HTTP server
//
var socket = io.listen(server);
socket.on('connection', function (client) {
var server = io.listen(8080);
server.sockets.on('connection', function (client) {
util.debug('Got websocket connection');

client.on('message', function (msg) {
util.debug('Got message from client: ' + msg);
});

socket.broadcast('from server');
client.send('from server');
});

//
Expand All @@ -73,6 +63,7 @@ var proxy = new httpProxy.HttpProxy({
port: 8080
}
});

var proxyServer = http.createServer(function (req, res) {
proxy.proxyRequest(req, res);
});
Expand All @@ -92,14 +83,10 @@ proxyServer.on('upgrade', function (req, socket, head) {
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');

ws.on('open', function () {
ws.send(utils.encode('from client'));
});
var ws = client.connect('ws://localhost:8081');

ws.on('message', function (msg) {
util.debug('Got message: ' + utils.decode(msg));
util.debug('Got message: ' + msg);
});
36 changes: 11 additions & 25 deletions examples/websocket/standalone-websocket-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,31 @@
var util = require('util'),
http = require('http'),
colors = require('colors'),
websocket = require('../../vendor/websocket'),
httpProxy = require('../../lib/node-http-proxy');

try {
var utils = require('socket.io/lib/socket.io/utils'),
io = require('socket.io');
var io = require('socket.io'),
client = require('socket.io-client');
}
catch (ex) {
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);
}

//
// Create the target HTTP server
// Create the target HTTP server and setup
// socket.io on it.
//
var server = http.createServer(function (req, res) {
res.writeHead(200);
res.end();
});

server.listen(8080);

//
// Setup socket.io on the target HTTP server
//
var socket = io.listen(server);
socket.on('connection', function (client) {
var server = io.listen(8080);
server.sockets.on('connection', function (client) {
util.debug('Got websocket connection');

client.on('message', function (msg) {
util.debug('Got message from client: ' + msg);
});

socket.broadcast('from server');
client.send('from server');
});

//
Expand All @@ -88,14 +78,10 @@ proxyServer.on('upgrade', function (req, socket, head) {
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');

ws.on('open', function () {
ws.send(utils.encode('from client'));
});
var ws = client.connect('ws://localhost:8081');

ws.on('message', function (msg) {
util.debug('Got message: ' + utils.decode(msg));
util.debug('Got message: ' + msg);
});
39 changes: 12 additions & 27 deletions examples/websocket/websocket-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,58 +27,43 @@
var util = require('util'),
http = require('http'),
colors = require('colors'),
websocket = require('../../vendor/websocket'),
httpProxy = require('../../lib/node-http-proxy');

try {
var utils = require('socket.io/lib/socket.io/utils'),
io = require('socket.io');
var io = require('socket.io'),
client = require('socket.io-client');
}
catch (ex) {
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);
}

//
// Create the target HTTP server
// Create the target HTTP server and setup
// socket.io on it.
//
var server = http.createServer(function (req, res) {
res.writeHead(200);
res.end();
});

server.listen(8080);

//
// Setup socket.io on the target HTTP server
//
var socket = io.listen(server);
socket.on('connection', function (client) {
var server = io.listen(8080);
server.sockets.on('connection', function (client) {
util.debug('Got websocket connection');

client.on('message', function (msg) {
util.debug('Got message from client: ' + msg);
});

socket.broadcast('from server');
client.send('from server');
});

//
// Create a proxy server with node-http-proxy
//
var proxy = httpProxy.createServer(8080, 'localhost');
proxy.listen(8081);
httpProxy.createServer(8080, 'localhost').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');

ws.on('open', function () {
ws.send(utils.encode('from client'));
});
var ws = client.connect('ws://localhost:8081');

ws.on('message', function (msg) {
util.debug('Got message: ' + utils.decode(msg));
util.debug('Got message: ' + msg);
});
16 changes: 16 additions & 0 deletions test/examples-test.js
Original file line number Diff line number Diff line change
@@ -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 changes: 101 additions & 0 deletions test/macros/examples.js
Original file line number Diff line number Diff line change
@@ -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;
};
5 changes: 3 additions & 2 deletions test/macros/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
*
*/

exports.http = require('./http');
exports.ws = require('./ws');
exports.examples = require('./examples');
exports.http = require('./http');
exports.ws = require('./ws');

0 comments on commit fd648a5

Please sign in to comment.