Skip to content

Commit 1ee6bef

Browse files
committed
[test] Fix tests in https mode
Tests can be run in https mode by adding the --https flag vows test/*-test.js --spec --https
1 parent ff82946 commit 1ee6bef

File tree

4 files changed

+67
-57
lines changed

4 files changed

+67
-57
lines changed

test/helpers.js

+47-37
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var TestRunner = exports.TestRunner = function (protocol) {
3737
this.options = {};
3838
this.protocol = protocol;
3939
this.testServers = [];
40-
40+
4141
if (protocol === 'https') {
4242
this.options.https = loadHttps();
4343
}
@@ -47,17 +47,17 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
4747
var self = this,
4848
assertion = "should receive 'hello " + host + "'",
4949
output = 'hello ' + host;
50-
50+
5151
var test = {
5252
topic: function () {
5353
var that = this, options = {
54-
method: 'GET',
54+
method: 'GET',
5555
uri: self.protocol + '://localhost:' + proxyPort,
5656
headers: {
5757
host: host
5858
}
5959
};
60-
60+
6161
function startTest () {
6262
if (port) {
6363
return self.startTargetServer(port, output, function () {
@@ -67,74 +67,77 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
6767

6868
request(options, this.callback);
6969
}
70-
70+
7171
return createProxy ? createProxy(startTest) : startTest();
7272
}
7373
};
74-
75-
test[assertion] = function (err, res, body) {;
74+
75+
test[assertion] = function (err, res, body) {
7676
assert.isNull(err);
7777
assert.equal(body, output);
7878
};
79-
79+
8080
return test;
8181
};
8282

8383
TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, createProxy) {
84-
var assertion = "should receive " + statusCode + " responseCode";
85-
84+
var assertion = "should receive " + statusCode + " responseCode",
85+
protocol = this.protocol;
86+
8687
var test = {
8788
topic: function () {
8889
var that = this, options = {
89-
method: 'GET',
90-
uri: 'http://localhost:' + proxyPort,
90+
method: 'GET',
91+
uri: protocol + '://localhost:' + proxyPort,
9192
headers: {
9293
host: 'unknown.com'
9394
}
9495
};
95-
96+
9697
if (createProxy) {
9798
return createProxy(function () {
98-
request(options, that.callback);
99+
request(options, that.callback);
99100
});
100101
}
101-
102+
102103
request(options, this.callback);
103104
}
104105
};
105-
106+
106107
test[assertion] = function (err, res, body) {
107108
assert.isNull(err);
108109
assert.equal(res.statusCode, statusCode);
109110
};
110-
111+
111112
return test;
112113
};
113114

114115
//
115116
// Creates the reverse proxy server
116117
//
117118
TestRunner.prototype.startProxyServer = function (port, targetPort, host, callback) {
118-
var that = this, proxyServer = httpProxy.createServer(targetPort, host);
119-
119+
var that = this,
120+
options = that.options,
121+
proxyServer = httpProxy.createServer(targetPort, host, options);
122+
120123
proxyServer.listen(port, function () {
121124
that.testServers.push(proxyServer);
122125
callback(null, proxyServer);
123-
});
126+
});
124127
};
125128

126-
//
129+
//
127130
// Creates the reverse proxy server with a specified latency
128131
//
129132
TestRunner.prototype.startLatentProxyServer = function (port, targetPort, host, latency, callback) {
130133
// Initialize the nodeProxy and start proxying the request
131134
var that = this, proxyServer = httpProxy.createServer(function (req, res, proxy) {
132135
var buffer = proxy.buffer(req);
133-
136+
134137
setTimeout(function () {
135138
proxy.proxyRequest(req, res, {
136-
port: targetPort,
137-
host: host,
139+
port: targetPort,
140+
host: host,
138141
buffer: buffer
139142
});
140143
}, latency);
@@ -150,12 +153,12 @@ TestRunner.prototype.startLatentProxyServer = function (port, targetPort, host,
150153
// Creates the reverse proxy server with a ProxyTable
151154
//
152155
TestRunner.prototype.startProxyServerWithTable = function (port, options, callback) {
153-
var that = this, proxyServer = httpProxy.createServer(merge({}, options, this.options));
156+
var that = this, proxyServer = httpProxy.createServer(merge({}, options, this.options));
154157
proxyServer.listen(port, function () {
155158
that.testServers.push(proxyServer);
156159
callback();
157160
});
158-
161+
159162
return proxyServer;
160163
};
161164

@@ -164,29 +167,36 @@ TestRunner.prototype.startProxyServerWithTable = function (port, options, callba
164167
//
165168
TestRunner.prototype.startProxyServerWithTableAndLatency = function (port, latency, options, callback) {
166169
// Initialize the nodeProxy and start proxying the request
167-
var proxyServer, that = this, proxy = new httpProxy.HttpProxy(merge({}, options, this.options));
168-
proxyServer = http.createServer(function (req, res) {
170+
var proxyServer,
171+
that = this,
172+
proxy = new httpProxy.HttpProxy(merge({}, options, that.options));
173+
174+
var handler = function (req, res) {
169175
var buffer = proxy.buffer(req);
170176
setTimeout(function () {
171177
proxy.proxyRequest(req, res, {
172178
buffer: buffer
173179
});
174180
}, latency);
175-
}, this.options);
176-
181+
};
182+
183+
proxyServer = that.options.https
184+
? https.createServer(that.options.https, handler, that.options)
185+
: http.createServer(handler, that.options);
186+
177187
proxyServer.listen(port, function () {
178188
that.testServers.push(proxyServer);
179189
callback();
180190
});
181-
191+
182192
return proxyServer;
183193
};
184194

185195
//
186196
// Creates proxy server forwarding to the specified options
187197
//
188198
TestRunner.prototype.startProxyServerWithForwarding = function (port, targetPort, host, options, callback) {
189-
var that = this, proxyServer = httpProxy.createServer(targetPort, host, merge({}, options, this.options));
199+
var that = this, proxyServer = httpProxy.createServer(targetPort, host, merge({}, options, this.options));
190200
proxyServer.listen(port, function () {
191201
that.testServers.push(proxyServer);
192202
callback(null, proxyServer);
@@ -200,13 +210,13 @@ TestRunner.prototype.startTargetServer = function (port, output, callback) {
200210
var that = this, targetServer, handler = function (req, res) {
201211
res.writeHead(200, { 'Content-Type': 'text/plain' });
202212
res.write(output);
203-
res.end();
213+
res.end();
204214
};
205-
206-
targetServer = this.options.https
215+
216+
targetServer = this.options.https
207217
? https.createServer(this.options.https, handler)
208218
: http.createServer(handler);
209-
219+
210220
targetServer.listen(port, function () {
211221
that.testServers.push(targetServer);
212222
callback(null, targetServer);
@@ -222,4 +232,4 @@ TestRunner.prototype.closeServers = function () {
222232
});
223233

224234
return this.testServers;
225-
};
235+
};

test/node-http-proxy-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,4 @@ vows.describe('node-http-proxy/' + protocol).addBatch({
8989
assert.isTrue(true);
9090
}
9191
}
92-
}).export(module);
92+
}).export(module);

test/proxy-table-test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var fs = require('fs'),
1414
helpers = require('./helpers'),
1515
argv = require('optimist').argv,
1616
TestRunner = helpers.TestRunner;
17-
17+
1818
var protocol = argv.https ? 'https' : 'http',
1919
runner = new TestRunner(protocol),
2020
routeFile = path.join(__dirname, 'config.json');
@@ -78,13 +78,13 @@ vows.describe('node-http-proxy/proxy-table/' + protocol).addBatch({
7878
data = fs.readFileSync(routeFile),
7979
config = JSON.parse(data);
8080

81-
config.router['dynamic.com'] = "127.0.0.1:8103"
81+
config.router['dynamic.com'] = "127.0.0.1:8103";
8282
fs.writeFileSync(routeFile, JSON.stringify(config));
83-
83+
8484
this.server.on('routes', function () {
8585
var options = {
86-
method: 'GET',
87-
uri: 'http://localhost:8100',
86+
method: 'GET',
87+
uri: protocol + '://localhost:8100',
8888
headers: {
8989
host: 'dynamic.com'
9090
}
@@ -126,4 +126,4 @@ vows.describe('node-http-proxy/proxy-table/' + protocol).addBatch({
126126
assert.isTrue(true);
127127
}
128128
}
129-
}).export(module);
129+
}).export(module);

test/web-socket-proxy-test.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2424
2525
*/
26-
26+
2727
var vows = require('vows'),
2828
util = require('util'),
2929
colors = require('colors'),
@@ -34,7 +34,7 @@ var vows = require('vows'),
3434

