Skip to content

Commit e2dc7f9

Browse files
committed
[refactor test] Finish removing old test code.
1 parent 4ae7a5b commit e2dc7f9

File tree

8 files changed

+294
-133
lines changed

8 files changed

+294
-133
lines changed

test/helpers/http.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,29 @@ exports.createProxyServer = function (options, callback) {
103103
});
104104
};
105105

106+
//
107+
// ### function assignPortsToRoutes (routes)
108+
// #### @routes {Object} Routing table to assign ports to
109+
//
110+
// Assigns dynamic ports to the `routes` for runtime testing.
111+
//
106112
exports.assignPortsToRoutes = function (routes) {
107113
Object.keys(routes).forEach(function (source) {
108114
routes[source] = routes[source].replace('{PORT}', helpers.nextPort);
109115
});
110116

111117
return routes;
112-
}
118+
};
113119

120+
//
121+
// ### function parseRoutes (options)
122+
// #### @options {Object} Options to use when parsing routes
123+
// #### @protocol {string} Protocol to use in the routes
124+
// #### @routes {Object} Routes to parse.
125+
//
126+
// Returns an Array of fully-parsed URLs for the source and
127+
// target of `options.routes`.
128+
//
114129
exports.parseRoutes = function (options) {
115130
var protocol = options.protocol || 'http',
116131
routes = options.routes;

test/helpers/ws.js

+47-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
var assert = require('assert'),
1010
async = require('async'),
1111
io = require('socket.io'),
12+
ws = require('ws'),
1213
http = require('./http');
1314

1415
//
1516
// ### function createServerPair (options, callback)
1617
// #### @options {Object} Options to create target and proxy server.
18+
// #### @target {Object} Options for the target server.
19+
// #### @proxy {Object} Options for the proxy server.
1720
// #### @callback {function} Continuation to respond to when complete.
1821
//
1922
// Creates http target and proxy servers
@@ -24,7 +27,7 @@ exports.createServerPair = function (options, callback) {
2427
// 1. Create the target server
2528
//
2629
function createTarget(next) {
27-
exports.createServer(options.target, next);
30+
exports.createServer(options.target, next);
2831
},
2932
//
3033
// 2. Create the proxy server
@@ -35,7 +38,30 @@ exports.createServerPair = function (options, callback) {
3538
], callback);
3639
};
3740

41+
//
42+
// ### function createServer (options, callback)
43+
// #### @options {Object} Options for creating the socket.io or ws server.
44+
// #### @raw {boolean} Enables ws.Websocket server.
45+
//
46+
// Creates a socket.io or ws server using the specified `options`.
47+
//
3848
exports.createServer = function (options, callback) {
49+
return options.raw
50+
? exports.createWsServer(options, callback)
51+
: exports.createSocketIoServer(options, callback);
52+
};
53+
54+
//
55+
// ### function createSocketIoServer (options, callback)
56+
// #### @options {Object} Options for creating the socket.io server
57+
// #### @port {number} Port to listen on
58+
// #### @input {string} Input to expect from the only socket
59+
// #### @output {string} Output to send the only socket
60+
//
61+
// Creates a socket.io server on the specified `options.port` which
62+
// will expect `options.input` and then send `options.output`.
63+
//
64+
exports.createSocketIoServer = function (options, callback) {
3965
var server = io.listen(options.port, callback);
4066

4167
server.sockets.on('connection', function (socket) {
@@ -46,3 +72,23 @@ exports.createServer = function (options, callback) {
4672
});
4773
};
4874

75+
//
76+
// ### function createWsServer (options, callback)
77+
// #### @options {Object} Options for creating the ws.Server instance
78+
// #### @port {number} Port to listen on
79+
// #### @input {string} Input to expect from the only socket
80+
// #### @output {string} Output to send the only socket
81+
//
82+
// Creates a ws.Server instance on the specified `options.port` which
83+
// will expect `options.input` and then send `options.output`.
84+
//
85+
exports.createWsServer = function (options, callback) {
86+
var server = new ws.Server({ port: options.port }, callback);
87+
88+
server.on('connection', function (socket) {
89+
socket.on('message', function (data) {
90+
assert.equal(data, options.input);
91+
socket.send(options.output);
92+
});
93+
});
94+
};

test/http/routing-table-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ vows.describe('node-http-proxy/http/routing-table').addBatch({
7272
)
7373
], function () {
7474
request({
75-
uri: 'http://localhost:' + that.port,
75+
uri: 'http://127.0.0.1:' + that.port,
7676
headers: {
7777
host: 'dynamic.com'
7878
}

test/macros/http.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ exports.assertProxied = function (options) {
5959
output = options.output || 'hello world from ' + ports.target,
6060
req = options.request || {};
6161

62-
req.uri = req.uri || 'http://localhost:' + ports.proxy;
62+
req.uri = req.uri || 'http://127.0.0.1:' + ports.proxy;
6363

6464
return {
6565
topic: function () {
@@ -79,7 +79,7 @@ exports.assertProxied = function (options) {
7979
proxy: {
8080
forward: options.forward,
8181
target: {
82-
host: 'localhost',
82+
host: '127.0.0.1',
8383
port: ports.target
8484
}
8585
}
@@ -110,7 +110,7 @@ exports.assertInvalidProxy = function (options) {
110110
var ports = options.ports || helpers.nextPortPair,
111111
req = options.request || {};
112112

113-
req.uri = req.uri || 'http://localhost:' + ports.proxy;
113+
req.uri = req.uri || 'http://127.0.0.1:' + ports.proxy;
114114

115115
return {
116116
topic: function () {
@@ -123,7 +123,7 @@ exports.assertInvalidProxy = function (options) {
123123
port: ports.proxy,
124124
proxy: {
125125
target: {
126-
host: 'localhost',
126+
host: '127.0.0.1',
127127
port: ports.target
128128
}
129129
}
@@ -158,13 +158,13 @@ exports.assertForwardProxied = function (options) {
158158
"and a valid forward target": exports.assertProxied({
159159
forward: {
160160
port: forwardPort,
161-
host: 'localhost'
161+
host: '127.0.0.1'
162162
}
163163
}),
164164
"and an invalid forward target": exports.assertProxied({
165165
forward: {
166166
port: 9898,
167-
host: 'localhost'
167+
host: '127.0.0.1'
168168
}
169169
})
170170
};
@@ -271,7 +271,7 @@ exports.assertProxiedToRoutes = function (options, nested) {
271271
"a request to unknown.com": exports.assertRequest({
272272
assert: { statusCode: 404 },
273273
request: {
274-
uri: 'http://localhost:' + port,
274+
uri: 'http://127.0.0.1:' + port,
275275
headers: {
276276
host: 'unknown.com'
277277
}
@@ -285,7 +285,7 @@ exports.assertProxiedToRoutes = function (options, nested) {
285285
locations.forEach(function (location) {
286286
context[location.source.href] = exports.assertRequest({
287287
request: {
288-
uri: 'http://localhost:' + port + location.source.path,
288+
uri: 'http://127.0.0.1:' + port + location.source.path,
289289
headers: {
290290
host: location.source.hostname
291291
}

0 commit comments

Comments
 (0)