Skip to content

Commit

Permalink
[refactor test] Finish removing old test code.
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Jul 22, 2012
1 parent 4ae7a5b commit e2dc7f9
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 133 deletions.
17 changes: 16 additions & 1 deletion test/helpers/http.js
Expand Up @@ -103,14 +103,29 @@ exports.createProxyServer = function (options, callback) {
});
};

//
// ### function assignPortsToRoutes (routes)
// #### @routes {Object} Routing table to assign ports to
//
// Assigns dynamic ports to the `routes` for runtime testing.
//
exports.assignPortsToRoutes = function (routes) {
Object.keys(routes).forEach(function (source) {
routes[source] = routes[source].replace('{PORT}', helpers.nextPort);
});

return routes;
}
};

//
// ### function parseRoutes (options)
// #### @options {Object} Options to use when parsing routes
// #### @protocol {string} Protocol to use in the routes
// #### @routes {Object} Routes to parse.
//
// Returns an Array of fully-parsed URLs for the source and
// target of `options.routes`.
//
exports.parseRoutes = function (options) {
var protocol = options.protocol || 'http',
routes = options.routes;
Expand Down
48 changes: 47 additions & 1 deletion test/helpers/ws.js
Expand Up @@ -9,11 +9,14 @@
var assert = require('assert'),
async = require('async'),
io = require('socket.io'),
ws = require('ws'),
http = require('./http');

//
// ### function createServerPair (options, callback)
// #### @options {Object} Options to create target and proxy server.
// #### @target {Object} Options for the target server.
// #### @proxy {Object} Options for the proxy server.
// #### @callback {function} Continuation to respond to when complete.
//
// Creates http target and proxy servers
Expand All @@ -24,7 +27,7 @@ exports.createServerPair = function (options, callback) {
// 1. Create the target server
//
function createTarget(next) {
exports.createServer(options.target, next);
exports.createServer(options.target, next);
},
//
// 2. Create the proxy server
Expand All @@ -35,7 +38,30 @@ exports.createServerPair = function (options, callback) {
], callback);
};

//
// ### function createServer (options, callback)
// #### @options {Object} Options for creating the socket.io or ws server.
// #### @raw {boolean} Enables ws.Websocket server.
//
// Creates a socket.io or ws server using the specified `options`.
//
exports.createServer = function (options, callback) {
return options.raw
? exports.createWsServer(options, callback)
: exports.createSocketIoServer(options, callback);
};

//
// ### function createSocketIoServer (options, callback)
// #### @options {Object} Options for creating the socket.io server
// #### @port {number} Port to listen on
// #### @input {string} Input to expect from the only socket
// #### @output {string} Output to send the only socket
//
// Creates a socket.io server on the specified `options.port` which
// will expect `options.input` and then send `options.output`.
//
exports.createSocketIoServer = function (options, callback) {
var server = io.listen(options.port, callback);

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

//
// ### function createWsServer (options, callback)
// #### @options {Object} Options for creating the ws.Server instance
// #### @port {number} Port to listen on
// #### @input {string} Input to expect from the only socket
// #### @output {string} Output to send the only socket
//
// Creates a ws.Server instance on the specified `options.port` which
// 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) {
socket.on('message', function (data) {
assert.equal(data, options.input);
socket.send(options.output);
});
});
};
2 changes: 1 addition & 1 deletion test/http/routing-table-test.js
Expand Up @@ -72,7 +72,7 @@ vows.describe('node-http-proxy/http/routing-table').addBatch({
)
], function () {
request({
uri: 'http://localhost:' + that.port,
uri: 'http://127.0.0.1:' + that.port,
headers: {
host: 'dynamic.com'
}
Expand Down
16 changes: 8 additions & 8 deletions test/macros/http.js
Expand Up @@ -59,7 +59,7 @@ exports.assertProxied = function (options) {
output = options.output || 'hello world from ' + ports.target,
req = options.request || {};

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

return {
topic: function () {
Expand All @@ -79,7 +79,7 @@ exports.assertProxied = function (options) {
proxy: {
forward: options.forward,
target: {
host: 'localhost',
host: '127.0.0.1',
port: ports.target
}
}
Expand Down Expand Up @@ -110,7 +110,7 @@ exports.assertInvalidProxy = function (options) {
var ports = options.ports || helpers.nextPortPair,
req = options.request || {};

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

return {
topic: function () {
Expand All @@ -123,7 +123,7 @@ exports.assertInvalidProxy = function (options) {
port: ports.proxy,
proxy: {
target: {
host: 'localhost',
host: '127.0.0.1',
port: ports.target
}
}
Expand Down Expand Up @@ -158,13 +158,13 @@ exports.assertForwardProxied = function (options) {
"and a valid forward target": exports.assertProxied({
forward: {
port: forwardPort,
host: 'localhost'
host: '127.0.0.1'
}
}),
"and an invalid forward target": exports.assertProxied({
forward: {
port: 9898,
host: 'localhost'
host: '127.0.0.1'
}
})
};
Expand Down Expand Up @@ -271,7 +271,7 @@ exports.assertProxiedToRoutes = function (options, nested) {
"a request to unknown.com": exports.assertRequest({
assert: { statusCode: 404 },
request: {
uri: 'http://localhost:' + port,
uri: 'http://127.0.0.1:' + port,
headers: {
host: 'unknown.com'
}
Expand All @@ -285,7 +285,7 @@ exports.assertProxiedToRoutes = function (options, nested) {
locations.forEach(function (location) {
context[location.source.href] = exports.assertRequest({
request: {
uri: 'http://localhost:' + port + location.source.path,
uri: 'http://127.0.0.1:' + port + location.source.path,
headers: {
host: location.source.hostname
}
Expand Down

0 comments on commit e2dc7f9

Please sign in to comment.