-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix test] Fix examples to use newest version of socket.io and helper…
…s. Added tests for ensuring that examples require as expected with no errors.
- Loading branch information
Showing
8 changed files
with
158 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 + ' [email protected]'.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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 + ' [email protected]'.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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 + ' [email protected]'.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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters