Skip to content

Commit fd648a5

Browse files
committed
[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.
1 parent 82da853 commit fd648a5

8 files changed

+158
-86
lines changed

Diff for: examples/http/proxy-https-to-http.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ var https = require('https'),
3131
httpProxy = require('../../lib/node-http-proxy'),
3232
helpers = require('../../test/helpers');
3333

34-
var opts = helpers.loadHttps();
35-
3634
//
3735
// Create the target HTTPS server
3836
//
@@ -46,7 +44,7 @@ http.createServer(function (req, res) {
4644
// Create the proxy server listening on port 443
4745
//
4846
httpProxy.createServer(8000, 'localhost', {
49-
https: opts
47+
https: helpers.https
5048
}).listen(8080);
5149

5250
util.puts('https proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8080'.yellow);

Diff for: examples/http/proxy-https-to-https.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@ var https = require('https'),
3131
httpProxy = require('../../lib/node-http-proxy'),
3232
helpers = require('../../test/helpers');
3333

34-
var opts = helpers.loadHttps();
35-
3634
//
3735
// Create the target HTTPS server
3836
//
39-
https.createServer(opts, function (req, res) {
37+
https.createServer(helpers.https, function (req, res) {
4038
res.writeHead(200, { 'Content-Type': 'text/plain' });
4139
res.write('hello https\n');
4240
res.end();
@@ -46,7 +44,7 @@ https.createServer(opts, function (req, res) {
4644
// Create the proxy server listening on port 443
4745
//
4846
httpProxy.createServer(8000, 'localhost', {
49-
https: opts,
47+
https: helpers.https,
5048
target: {
5149
https: true
5250
}

Diff for: examples/websocket/latent-websocket-proxy.js

+12-25
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,31 @@
2727
var util = require('util'),
2828
http = require('http'),
2929
colors = require('colors'),
30-
websocket = require('../../vendor/websocket'),
3130
httpProxy = require('../../lib/node-http-proxy');
3231

3332
try {
34-
var utils = require('socket.io/lib/socket.io/utils'),
35-
io = require('socket.io');
33+
var io = require('socket.io'),
34+
client = require('socket.io-client');
3635
}
3736
catch (ex) {
3837
console.error('Socket.io is required for this example:');
39-
console.error('npm ' + 'install'.green + ' [email protected]'.magenta);
38+
console.error('npm ' + 'install'.green);
4039
process.exit(1);
4140
}
4241

4342
//
44-
// Create the target HTTP server
43+
// Create the target HTTP server and setup
44+
// socket.io on it.
4545
//
46-
var server = http.createServer(function (req, res) {
47-
res.writeHead(200);
48-
res.end();
49-
});
50-
51-
server.listen(8080);
52-
53-
//
54-
// Setup socket.io on the target HTTP server
55-
//
56-
var socket = io.listen(server);
57-
socket.on('connection', function (client) {
46+
var server = io.listen(8080);
47+
server.sockets.on('connection', function (client) {
5848
util.debug('Got websocket connection');
5949

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

64-
socket.broadcast('from server');
54+
client.send('from server');
6555
});
6656

6757
//
@@ -73,6 +63,7 @@ var proxy = new httpProxy.HttpProxy({
7363
port: 8080
7464
}
7565
});
66+
7667
var proxyServer = http.createServer(function (req, res) {
7768
proxy.proxyRequest(req, res);
7869
});
@@ -92,14 +83,10 @@ proxyServer.on('upgrade', function (req, socket, head) {
9283
proxyServer.listen(8081);
9384

9485
//
95-
// Setup the web socket against our proxy
86+
// Setup the socket.io client against our proxy
9687
//
97-
var ws = new websocket.WebSocket('ws://localhost:8081/socket.io/websocket/', 'borf');
98-
99-
ws.on('open', function () {
100-
ws.send(utils.encode('from client'));
101-
});
88+
var ws = client.connect('ws://localhost:8081');
10289

10390
ws.on('message', function (msg) {
104-
util.debug('Got message: ' + utils.decode(msg));
91+
util.debug('Got message: ' + msg);
10592
});

Diff for: examples/websocket/standalone-websocket-proxy.js

+11-25
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,31 @@
2727
var util = require('util'),
2828
http = require('http'),
2929
colors = require('colors'),
30-
websocket = require('../../vendor/websocket'),
3130
httpProxy = require('../../lib/node-http-proxy');
3231

3332
try {
34-
var utils = require('socket.io/lib/socket.io/utils'),
35-
io = require('socket.io');
33+
var io = require('socket.io'),
34+
client = require('socket.io-client');
3635
}
3736
catch (ex) {
3837
console.error('Socket.io is required for this example:');
39-
console.error('npm ' + 'install'.green + ' [email protected]'.magenta);
38+
console.error('npm ' + 'install'.green);
4039
process.exit(1);
4140
}
4241

4342
//
44-
// Create the target HTTP server
43+
// Create the target HTTP server and setup
44+
// socket.io on it.
4545
//
46-
var server = http.createServer(function (req, res) {
47-
res.writeHead(200);
48-
res.end();
49-
});
50-
51-
server.listen(8080);
52-
53-
//
54-
// Setup socket.io on the target HTTP server
55-
//
56-
var socket = io.listen(server);
57-
socket.on('connection', function (client) {
46+
var server = io.listen(8080);
47+
server.sockets.on('connection', function (client) {
5848
util.debug('Got websocket connection');
5949

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

64-
socket.broadcast('from server');
54+
client.send('from server');
6555
});
6656

6757
//
@@ -88,14 +78,10 @@ proxyServer.on('upgrade', function (req, socket, head) {
8878
proxyServer.listen(8081);
8979

9080
//
91-
// Setup the web socket against our proxy
81+
// Setup the socket.io client against our proxy
9282
//
93-
var ws = new websocket.WebSocket('ws://localhost:8081/socket.io/websocket/', 'borf');
94-
95-
ws.on('open', function () {
96-
ws.send(utils.encode('from client'));
97-
});
83+
var ws = client.connect('ws://localhost:8081');
9884

9985
ws.on('message', function (msg) {
100-
util.debug('Got message: ' + utils.decode(msg));
86+
util.debug('Got message: ' + msg);
10187
});

Diff for: examples/websocket/websocket-proxy.js

+12-27
Original file line numberDiff line numberDiff line change
@@ -27,58 +27,43 @@
2727
var util = require('util'),
2828
http = require('http'),
2929
colors = require('colors'),
30-
websocket = require('../../vendor/websocket'),
3130
httpProxy = require('../../lib/node-http-proxy');
3231

3332
try {
34-
var utils = require('socket.io/lib/socket.io/utils'),
35-
io = require('socket.io');
33+
var io = require('socket.io'),
34+
client = require('socket.io-client');
3635
}
3736
catch (ex) {
3837
console.error('Socket.io is required for this example:');
39-
console.error('npm ' + 'install'.green + ' [email protected]'.magenta);
38+
console.error('npm ' + 'install'.green);
4039
process.exit(1);
4140
}
4241

4342
//
44-
// Create the target HTTP server
43+
// Create the target HTTP server and setup
44+
// socket.io on it.
4545
//
46-
var server = http.createServer(function (req, res) {
47-
res.writeHead(200);
48-
res.end();
49-
});
50-
51-
server.listen(8080);
52-
53-
//
54-
// Setup socket.io on the target HTTP server
55-
//
56-
var socket = io.listen(server);
57-
socket.on('connection', function (client) {
46+
var server = io.listen(8080);
47+
server.sockets.on('connection', function (client) {
5848
util.debug('Got websocket connection');
5949

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

64-
socket.broadcast('from server');
54+
client.send('from server');
6555
});
6656

6757
//
6858
// Create a proxy server with node-http-proxy
6959
//
70-
var proxy = httpProxy.createServer(8080, 'localhost');
71-
proxy.listen(8081);
60+
httpProxy.createServer(8080, 'localhost').listen(8081);
7261

7362
//
74-
// Setup the web socket against our proxy
63+
// Setup the socket.io client against our proxy
7564
//
76-
var ws = new websocket.WebSocket('ws://localhost:8081/socket.io/websocket/', 'borf');
77-
78-
ws.on('open', function () {
79-
ws.send(utils.encode('from client'));
80-
});
65+
var ws = client.connect('ws://localhost:8081');
8166

8267
ws.on('message', function (msg) {
83-
util.debug('Got message: ' + utils.decode(msg));
68+
util.debug('Got message: ' + msg);
8469
});

Diff for: test/examples-test.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* examples.js: Tests which ensure all examples do not throw errors.
3+
*
4+
* (C) 2010, Charlie Robbins
5+
*
6+
*/
7+
8+
var vows = require('vows')
9+
macros = require('./macros'),
10+
examples = macros.examples;
11+
12+
vows.describe('node-http-proxy/examples').addBatch(
13+
examples.shouldHaveDeps()
14+
).addBatch(
15+
examples.shouldHaveNoErrors()
16+
).export(module);

Diff for: test/macros/examples.js

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* examples.js: Macros for testing code in examples/
3+
*
4+
* (C) 2010 Nodejitsu Inc.
5+
* MIT LICENCE
6+
*
7+
*/
8+
9+
var assert = require('assert'),
10+
fs = require('fs'),
11+
path = require('path'),
12+
spawn = require('child_process').spawn,
13+
async = require('async');
14+
15+
var rootDir = path.join(__dirname, '..', '..'),
16+
examplesDir = path.join(rootDir, 'examples');
17+
18+
//
19+
// ### function shouldHaveDeps ()
20+
//
21+
// Ensures that all `npm` dependencies are installed in `/examples`.
22+
//
23+
exports.shouldHaveDeps = function () {
24+
return {
25+
"Before testing examples": {
26+
topic: function () {
27+
async.waterfall([
28+
//
29+
// 1. Read files in examples dir
30+
//
31+
async.apply(fs.readdir, examplesDir),
32+
//
33+
// 2. If node_modules exists, continue. Otherwise
34+
// exec `npm` to install them
35+
//
36+
function checkNodeModules(files, next) {
37+
if (files.indexOf('node_modules') !== -1) {
38+
return next();
39+
}
40+
41+
var child = spawn('npm', ['install'], {
42+
cwd: examplesDir
43+
});
44+
45+
child.on('exit', function (code) {
46+
return code
47+
? next(new Error('npm install exited with non-zero exit code'))
48+
: next();
49+
});
50+
},
51+
//
52+
// 3. Read files in examples dir again to ensure the install
53+
// worked as expected.
54+
//
55+
async.apply(fs.readdir, examplesDir),
56+
], this.callback);
57+
},
58+
"examples/node_modules should exist": function (err, files) {
59+
assert.notEqual(files.indexOf('node_modules'), -1);
60+
}
61+
}
62+
}
63+
};
64+
65+
//
66+
// ### function shouldRequire (file)
67+
// #### @file {string} File to attempt to require
68+
//
69+
// Returns a test which attempts to require `file`.
70+
//
71+
exports.shouldRequire = function (file) {
72+
return {
73+
"should have no errors": function () {
74+
try { assert.isObject(require(file)) }
75+
catch (ex) { assert.isNull(ex) }
76+
}
77+
};
78+
};
79+
80+
//
81+
// ### function shouldHaveNoErrors ()
82+
//
83+
// Returns a vows context that attempts to require
84+
// every relevant example file in `examples`.
85+
//
86+
exports.shouldHaveNoErrors = function () {
87+
var context = {};
88+
89+
['balancer', 'http', 'middleware', 'websocket'].forEach(function (dir) {
90+
var name = 'examples/' + dir,
91+
files = fs.readdirSync(path.join(rootDir, 'examples', dir));
92+
93+
files.forEach(function (file) {
94+
context[name + '/' + file] = exports.shouldRequire(path.join(
95+
examplesDir, dir, file
96+
));
97+
});
98+
});
99+
100+
return context;
101+
};

Diff for: test/macros/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
*
77
*/
88

9-
exports.http = require('./http');
10-
exports.ws = require('./ws');
9+
exports.examples = require('./examples');
10+
exports.http = require('./http');
11+
exports.ws = require('./ws');

0 commit comments

Comments
 (0)