[refactor tests] Finished refactoring tests to support ws*-to-ws* tests based on CLI arguments

This commit is contained in:
indexzero 2012-07-22 01:38:23 -04:00
parent 828dbebcaa
commit 7e854d778b
6 changed files with 39 additions and 16 deletions

View File

@ -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"

View File

@ -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);

View File

@ -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) {

View File

@ -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(),

View File

@ -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({

View File

@ -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