Skip to content

Commit

Permalink
[refactor tests] Finished refactoring tests to support ws*-to-ws* t…
Browse files Browse the repository at this point in the history
…ests based on CLI arguments
  • Loading branch information
indexzero committed Jul 22, 2012
1 parent 828dbeb commit 7e854d7
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"node-http-proxy": "./bin/node-http-proxy"
},
"scripts": {
"test": "npm run-script test-http && npm run-script test-https && npm run-script test-core",
"test": "npm run-script test-http && npm run-script test-https",
"test-http": "vows --spec && vows --spec --target=https",
"test-https": "vows --spec --proxy=https && vows --spec --proxy=https --target=https",
"test-core": "test/core/run"
Expand Down
26 changes: 22 additions & 4 deletions test/helpers/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
*/

var assert = require('assert'),
https = require('https'),
async = require('async'),
io = require('socket.io'),
ws = require('ws'),
helpers = require('./index'),
protocols = helpers.protocols,
http = require('./http');

//
Expand Down Expand Up @@ -62,7 +65,9 @@ exports.createServer = function (options, callback) {
// will expect `options.input` and then send `options.output`.
//
exports.createSocketIoServer = function (options, callback) {
var server = io.listen(options.port, callback);
var server = protocols.target === 'https'
? io.listen(options.port, helpers.https, callback)
: io.listen(options.port, callback);

server.sockets.on('connection', function (socket) {
socket.on('incoming', function (data) {
Expand All @@ -83,9 +88,22 @@ exports.createSocketIoServer = function (options, callback) {
// will expect `options.input` and then send `options.output`.
//
exports.createWsServer = function (options, callback) {
var server = new ws.Server({ port: options.port }, callback);

server.on('connection', function (socket) {
var server,
wss;

if (protocols.target === 'https') {
server = https.createServer(helpers.https, function (req, res) {
req.writeHead(200);
req.end();
}).listen(options.port, callback);

wss = new ws.Server({ server: server });
}
else {
wss = new ws.Server({ port: options.port }, callback);
}

wss.on('connection', function (socket) {
socket.on('message', function (data) {
assert.equal(data, options.input);
socket.send(options.output);
Expand Down
21 changes: 13 additions & 8 deletions test/macros/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ exports.assertProxied = function (options) {
var ports = options.ports || helpers.nextPortPair,
input = options.input || 'hello world to ' + ports.target,
output = options.output || 'hello world from ' + ports.target,
protocol = options.protocol || 'http';
protocol = helpers.protocols.proxy;

if (options.raw && !options.protocol) {
protocol = 'ws';
}
if (options.raw) {
protocol = helpers.protocols.proxy === 'https'
? 'wss'
: 'ws';
}

return {
topic: function () {
Expand All @@ -89,6 +91,7 @@ exports.assertProxied = function (options) {
port: ports.proxy,
proxy: {
target: {
https: helpers.protocols.target === 'https',
host: '127.0.0.1',
port: ports.target
}
Expand Down Expand Up @@ -131,13 +134,15 @@ exports.assertProxiedToRoutes = function (options, nested) {
// Parse locations from routes for making assertion requests.
//
var locations = helpers.http.parseRoutes(options),
protocol = options.protocol || 'http',
protocol = helpers.protocols.proxy,
port = helpers.nextPort,
context,
proxy;

if (options.raw && !options.protocol) {
protocol = 'ws';

if (options.raw) {
protocol = helpers.protocols.proxy === 'https'
? 'wss'
: 'ws';
}

if (options.filename) {
Expand Down
2 changes: 1 addition & 1 deletion test/ws/routing-table-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var vows = require('vows'),
macros = require('../macros'),
helpers = require('../helpers/index');

vows.describe('node-http-proxy/ws').addBatch({
vows.describe(helpers.describe('routing-proxy', 'ws')).addBatch({
"With a valid target server": {
"and no latency": {
"using ws": macros.ws.assertProxied(),
Expand Down
2 changes: 1 addition & 1 deletion test/ws/socket.io-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var vows = require('vows'),
macros = require('../macros'),
helpers = require('../helpers/index');

vows.describe('node-http-proxy/ws/socket.io').addBatch({
vows.describe(helpers.describe('socket.io', 'ws')).addBatch({
"With a valid target server": {
"and no latency": macros.ws.assertProxied(),
// "and latency": macros.ws.assertProxied({
Expand Down
2 changes: 1 addition & 1 deletion test/ws/ws-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var vows = require('vows'),
macros = require('../macros'),
helpers = require('../helpers/index');

vows.describe('node-http-proxy/ws/WebSocket').addBatch({
vows.describe(helpers.describe('websocket', 'ws')).addBatch({
"With a valid target server": {
"and no latency": macros.ws.assertProxied({
raw: true
Expand Down

0 comments on commit 7e854d7

Please sign in to comment.