From bc236d7e95ef10bc17cf551eea2cd2fb9bf265eb Mon Sep 17 00:00:00 2001 From: cronopio Date: Wed, 27 Nov 2013 17:58:00 -0500 Subject: [PATCH] [tests] Added a test case for run all the examples * I changed all the ports across examples to be different and can run at same time --- .../simple-balancer-with-websockets.js | 2 +- examples/balancer/simple-balancer.js | 2 +- examples/http/basic-proxy.js | 10 +-- examples/http/concurrent-proxy.js | 10 +-- examples/http/custom-proxy-error.js | 6 +- examples/http/forward-and-target-proxy.js | 16 ++--- examples/http/forward-proxy.js | 10 +-- examples/http/latent-proxy.js | 10 +-- examples/http/proxy-http-to-https.js | 4 +- examples/http/proxy-https-to-http.js | 10 +-- examples/http/proxy-https-to-https.js | 10 +-- examples/http/standalone-proxy.js | 10 +-- examples/middleware/gzip-middleware.js | 10 +-- .../middleware/modifyResponse-middleware.js | 10 +-- examples/websocket/latent-websocket-proxy.js | 8 +-- .../websocket/standalone-websocket-proxy.js | 8 +-- examples/websocket/websocket-proxy.js | 6 +- package.json | 3 +- test/examples-test.js | 71 +++++++++++++++++++ 19 files changed, 144 insertions(+), 72 deletions(-) create mode 100644 test/examples-test.js diff --git a/examples/balancer/simple-balancer-with-websockets.js b/examples/balancer/simple-balancer-with-websockets.js index b17afc772..cc13f4b5c 100644 --- a/examples/balancer/simple-balancer-with-websockets.js +++ b/examples/balancer/simple-balancer-with-websockets.js @@ -80,5 +80,5 @@ server.on('upgrade', function (req, socket, head) { nextProxy().ws(req, socket, head); }); -server.listen(8080); +server.listen(8001); \ No newline at end of file diff --git a/examples/balancer/simple-balancer.js b/examples/balancer/simple-balancer.js index 80b91760c..3f53cc63d 100644 --- a/examples/balancer/simple-balancer.js +++ b/examples/balancer/simple-balancer.js @@ -59,6 +59,6 @@ http.createServer(function (req, res) { // ...and then the server you just used becomes the last item in the list. // addresses.push(target); -}).listen(8000); +}).listen(8021); // Rinse; repeat; enjoy. \ No newline at end of file diff --git a/examples/http/basic-proxy.js b/examples/http/basic-proxy.js index 640318c11..e9be0d79b 100644 --- a/examples/http/basic-proxy.js +++ b/examples/http/basic-proxy.js @@ -44,8 +44,8 @@ util.puts(welcome.rainbow.bold); // Basic Http Proxy Server // httpProxy.createServer({ - target:'http://localhost:9000' -}).listen(8000); + target:'http://localhost:9003' +}).listen(8003); // // Target Http Server @@ -54,7 +54,7 @@ http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2)); res.end(); -}).listen(9000); +}).listen(9003); -util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow); -util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow); +util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8003'.yellow); +util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9003 '.yellow); diff --git a/examples/http/concurrent-proxy.js b/examples/http/concurrent-proxy.js index 5ca1054b8..30aa53dd6 100644 --- a/examples/http/concurrent-proxy.js +++ b/examples/http/concurrent-proxy.js @@ -33,8 +33,8 @@ var util = require('util'), // Basic Http Proxy Server // httpProxy.createServer({ - target:'http://localhost:9000' -}).listen(8000); + target:'http://localhost:9004' +}).listen(8004); // // Target Http Server @@ -62,7 +62,7 @@ http.createServer(function (req, res) { connections.shift()(); } } -}).listen(9000); +}).listen(9004); -util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow); -util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow); +util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8004'.yellow); +util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9004 '.yellow); diff --git a/examples/http/custom-proxy-error.js b/examples/http/custom-proxy-error.js index dd62273a7..1c54b5ab8 100644 --- a/examples/http/custom-proxy-error.js +++ b/examples/http/custom-proxy-error.js @@ -33,13 +33,13 @@ var util = require('util'), // Http Proxy Server with bad target // var proxy = httpProxy.createServer({ - target:'http://localhost:9000' + target:'http://localhost:9005' }); // // Tell the proxy to listen on port 8000 // -proxy.listen(8000); +proxy.listen(8005); // // Listen for the `error` event on `proxy`. @@ -52,4 +52,4 @@ proxy.on('error', function (err, req, res) { }); -util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8000 '.yellow + 'with custom error message'.magenta.underline); \ No newline at end of file +util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8005 '.yellow + 'with custom error message'.magenta.underline); \ No newline at end of file diff --git a/examples/http/forward-and-target-proxy.js b/examples/http/forward-and-target-proxy.js index 0d5acd1f6..c564bfbbd 100644 --- a/examples/http/forward-and-target-proxy.js +++ b/examples/http/forward-and-target-proxy.js @@ -34,14 +34,14 @@ var util = require('util'), // httpProxy.createServer({ target: { - port: 9000, + port: 9006, host: 'localhost' }, forward: { - port: 9001, + port: 9007, host: 'localhost' } -}).listen(8000); +}).listen(8006); // // Target Http Server @@ -50,7 +50,7 @@ http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2)); res.end(); -}).listen(9000); +}).listen(9006); // // Target Http Forwarding Server @@ -60,8 +60,8 @@ http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.write('request successfully forwarded to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2)); res.end(); -}).listen(9001); +}).listen(9007); -util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8000 '.yellow + 'with forward proxy'.magenta.underline); -util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow); -util.puts('http forward server '.blue + 'started '.green.bold + 'on port '.blue + '9001 '.yellow); \ No newline at end of file +util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8006 '.yellow + 'with forward proxy'.magenta.underline); +util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9006 '.yellow); +util.puts('http forward server '.blue + 'started '.green.bold + 'on port '.blue + '9007 '.yellow); \ No newline at end of file diff --git a/examples/http/forward-proxy.js b/examples/http/forward-proxy.js index 5f93c49f6..d94f48414 100644 --- a/examples/http/forward-proxy.js +++ b/examples/http/forward-proxy.js @@ -34,10 +34,10 @@ var util = require('util'), // httpProxy.createServer({ forward: { - port: 9000, + port: 9019, host: 'localhost' } -}).listen(8000); +}).listen(8019); // // Target Http Forwarding Server @@ -47,7 +47,7 @@ http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.write('request successfully forwarded to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2)); res.end(); -}).listen(9000); +}).listen(9019); -util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8000 '.yellow + 'with forward proxy'.magenta.underline); -util.puts('http forward server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow); \ No newline at end of file +util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8019 '.yellow + 'with forward proxy'.magenta.underline); +util.puts('http forward server '.blue + 'started '.green.bold + 'on port '.blue + '9019 '.yellow); \ No newline at end of file diff --git a/examples/http/latent-proxy.js b/examples/http/latent-proxy.js index 85de6338f..01ec93cc7 100644 --- a/examples/http/latent-proxy.js +++ b/examples/http/latent-proxy.js @@ -36,10 +36,10 @@ var proxy = httpProxy.createProxyServer(); http.createServer(function (req, res) { setTimeout(function () { proxy.web(req, res, { - target: 'http://localhost:9000' + target: 'http://localhost:9008' }); }, 500); -}).listen(8000); +}).listen(8008); // // Target Http Server @@ -48,7 +48,7 @@ http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2)); res.end(); -}).listen(9000); +}).listen(9008); -util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8000 '.yellow + 'with latency'.magenta.underline); -util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow); +util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8008 '.yellow + 'with latency'.magenta.underline); +util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9008 '.yellow); diff --git a/examples/http/proxy-http-to-https.js b/examples/http/proxy-http-to-https.js index ffaa6fa04..ba5c83816 100644 --- a/examples/http/proxy-http-to-https.js +++ b/examples/http/proxy-http-to-https.js @@ -41,6 +41,6 @@ httpProxy.createProxyServer({ headers: { host: 'google.com' } -}).listen(8000); +}).listen(8011); -util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow); \ No newline at end of file +util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8011'.yellow); \ No newline at end of file diff --git a/examples/http/proxy-https-to-http.js b/examples/http/proxy-https-to-http.js index 1b6ac92bd..d2a2d5c0d 100644 --- a/examples/http/proxy-https-to-http.js +++ b/examples/http/proxy-https-to-http.js @@ -40,7 +40,7 @@ http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.write('hello http over https\n'); res.end(); -}).listen(9000); +}).listen(9009); // // Create the HTTPS proxy server listening on port 8000 @@ -48,13 +48,13 @@ http.createServer(function (req, res) { httpProxy.createServer({ target: { host: 'localhost', - port: 9000 + port: 9009 }, ssl: { key: fs.readFileSync(path.join(fixturesDir, 'agent2-key.pem'), 'utf8'), cert: fs.readFileSync(path.join(fixturesDir, 'agent2-cert.pem'), 'utf8') } -}).listen(8000); +}).listen(8009); -util.puts('https proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow); -util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow); +util.puts('https proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8009'.yellow); +util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9009 '.yellow); diff --git a/examples/http/proxy-https-to-https.js b/examples/http/proxy-https-to-https.js index f0d06e8e4..e543f98a7 100644 --- a/examples/http/proxy-https-to-https.js +++ b/examples/http/proxy-https-to-https.js @@ -44,16 +44,16 @@ https.createServer(httpsOpts, function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.write('hello https\n'); res.end(); -}).listen(9000); +}).listen(9010); // // Create the proxy server listening on port 443 // httpProxy.createServer({ ssl: httpsOpts, - target: 'https://localhost:9000', + target: 'https://localhost:9010', secure: false -}).listen(8000); +}).listen(8010); -util.puts('https proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow); -util.puts('https server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow); +util.puts('https proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8010'.yellow); +util.puts('https server '.blue + 'started '.green.bold + 'on port '.blue + '9010 '.yellow); diff --git a/examples/http/standalone-proxy.js b/examples/http/standalone-proxy.js index 7fc97b3d7..410d70b31 100644 --- a/examples/http/standalone-proxy.js +++ b/examples/http/standalone-proxy.js @@ -36,10 +36,10 @@ var proxy = new httpProxy.createProxyServer(); http.createServer(function (req, res) { setTimeout(function () { proxy.web(req, res, { - target: 'http://localhost:9000' + target: 'http://localhost:9002' }); }, 200); -}).listen(8000); +}).listen(8002); // // Target Http Server @@ -48,7 +48,7 @@ http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2)); res.end(); -}).listen(9000); +}).listen(9002); -util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '8000 '.yellow + 'with proxy.web() handler'.cyan.underline + ' and latency'.magenta); -util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow); +util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '8002 '.yellow + 'with proxy.web() handler'.cyan.underline + ' and latency'.magenta); +util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9001 '.yellow); diff --git a/examples/middleware/gzip-middleware.js b/examples/middleware/gzip-middleware.js index ee32e445b..756b68fa3 100644 --- a/examples/middleware/gzip-middleware.js +++ b/examples/middleware/gzip-middleware.js @@ -43,13 +43,13 @@ connect.createServer( function (req, res) { proxy.web(req, res); } -).listen(8000); +).listen(8012); // // Basic Http Proxy Server // var proxy = httpProxy.createProxyServer({ - target: 'http://localhost:9000' + target: 'http://localhost:9012' }); // @@ -59,7 +59,7 @@ http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2)); res.end(); -}).listen(9000); +}).listen(9012); -util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow); -util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow); +util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8012'.yellow); +util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9012 '.yellow); diff --git a/examples/middleware/modifyResponse-middleware.js b/examples/middleware/modifyResponse-middleware.js index c4a59a2f0..fdd7e6596 100644 --- a/examples/middleware/modifyResponse-middleware.js +++ b/examples/middleware/modifyResponse-middleware.js @@ -45,13 +45,13 @@ connect.createServer( function (req, res) { proxy.web(req, res); } -).listen(8000); +).listen(8013); // // Basic Http Proxy Server // var proxy = httpProxy.createProxyServer({ - target: 'http://localhost:9000' + target: 'http://localhost:9013' }); // @@ -60,8 +60,8 @@ var proxy = httpProxy.createProxyServer({ http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello, I know Ruby\n'); -}).listen(9000); +}).listen(9013); -util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow); -util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow); +util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8013'.yellow); +util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9013 '.yellow); diff --git a/examples/websocket/latent-websocket-proxy.js b/examples/websocket/latent-websocket-proxy.js index a80a16edd..64d3d7ce0 100644 --- a/examples/websocket/latent-websocket-proxy.js +++ b/examples/websocket/latent-websocket-proxy.js @@ -43,7 +43,7 @@ catch (ex) { // Create the target HTTP server and setup // socket.io on it. // -var server = io.listen(9000); +var server = io.listen(9016); server.sockets.on('connection', function (client) { util.debug('Got websocket connection'); @@ -60,7 +60,7 @@ server.sockets.on('connection', function (client) { var proxy = new httpProxy.createProxyServer({ target: { host: 'localhost', - port: 9000 + port: 9016 } }); @@ -78,12 +78,12 @@ proxyServer.on('upgrade', function (req, socket, head) { }, 1000); }); -proxyServer.listen(8000); +proxyServer.listen(8016); // // Setup the socket.io client against our proxy // -var ws = client.connect('ws://localhost:8000'); +var ws = client.connect('ws://localhost:8016'); ws.on('message', function (msg) { util.debug('Got message: ' + msg); diff --git a/examples/websocket/standalone-websocket-proxy.js b/examples/websocket/standalone-websocket-proxy.js index a78ecedbc..81d019650 100644 --- a/examples/websocket/standalone-websocket-proxy.js +++ b/examples/websocket/standalone-websocket-proxy.js @@ -43,7 +43,7 @@ catch (ex) { // Create the target HTTP server and setup // socket.io on it. // -var server = io.listen(9000); +var server = io.listen(9015); server.sockets.on('connection', function (client) { util.debug('Got websocket connection'); @@ -60,7 +60,7 @@ server.sockets.on('connection', function (client) { var proxy = new httpProxy.createProxyServer({ target: { host: 'localhost', - port: 9000 + port: 9015 } }); var proxyServer = http.createServer(function (req, res) { @@ -75,12 +75,12 @@ proxyServer.on('upgrade', function (req, socket, head) { proxy.ws(req, socket, head); }); -proxyServer.listen(8000); +proxyServer.listen(8015); // // Setup the socket.io client against our proxy // -var ws = client.connect('ws://localhost:8000'); +var ws = client.connect('ws://localhost:8015'); ws.on('message', function (msg) { util.debug('Got message: ' + msg); diff --git a/examples/websocket/websocket-proxy.js b/examples/websocket/websocket-proxy.js index dfd46e064..33d78c675 100644 --- a/examples/websocket/websocket-proxy.js +++ b/examples/websocket/websocket-proxy.js @@ -43,7 +43,7 @@ catch (ex) { // Create the target HTTP server and setup // socket.io on it. // -var server = io.listen(9000); +var server = io.listen(9014); server.sockets.on('connection', function (client) { util.debug('Got websocket connection'); @@ -57,12 +57,12 @@ server.sockets.on('connection', function (client) { // // Create a proxy server with node-http-proxy // -httpProxy.createServer({ target: 'ws://localhost:9000', ws: true }).listen(8000); +httpProxy.createServer({ target: 'ws://localhost:9014', ws: true }).listen(8014); // // Setup the socket.io client against our proxy // -var ws = client.connect('ws://localhost:8000'); +var ws = client.connect('ws://localhost:8014'); ws.on('message', function (msg) { util.debug('Got message: ' + msg); diff --git a/package.json b/package.json index 98cd9b5d4..5c7c3a1eb 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "blanket" : "*", "ws" : "*", "socket.io" : "*", - "socket.io-client" : "*" + "socket.io-client" : "*", + "async" : "*" }, "scripts" : { "coveralls" : "mocha --require blanket --reporter mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js", diff --git a/test/examples-test.js b/test/examples-test.js new file mode 100644 index 000000000..234587bcb --- /dev/null +++ b/test/examples-test.js @@ -0,0 +1,71 @@ +/* + examples-test.js: Test to run all the examples + + Copyright (c) Nodejitsu 2013 + +*/ +var path = require('path'), + fs = require('fs'), + spawn = require('child_process').spawn, + expect = require('expect.js'), + async = require('async'); + +var rootDir = path.join(__dirname, '..'), + examplesDir = path.join(rootDir, 'examples'); + +describe('http-proxy examples', function () { + describe('Before testing examples', function () { + // Set a timeout to avoid this error + this.timeout(30 * 1000); + it('should have installed dependencies', function (done) { + 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(); + } + + console.log('Warning: installing dependencies, this operation could take a while'); + + var child = spawn('npm', ['install', '-f'], { + 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), + ], done); + }) + }); + + describe('Requiring all the examples', function () { + it('should have no errors', function (done) { + async.each(['balancer', 'http', 'middleware', 'websocket'], function (dir, cb) { + var name = 'examples/' + dir, + files = fs.readdirSync(path.join(rootDir, 'examples', dir)); + + async.each(files, function (file, callback) { + var example; + expect(function () { example = require(path.join(examplesDir, dir, file)); }).to.not.throwException(); + expect(example).to.be.an('object'); + callback(); + }, cb); + }, done); + }) + }) +}) \ No newline at end of file