3535
try {
3636
var utils = require('socket.io/lib/socket.io/utils'),
37-
io = require('socket.io');
37+
io = require('socket.io');
3838
}
3939
catch (ex) {
4040
console.error('Socket.io is required for this test:');
@@ -50,16 +50,16 @@ vows.describe('node-http-proxy/websocket').addBatch({
5050
"when an inbound message is sent from a WebSocket client": {
5151
topic: function () {
5252
var that = this;
53-
53+
5454
runner.startTargetServer(8130, 'hello websocket', function (err, target) {
5555
var socket = io.listen(target);
56-
56+
5757
socket.on('connection', function (client) {
5858
client.on('message', function (msg) {
5959
that.callback(null, msg);
6060
});
6161
});
62-
62+
6363
runner.startProxyServer(8131, 8130, 'localhost', function (err, proxy) {
6464
//
6565
// Setup the web socket against our proxy
@@ -70,23 +70,23 @@ vows.describe('node-http-proxy/websocket').addBatch({
7070
ws.send(utils.encode('from client'));
7171
});
7272
});
73-
})
73+
});
7474
},
7575
"the target server should receive the message": function (err, msg) {
7676
assert.equal(msg, 'from client');
77-
}
77+
}
7878
},
7979
"when an outbound message is sent from the target server": {
8080
topic: function () {
8181
var that = this;
82-
82+
8383
runner.startTargetServer(8132, 'hello websocket', function (err, target) {
8484
var socket = io.listen(target);
85-
85+
8686
socket.on('connection', function (client) {
8787
socket.broadcast('from server');
8888
});
89-
89+
9090
runner.startProxyServer(8133, 8132, 'localhost', function (err, proxy) {
9191
//
9292
// Setup the web socket against our proxy
@@ -100,11 +100,11 @@ vows.describe('node-http-proxy/websocket').addBatch({
100100
}
101101
});
102102
});
103-
})
103+
});
104104
},
105105
"the client should receive the message": function (err, msg) {
106106
assert.equal(msg, 'from server');
107-
}
107+
}
108108
}
109109
}
110110
}
@@ -117,4 +117,4 @@ vows.describe('node-http-proxy/websocket').addBatch({
117117
assert.isTrue(true);
118118
}
119119
}
120-
}).export(module);
120+
}).export(module);

0 commit comments

Comments
 (0)