Skip to content

Commit 7e854d7

Browse files
committed
[refactor tests] Finished refactoring tests to support ws*-to-ws* tests based on CLI arguments
1 parent 828dbeb commit 7e854d7

File tree

6 files changed

+39
-16
lines changed

6 files changed

+39
-16
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"node-http-proxy": "./bin/node-http-proxy"
3535
},
3636
"scripts": {
37-
"test": "npm run-script test-http && npm run-script test-https && npm run-script test-core",
37+
"test": "npm run-script test-http && npm run-script test-https",
3838
"test-http": "vows --spec && vows --spec --target=https",
3939
"test-https": "vows --spec --proxy=https && vows --spec --proxy=https --target=https",
4040
"test-core": "test/core/run"

test/helpers/ws.js

+22-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
*/
88

99
var assert = require('assert'),
10+
https = require('https'),
1011
async = require('async'),
1112
io = require('socket.io'),
1213
ws = require('ws'),
14+
helpers = require('./index'),
15+
protocols = helpers.protocols,
1316
http = require('./http');
1417

1518
//
@@ -62,7 +65,9 @@ exports.createServer = function (options, callback) {
6265
// will expect `options.input` and then send `options.output`.
6366
//
6467
exports.createSocketIoServer = function (options, callback) {
65-
var server = io.listen(options.port, callback);
68+
var server = protocols.target === 'https'
69+
? io.listen(options.port, helpers.https, callback)
70+
: io.listen(options.port, callback);
6671

6772
server.sockets.on('connection', function (socket) {
6873
socket.on('incoming', function (data) {
@@ -83,9 +88,22 @@ exports.createSocketIoServer = function (options, callback) {
8388
// will expect `options.input` and then send `options.output`.
8489
//
8590
exports.createWsServer = function (options, callback) {
86-
var server = new ws.Server({ port: options.port }, callback);
87-
88-
server.on('connection', function (socket) {
91+
var server,
92+
wss;
93+
94+
if (protocols.target === 'https') {
95+
server = https.createServer(helpers.https, function (req, res) {
96+
req.writeHead(200);
97+
req.end();
98+
}).listen(options.port, callback);
99+
100+
wss = new ws.Server({ server: server });
101+
}
102+
else {
103+
wss = new ws.Server({ port: options.port }, callback);
104+
}
105+
106+
wss.on('connection', function (socket) {
89107
socket.on('message', function (data) {
90108
assert.equal(data, options.input);
91109
socket.send(options.output);

test/macros/ws.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ exports.assertProxied = function (options) {
6969
var ports = options.ports || helpers.nextPortPair,
7070
input = options.input || 'hello world to ' + ports.target,
7171
output = options.output || 'hello world from ' + ports.target,
72-
protocol = options.protocol || 'http';
72+
protocol = helpers.protocols.proxy;
7373

74-
if (options.raw && !options.protocol) {
75-
protocol = 'ws';
76-
}
74+
if (options.raw) {
75+
protocol = helpers.protocols.proxy === 'https'
76+
? 'wss'
77+
: 'ws';
78+
}
7779

7880
return {
7981
topic: function () {
@@ -89,6 +91,7 @@ exports.assertProxied = function (options) {
8991
port: ports.proxy,
9092
proxy: {
9193
target: {
94+
https: helpers.protocols.target === 'https',
9295
host: '127.0.0.1',
9396
port: ports.target
9497
}
@@ -131,13 +134,15 @@ exports.assertProxiedToRoutes = function (options, nested) {
131134
// Parse locations from routes for making assertion requests.
132135
//
133136
var locations = helpers.http.parseRoutes(options),
134-
protocol = options.protocol || 'http',
137+
protocol = helpers.protocols.proxy,
135138
port = helpers.nextPort,
136139
context,
137140
proxy;
138-
139-
if (options.raw && !options.protocol) {
140-
protocol = 'ws';
141+
142+
if (options.raw) {
143+
protocol = helpers.protocols.proxy === 'https'
144+
? 'wss'
145+
: 'ws';
141146
}
142147

143148
if (options.filename) {

test/ws/routing-table-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var vows = require('vows'),
1010
macros = require('../macros'),
1111
helpers = require('../helpers/index');
1212

13-
vows.describe('node-http-proxy/ws').addBatch({
13+
vows.describe(helpers.describe('routing-proxy', 'ws')).addBatch({
1414
"With a valid target server": {
1515
"and no latency": {
1616
"using ws": macros.ws.assertProxied(),

test/ws/socket.io-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var vows = require('vows'),
1010
macros = require('../macros'),
1111
helpers = require('../helpers/index');
1212

13-
vows.describe('node-http-proxy/ws/socket.io').addBatch({
13+
vows.describe(helpers.describe('socket.io', 'ws')).addBatch({
1414
"With a valid target server": {
1515
"and no latency": macros.ws.assertProxied(),
1616
// "and latency": macros.ws.assertProxied({

test/ws/ws-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var vows = require('vows'),
1010
macros = require('../macros'),
1111
helpers = require('../helpers/index');
1212

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

0 commit comments

Comments
 (0)