Skip to content

Commit 734769f

Browse files
committed
[test] Updated tests to reflect finalized API of the RoutingProxy
1 parent f765f90 commit 734769f

File tree

5 files changed

+72
-55
lines changed

5 files changed

+72
-55
lines changed

test/helpers.js

+36-17
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
*
66
*/
77

8-
var fs = require('fs'),
8+
var assert = require('assert'),
9+
fs = require('fs'),
910
http = require('http'),
1011
https = require('https'),
1112
path = require('path'),
12-
vows = require('vows'),
13-
assert = require('assert'),
13+
argv = require('optimist').argv,
1414
request = require('request'),
15-
websocket = require('./../vendor/websocket'),
16-
httpProxy = require('./../lib/node-http-proxy');
15+
vows = require('vows'),
16+
websocket = require('../vendor/websocket'),
17+
httpProxy = require('../lib/node-http-proxy');
1718

1819
var loadHttps = exports.loadHttps = function () {
1920
return {
@@ -22,16 +23,34 @@ var loadHttps = exports.loadHttps = function () {
2223
};
2324
};
2425

25-
var TestRunner = exports.TestRunner = function (source, target) {
26-
this.source = { protocol: source },
27-
this.target = { protocol: target };
26+
var parseProtocol = exports.parseProtocol = function () {
27+
function setupProtocol (secure) {
28+
return {
29+
secure: secure,
30+
protocols: {
31+
http: secure ? 'https' : 'http',
32+
ws: secure ? 'wss' : 'ws'
33+
}
34+
}
35+
}
36+
37+
return {
38+
source: setupProtocol(argv.source === 'secure'),
39+
target: setupProtocol(argv.target === 'secure')
40+
};
41+
}
42+
43+
var TestRunner = exports.TestRunner = function (options) {
44+
options = options || {};
45+
this.source = options.source || {};
46+
this.target = options.target || {};
2847
this.testServers = [];
2948

30-
if (source === 'https') {
49+
if (this.source.secure) {
3150
this.source.https = loadHttps();
3251
}
3352

34-
if (target === 'https') {
53+
if (this.target.secure) {
3554
this.target.https = loadHttps();
3655
}
3756
};
@@ -48,7 +67,7 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
4867

4968
options = {
5069
method: 'GET',
51-
uri: self.source.protocol + '://localhost:' + proxyPort,
70+
uri: self.source.protocols.http + '://localhost:' + proxyPort,
5271
headers: {
5372
host: host
5473
}
@@ -79,7 +98,7 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
7998

8099
TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, createProxy) {
81100
var assertion = "should receive " + statusCode + " responseCode",
82-
protocol = this.source.protocol;
101+
protocol = this.source.protocols.http;
83102

84103
var test = {
85104
topic: function () {
@@ -240,21 +259,21 @@ TestRunner.prototype.startProxyServerWithTableAndLatency = function (port, laten
240259
// Initialize the nodeProxy and start proxying the request
241260
//
242261
var that = this,
243-
proxy = new httpProxy.HttpProxy(merge({}, options, this.getOptions())),
262+
proxy = new httpProxy.RoutingProxy(merge({}, options, this.getOptions())),
244263
proxyServer;
245264

246265
var handler = function (req, res) {
247-
var buffer = proxy.buffer(req);
266+
var buffer = httpProxy.buffer(req);
248267
setTimeout(function () {
249268
proxy.proxyRequest(req, res, {
250269
buffer: buffer
251270
});
252271
}, latency);
253272
};
254273

255-
proxyServer = that.options.https
256-
? https.createServer(that.options.https, handler, that.options)
257-
: http.createServer(handler, that.options);
274+
proxyServer = this.source.https
275+
? https.createServer(this.source.https, handler)
276+
: http.createServer(handler);
258277

259278
proxyServer.listen(port, function () {
260279
that.testServers.push(proxyServer);

test/http/http-proxy-test.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
var assert = require('assert'),
2828
util = require('util'),
29-
argv = require('optimist').argv,
3029
request = require('request'),
3130
vows = require('vows'),
3231
helpers = require('../helpers');
@@ -45,11 +44,11 @@ var badForwardOptions = {
4544
}
4645
};
4746

48-
var protocol = argv.https ? 'https' : 'http',
49-
target = argv.target ? argv.target : 'http',
50-
runner = new helpers.TestRunner(protocol, target);
47+
var options = helpers.parseProtocol(),
48+
testName = [options.source.protocols.http, options.target.protocols.http].join('-to-'),
49+
runner = new helpers.TestRunner(options);
5150

52-
vows.describe('node-http-proxy/' + protocol).addBatch({
51+
vows.describe('node-http-proxy/http-proxy/' + testName).addBatch({
5352
"When using server created by httpProxy.createServer()": {
5453
"with no latency" : {
5554
"and a valid target server": runner.assertProxied('localhost', 8080, 8081, function (callback) {

test/http/routing-proxy-test.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ var assert = require('assert'),
1414
vows = require('vows'),
1515
helpers = require('../helpers');
1616

17-
var protocol = argv.https ? 'https' : 'http',
18-
runner = new helpers.TestRunner(protocol),
17+
var options = helpers.parseProtocol(),
18+
testName = [options.source.protocols.http, options.target.protocols.http].join('-to-'),
19+
runner = new helpers.TestRunner(options),
1920
routeFile = path.join(__dirname, 'config.json');
2021

2122
var fileOptions = {
@@ -40,7 +41,7 @@ var hostnameOptions = {
4041
}
4142
};
4243

43-
vows.describe('node-http-proxy/proxy-table/' + protocol).addBatch({
44+
vows.describe('node-http-proxy/routing-proxy/' + testName).addBatch({
4445
"When using server created by httpProxy.createServer()": {
4546
"when passed a routing table": {
4647
"and routing by RegExp": {
@@ -81,16 +82,14 @@ vows.describe('node-http-proxy/proxy-table/' + protocol).addBatch({
8182
fs.writeFileSync(routeFile, JSON.stringify(config));
8283

8384
this.server.on('routes', function () {
84-
var options = {
85-
method: 'GET',
86-
uri: protocol + '://localhost:8100',
87-
headers: {
88-
host: 'dynamic.com'
89-
}
90-
};
91-
9285
runner.startTargetServer(8103, 'hello dynamic.com', function () {
93-
request(options, that.callback);
86+
request({
87+
method: 'GET',
88+
uri: options.source.protocols.http + '://localhost:8100',
89+
headers: {
90+
host: 'dynamic.com'
91+
}
92+
}, that.callback);
9493
});
9594
});
9695
},

test/websocket/websocket-proxy-test.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ catch (ex) {
4343
process.exit(1);
4444
}
4545

46-
var protocol = argv.https ? 'https' : 'http',
47-
wsprotocol = argv.https ? 'wss' : 'ws',
48-
runner = new helpers.TestRunner(protocol);
46+
var options = helpers.parseProtocol(),
47+
testName = [options.source.protocols.ws, options.target.protocols.ws].join('-to-'),
48+
runner = new helpers.TestRunner(options);
4949

50-
vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
50+
vows.describe('node-http-proxy/http-proxy/' + testName).addBatch({
5151
"When using server created by httpProxy.createServer()": {
5252
"with no latency" : {
5353
"when an inbound message is sent from a WebSocket client": {
@@ -58,8 +58,8 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
5858
runner.webSocketTest({
5959
io: io,
6060
host: 'localhost',
61-
wsprotocol: wsprotocol,
62-
protocol: protocol,
61+
wsprotocol: options.source.protocols.ws,
62+
protocol: options.source.protocols.http,
6363
ports: {
6464
target: 8130,
6565
proxy: 8131
@@ -85,7 +85,7 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
8585
},
8686
"the origin and sec-websocket-origin headers should match": function (err, msg, headers) {
8787
assert.isString(headers.response['sec-websocket-location']);
88-
assert.isTrue(headers.response['sec-websocket-location'].indexOf(wsprotocol) !== -1);
88+
assert.isTrue(headers.response['sec-websocket-location'].indexOf(options.source.protocols.ws) !== -1);
8989
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
9090
}
9191
},
@@ -97,8 +97,8 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
9797
runner.webSocketTest({
9898
io: io,
9999
host: 'localhost',
100-
wsprotocol: wsprotocol,
101-
protocol: protocol,
100+
wsprotocol: options.source.protocols.ws,
101+
protocol: options.source.protocols.http,
102102
ports: {
103103
target: 8132,
104104
proxy: 8133
@@ -126,8 +126,8 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
126126
runner.webSocketTest({
127127
io: io,
128128
host: 'localhost',
129-
wsprotocol: wsprotocol,
130-
protocol: protocol,
129+
wsprotocol: options.source.protocols.ws,
130+
protocol: options.source.protocols.http,
131131
ports: {
132132
target: 8134,
133133
proxy: 8135
@@ -154,7 +154,7 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
154154
},
155155
"the origin and sec-websocket-origin headers should match": function (err, msg, headers) {
156156
assert.isString(headers.response['sec-websocket-location']);
157-
assert.isTrue(headers.response['sec-websocket-location'].indexOf(wsprotocol) !== -1);
157+
assert.isTrue(headers.response['sec-websocket-location'].indexOf(options.source.protocols.ws) !== -1);
158158
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
159159
}
160160
}

test/websocket/websocket-routing-proxy-test.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var util = require('util'),
3030
colors = require('colors'),
3131
request = require('request'),
3232
vows = require('vows'),
33-
websocket = require('../vendor/websocket'),
33+
websocket = require('../../vendor/websocket'),
3434
helpers = require('../helpers');
3535

3636
try {
@@ -43,11 +43,11 @@ catch (ex) {
4343
process.exit(1);
4444
}
4545

46-
var protocol = argv.https ? 'https' : 'http',
47-
wsprotocol = argv.https ? 'wss' : 'ws',
48-
runner = new helpers.TestRunner(protocol);
46+
var options = helpers.parseProtocol(),
47+
testName = [options.source.protocols.ws, options.target.protocols.ws].join('-to-'),
48+
runner = new helpers.TestRunner(options);
4949

50-
vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
50+
vows.describe('node-http-proxy/routing-proxy/' + testName).addBatch({
5151
"When using server created by httpProxy.createServer()": {
5252
"using proxy table with no latency": {
5353
"when an inbound message is sent from a WebSocket client": {
@@ -58,9 +58,9 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
5858
runner.webSocketTestWithTable({
5959
io: io,
6060
host: 'localhost',
61-
wsprotocol: wsprotocol,
62-
protocol: protocol,
63-
router: {'localhost':'localhost:8230'},
61+
wsprotocol: options.source.protocols.ws,
62+
protocol: options.source.protocols.http,
63+
router: { 'localhost' : 'localhost:8230' },
6464
ports: {
6565
target: 8230,
6666
proxy: 8231
@@ -86,7 +86,7 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
8686
},
8787
"the origin and sec-websocket-origin headers should match": function (err, msg, headers) {
8888
assert.isString(headers.response['sec-websocket-location']);
89-
assert.isTrue(headers.response['sec-websocket-location'].indexOf(wsprotocol) !== -1);
89+
assert.isTrue(headers.response['sec-websocket-location'].indexOf(options.source.protocols.ws) !== -1);
9090
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
9191
}
9292
}

0 commit comments

Comments
 (0)