From 7e854d778b89201f7cb933e8bbda66316b98b0b4 Mon Sep 17 00:00:00 2001 From: indexzero Date: Sun, 22 Jul 2012 01:38:23 -0400 Subject: [PATCH] [refactor tests] Finished refactoring tests to support `ws*-to-ws*` tests based on CLI arguments --- package.json | 2 +- test/helpers/ws.js | 26 ++++++++++++++++++++++---- test/macros/ws.js | 21 +++++++++++++-------- test/ws/routing-table-test.js | 2 +- test/ws/socket.io-test.js | 2 +- test/ws/ws-test.js | 2 +- 6 files changed, 39 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 6a5d4da..7cabd20 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/helpers/ws.js b/test/helpers/ws.js index d31dd8c..cb6f7ba 100644 --- a/test/helpers/ws.js +++ b/test/helpers/ws.js @@ -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'); // @@ -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) { @@ -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); diff --git a/test/macros/ws.js b/test/macros/ws.js index b520ec2..eee785f 100644 --- a/test/macros/ws.js +++ b/test/macros/ws.js @@ -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 () { @@ -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 } @@ -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) { diff --git a/test/ws/routing-table-test.js b/test/ws/routing-table-test.js index 1d5dbb0..e04d647 100644 --- a/test/ws/routing-table-test.js +++ b/test/ws/routing-table-test.js @@ -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(), diff --git a/test/ws/socket.io-test.js b/test/ws/socket.io-test.js index 7d80d91..d833109 100644 --- a/test/ws/socket.io-test.js +++ b/test/ws/socket.io-test.js @@ -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({ diff --git a/test/ws/ws-test.js b/test/ws/ws-test.js index 9427e50..f354915 100644 --- a/test/ws/ws-test.js +++ b/test/ws/ws-test.js @@ -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