From fd648a529090cefc202613fff3fdfec9ba0e6a72 Mon Sep 17 00:00:00 2001 From: indexzero Date: Thu, 26 Jul 2012 04:29:47 -0400 Subject: [PATCH] [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. --- examples/http/proxy-https-to-http.js | 4 +- examples/http/proxy-https-to-https.js | 6 +- examples/websocket/latent-websocket-proxy.js | 37 +++---- .../websocket/standalone-websocket-proxy.js | 36 ++----- examples/websocket/websocket-proxy.js | 39 +++---- test/examples-test.js | 16 +++ test/macros/examples.js | 101 ++++++++++++++++++ test/macros/index.js | 5 +- 8 files changed, 158 insertions(+), 86 deletions(-) create mode 100644 test/examples-test.js create mode 100644 test/macros/examples.js diff --git a/examples/http/proxy-https-to-http.js b/examples/http/proxy-https-to-http.js index d6fce41..378de03 100644 --- a/examples/http/proxy-https-to-http.js +++ b/examples/http/proxy-https-to-http.js @@ -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 // @@ -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); diff --git a/examples/http/proxy-https-to-https.js b/examples/http/proxy-https-to-https.js index 9acef1f..b35ecc3 100644 --- a/examples/http/proxy-https-to-https.js +++ b/examples/http/proxy-https-to-https.js @@ -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(); @@ -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 } diff --git a/examples/websocket/latent-websocket-proxy.js b/examples/websocket/latent-websocket-proxy.js index 338d7ea..99a0728 100644 --- a/examples/websocket/latent-websocket-proxy.js +++ b/examples/websocket/latent-websocket-proxy.js @@ -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'); }); // @@ -73,6 +63,7 @@ var proxy = new httpProxy.HttpProxy({ port: 8080 } }); + var proxyServer = http.createServer(function (req, res) { proxy.proxyRequest(req, res); }); @@ -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); }); diff --git a/examples/websocket/standalone-websocket-proxy.js b/examples/websocket/standalone-websocket-proxy.js index cd4a855..acf43b9 100644 --- a/examples/websocket/standalone-websocket-proxy.js +++ b/examples/websocket/standalone-websocket-proxy.js @@ -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'); }); // @@ -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); }); diff --git a/examples/websocket/websocket-proxy.js b/examples/websocket/websocket-proxy.js index 0b76b8a..4e3cf6f 100644 --- a/examples/websocket/websocket-proxy.js +++ b/examples/websocket/websocket-proxy.js @@ -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); }); diff --git a/test/examples-test.js b/test/examples-test.js new file mode 100644 index 0000000..472fc8a --- /dev/null +++ b/test/examples-test.js @@ -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); \ No newline at end of file diff --git a/test/macros/examples.js b/test/macros/examples.js new file mode 100644 index 0000000..3e16b23 --- /dev/null +++ b/test/macros/examples.js @@ -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; +}; \ No newline at end of file diff --git a/test/macros/index.js b/test/macros/index.js index 72d3249..c01f962 100644 --- a/test/macros/index.js +++ b/test/macros/index.js @@ -6,5 +6,6 @@ * */ -exports.http = require('./http'); -exports.ws = require('./ws'); \ No newline at end of file +exports.examples = require('./examples'); +exports.http = require('./http'); +exports.ws = require('./ws'); \ No newline at end of file