Skip to content

Commit 828dbeb

Browse files
committed
[refactor test] Add support for http*-to-http* testing from CLI arguments
1 parent 55286a7 commit 828dbeb

File tree

6 files changed

+131
-24
lines changed

6 files changed

+131
-24
lines changed

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
],
1919
"dependencies": {
2020
"colors": "0.x.x",
21-
"optimist": "0.2.x",
21+
"optimist": "0.3.x",
2222
"pkginfo": "0.2.x"
2323
},
2424
"devDependencies": {
2525
"request": "1.9.x",
26-
"vows": "0.5.x",
26+
"vows": "0.6.x",
2727
"async": "0.1.x",
2828
"socket.io": "0.9.6",
2929
"socket.io-client": "0.9.6",
@@ -35,8 +35,8 @@
3535
},
3636
"scripts": {
3737
"test": "npm run-script test-http && npm run-script test-https && npm run-script test-core",
38-
"test-http": "vows --spec && vows --spec --target=secure",
39-
"test-https": "vows --spec --source=secure && vows --spec --source=secure --target=secure",
38+
"test-http": "vows --spec && vows --spec --target=https",
39+
"test-https": "vows --spec --proxy=https && vows --spec --proxy=https --target=https",
4040
"test-core": "test/core/run"
4141
},
4242
"engines": {

test/helpers/http.js

+31-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88

99
var assert = require('assert'),
1010
http = require('http'),
11+
https = require('https'),
1112
url = require('url'),
1213
async = require('async'),
1314
helpers = require('./index'),
15+
protocols = helpers.protocols,
1416
httpProxy = require('../../lib/node-http-proxy');
1517

1618
//
@@ -48,7 +50,11 @@ exports.createServerPair = function (options, callback) {
4850
// Creates a target server that the tests will proxy to.
4951
//
5052
exports.createServer = function (options, callback) {
51-
http.createServer(function (req, res) {
53+
//
54+
// Request handler to use in either `http`
55+
// or `https` server.
56+
//
57+
function requestHandler(req, res) {
5258
if (options.headers) {
5359
Object.keys(options.headers).forEach(function (key) {
5460
assert.equal(req.headers[key], options.headers[key]);
@@ -58,7 +64,13 @@ exports.createServer = function (options, callback) {
5864
res.writeHead(200, { 'Content-Type': 'text/plain' });
5965
res.write(options.output || 'hello proxy');
6066
res.end();
61-
}).listen(options.port, function () {
67+
}
68+
69+
var server = protocols.target === 'https'
70+
? https.createServer(helpers.https, requestHandler)
71+
: http.createServer(requestHandler);
72+
73+
server.listen(options.port, function () {
6274
callback(null, this);
6375
});
6476
};
@@ -76,6 +88,10 @@ exports.createServer = function (options, callback) {
7688
//
7789
exports.createProxyServer = function (options, callback) {
7890
if (!options.latency) {
91+
if (protocols.proxy === 'https') {
92+
options.proxy.https = helpers.https;
93+
}
94+
7995
return httpProxy
8096
.createServer(options.proxy)
8197
.listen(options.port, function () {
@@ -87,9 +103,13 @@ exports.createProxyServer = function (options, callback) {
87103
? new httpProxy.RoutingProxy(options.proxy)
88104
: new httpProxy.HttpProxy(options.proxy);
89105

90-
http.createServer(function (req, res) {
106+
//
107+
// Request handler to use in either `http`
108+
// or `https` server.
109+
//
110+
function requestHandler(req, res) {
91111
var buffer = httpProxy.buffer(req);
92-
112+
93113
setTimeout(function () {
94114
//
95115
// Setup options dynamically for `RoutingProxy.prototype.proxyRequest`
@@ -98,7 +118,13 @@ exports.createProxyServer = function (options, callback) {
98118
buffer = options.routing ? { buffer: buffer } : buffer
99119
proxy.proxyRequest(req, res, buffer);
100120
}, options.latency);
101-
}).listen(options.port, function () {
121+
}
122+
123+
var server = protocols.proxy === 'https'
124+
? https.createServer(helpers.https, requestHandler)
125+
: http.createServer(requestHandler);
126+
127+
server.listen(options.port, function () {
102128
callback(null, this);
103129
});
104130
};

test/helpers/index.js

+67
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,40 @@
66
*
77
*/
88

9+
var fs = require('fs'),
10+
path = require('path');
11+
12+
var fixturesDir = path.join(__dirname, '..', 'fixtures');
13+
14+
//
15+
// @https {Object}
16+
// Returns the necessary `https` credentials.
17+
//
18+
Object.defineProperty(exports, 'https', {
19+
get: function () {
20+
delete this.https;
21+
return this.https = {
22+
key: fs.readFileSync(path.join(fixturesDir, 'agent2-key.pem'), 'utf8'),
23+
cert: fs.readFileSync(path.join(fixturesDir, 'agent2-cert.pem'), 'utf8')
24+
};
25+
}
26+
});
27+
28+
//
29+
// @protocols {Object}
30+
// Returns an object representing the desired protocols
31+
// for the `proxy` and `target` server.
32+
//
33+
Object.defineProperty(exports, 'protocols', {
34+
get: function () {
35+
delete this.protocols;
36+
return this.protocols = {
37+
target: exports.argv.target || 'http',
38+
proxy: exports.argv.proxy || 'http'
39+
};
40+
}
41+
});
42+
943
//
1044
// @nextPort {number}
1145
// Returns an auto-incrementing port for tests.
@@ -31,6 +65,39 @@ Object.defineProperty(exports, 'nextPortPair', {
3165
}
3266
});
3367

68+
//
69+
// ### function describe(prefix)
70+
// #### @prefix {string} Prefix to use before the description
71+
//
72+
// Returns a string representing the protocols that this suite
73+
// is testing based on CLI arguments.
74+
//
75+
exports.describe = function (prefix, base) {
76+
prefix = prefix || '';
77+
base = base || 'http'
78+
79+
function protocol(endpoint) {
80+
return exports.protocols[endpoint] === 'https'
81+
? base + 's'
82+
: base;
83+
}
84+
85+
return [
86+
'node-http-proxy',
87+
prefix,
88+
[
89+
protocol('proxy'),
90+
'-to-',
91+
protocol('target')
92+
].join('')
93+
].filter(Boolean).join('/');
94+
};
95+
96+
//
97+
// Expose the CLI arguments
98+
//
99+
exports.argv = require('optimist').argv;
100+
34101
//
35102
// Export additional helpers for `http` and `websockets`.
36103
//

test/http/http-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ var assert = require('assert'),
3131
request = require('request'),
3232
vows = require('vows'),
3333
macros = require('../macros'),
34-
helpers = require('../helpers/index');
34+
helpers = require('../helpers');
3535

3636
var routeFile = path.join(__dirname, 'config.json');
3737

38-
vows.describe('node-http-proxy/http').addBatch({
38+
vows.describe(helpers.describe()).addBatch({
3939
"With a valid target server": {
4040
"and no latency": {
4141
"and no headers": macros.http.assertProxied(),

test/http/routing-table-test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var assert = require('assert'),
1616

1717
var routeFile = path.join(__dirname, 'config.json');
1818

19-
vows.describe('node-http-proxy/http/routing-table').addBatch({
19+
vows.describe(helpers.describe('routing-table')).addBatch({
2020
"With a routing table": {
2121
"with latency": macros.http.assertProxiedToRoutes({
2222
latency: 2000,
@@ -53,6 +53,7 @@ vows.describe('node-http-proxy/http/routing-table').addBatch({
5353
"after the file has been modified": {
5454
topic: function () {
5555
var config = JSON.parse(fs.readFileSync(routeFile, 'utf8')),
56+
protocol = helpers.protocols.proxy,
5657
port = helpers.nextPort,
5758
that = this;
5859

@@ -72,7 +73,7 @@ vows.describe('node-http-proxy/http/routing-table').addBatch({
7273
)
7374
], function () {
7475
request({
75-
uri: 'http://127.0.0.1:' + that.port,
76+
uri: protocol + '://127.0.0.1:' + that.port,
7677
headers: {
7778
host: 'dynamic.com'
7879
}

test/macros/http.js

+24-11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ exports.assertRequest = function (options) {
3030
request(options.request, this.callback);
3131
},
3232
"should succeed": function (err, res, body) {
33+
assert.isNull(err);
3334
if (options.assert.body) {
3435
assert.equal(body, options.assert.body);
3536
}
@@ -55,11 +56,12 @@ exports.assertRequest = function (options) {
5556
exports.assertProxied = function (options) {
5657
options = options || {};
5758

58-
var ports = options.ports || helpers.nextPortPair,
59-
output = options.output || 'hello world from ' + ports.target,
60-
req = options.request || {};
59+
var ports = options.ports || helpers.nextPortPair,
60+
output = options.output || 'hello world from ' + ports.target,
61+
protocol = helpers.protocols.proxy,
62+
req = options.request || {};
6163

62-
req.uri = req.uri || 'http://127.0.0.1:' + ports.proxy;
64+
req.uri = req.uri || protocol + '://127.0.0.1:' + ports.proxy;
6365

6466
return {
6567
topic: function () {
@@ -79,6 +81,7 @@ exports.assertProxied = function (options) {
7981
proxy: {
8082
forward: options.forward,
8183
target: {
84+
https: helpers.protocols.target === 'https',
8285
host: '127.0.0.1',
8386
port: ports.target
8487
}
@@ -107,10 +110,12 @@ exports.assertProxied = function (options) {
107110
exports.assertInvalidProxy = function (options) {
108111
options = options || {};
109112

110-
var ports = options.ports || helpers.nextPortPair,
111-
req = options.request || {};
113+
var ports = options.ports || helpers.nextPortPair,
114+
req = options.request || {},
115+
protocol = helpers.protocols.proxy;
112116

113-
req.uri = req.uri || 'http://127.0.0.1:' + ports.proxy;
117+
118+
req.uri = req.uri || protocol + '://127.0.0.1:' + ports.proxy;
114119

115120
return {
116121
topic: function () {
@@ -196,9 +201,10 @@ exports.assertProxiedToRoutes = function (options, nested) {
196201
//
197202
var locations = helpers.http.parseRoutes(options),
198203
port = helpers.nextPort,
204+
protocol = helpers.protocols.proxy,
199205
context,
200206
proxy;
201-
207+
202208
if (options.filename) {
203209
//
204210
// If we've been passed a filename write the routes to it
@@ -215,7 +221,14 @@ exports.assertProxiedToRoutes = function (options, nested) {
215221
hostnameOnly: options.hostnameOnly,
216222
router: options.routes
217223
};
218-
}
224+
}
225+
226+
//
227+
// Set the https options if necessary
228+
//
229+
if (helpers.protocols.target === 'https') {
230+
proxy.target = { https: true };
231+
}
219232

220233
//
221234
// Create the test context which creates all target
@@ -271,7 +284,7 @@ exports.assertProxiedToRoutes = function (options, nested) {
271284
"a request to unknown.com": exports.assertRequest({
272285
assert: { statusCode: 404 },
273286
request: {
274-
uri: 'http://127.0.0.1:' + port,
287+
uri: protocol + '://127.0.0.1:' + port,
275288
headers: {
276289
host: 'unknown.com'
277290
}
@@ -285,7 +298,7 @@ exports.assertProxiedToRoutes = function (options, nested) {
285298
locations.forEach(function (location) {
286299
context[location.source.href] = exports.assertRequest({
287300
request: {
288-
uri: 'http://127.0.0.1:' + port + location.source.path,
301+
uri: protocol + '://127.0.0.1:' + port + location.source.path,
289302
headers: {
290303
host: location.source.hostname
291304
}

0 commit comments

Comments
 (0)