Skip to content

Commit daf9231

Browse files
committed
[test fix] A few minor fixes to ensure basic WebSocket tests are working. Better scope tests by supported protocol
1 parent be4562d commit daf9231

File tree

7 files changed

+120
-61
lines changed

7 files changed

+120
-61
lines changed

Diff for: lib/node-http-proxy.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ exports.createServer = function () {
7878
case 'function': callback = arg; break;
7979
};
8080
});
81-
81+
8282
if (!host && !port && !options) {
8383
//
8484
// If `host`, `port` and `options` are all not passed, then
@@ -285,12 +285,12 @@ exports._getProtocol = function _getProtocol (options) {
285285
// containing the relevant `ca`, `key`, and `cert` properties.
286286
//
287287
exports._getBase = function _getBase (options) {
288-
var result = {};
288+
var result = function () {};
289289

290290
if (options.https && typeof options.https === 'object') {
291291
['ca', 'cert', 'key'].forEach(function (key) {
292292
if (options.https[key]) {
293-
result[key] = options.https[key];
293+
result.prototype[key] = options.https[key];
294294
}
295295
});
296296
}

Diff for: lib/node-http-proxy/http-proxy.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ var HttpProxy = exports.HttpProxy = function (options) {
9898
// be provided or the operation will fail with an `origin mismatch`
9999
// by definition.
100100
//
101-
this.source = options.source || { host: 'localhost', port: 8000 };
101+
this.source = options.source || { host: 'localhost', port: 8000 };
102+
this.source.https = this.source.https || options.https;
102103
this.changeOrigin = options.changeOrigin || false;
103104
};
104105

@@ -114,7 +115,7 @@ util.inherits(HttpProxy, events.EventEmitter);
114115
HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
115116
var self = this,
116117
errState = false,
117-
outgoing = new Object(this.target.base),
118+
outgoing = new(this.target.base),
118119
reverseProxy;
119120

120121
//
@@ -286,7 +287,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
286287
//
287288
HttpProxy.prototype._forwardRequest = function (req) {
288289
var self = this,
289-
outgoing = new Object(this.forward.base),
290+
outgoing = new(this.forward.base),
290291
forwardProxy;
291292

292293
//
@@ -336,7 +337,7 @@ HttpProxy.prototype._forwardRequest = function (req) {
336337
};
337338

338339
//
339-
// ### function proxyWebSocketRequest (req, socket, head, options)
340+
// ### function proxyWebSocketRequest (req, socket, head, buffer)
340341
// #### @req {ServerRequest} Websocket request to proxy.
341342
// #### @socket {net.Socket} Socket for the underlying HTTP request
342343
// #### @head {string} Headers for the Websocket request.
@@ -346,7 +347,7 @@ HttpProxy.prototype._forwardRequest = function (req) {
346347
//
347348
HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) {
348349
var self = this,
349-
outgoing = new Object(this.target.base);
350+
outgoing = new(this.target.base);
350351
listeners = {},
351352
errState = false,
352353
CRLF = '\r\n';
@@ -478,7 +479,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
478479
var agent = this.target.agent,
479480
protocolName = this.target.https ? 'https' : 'http',
480481
portUri = getPort(this.source.port),
481-
remoteHost = options.host + portUri;
482+
remoteHost = this.target.host + portUri;
482483

483484
//
484485
// Change headers (if requested).
@@ -567,7 +568,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
567568
// Get the Non-Printable data
568569
data = data.slice(Buffer.byteLength(sdata), data.length);
569570

570-
if (self.https && !self.target.https) {
571+
if (self.source.https && !self.target.https) {
571572
//
572573
// If the proxy server is running HTTPS but the client is running
573574
// HTTP then replace `ws` with `wss` in the data sent back to the client.

Diff for: test/helpers.js

-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, creat
112112
//
113113
// WebSocketTest
114114
//
115-
116115
TestRunner.prototype.webSocketTest = function (options) {
117116
var self = this;
118117

Diff for: test/proxy-table-test.js renamed to test/http/http-proxy-table-test.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ var assert = require('assert'),
1212
argv = require('optimist').argv,
1313
request = require('request'),
1414
vows = require('vows'),
15-
helpers = require('./helpers'),
16-
TestRunner = helpers.TestRunner;
15+
helpers = require('../helpers');
1716

1817
var protocol = argv.https ? 'https' : 'http',
19-
runner = new TestRunner(protocol),
18+
runner = new helpers.TestRunner(protocol),
2019
routeFile = path.join(__dirname, 'config.json');
2120

2221
var fileOptions = {

Diff for: test/node-http-proxy-test.js renamed to test/http/http-proxy-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var assert = require('assert'),
2929
argv = require('optimist').argv,
3030
request = require('request'),
3131
vows = require('vows'),
32-
helpers = require('./helpers');
32+
helpers = require('../helpers');
3333

3434
var forwardOptions = {
3535
forward: {

Diff for: test/websocket/websocket-proxy-table-test.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
node-http-proxy-test.js: http proxy for node.js
3+
4+
Copyright (c) 2010 Charlie Robbins, Marak Squires and Fedor Indutny
5+
6+
Permission is hereby granted, free of charge, to any person obtaining
7+
a copy of this software and associated documentation files (the
8+
"Software"), to deal in the Software without restriction, including
9+
without limitation the rights to use, copy, modify, merge, publish,
10+
distribute, sublicense, and/or sell copies of the Software, and to
11+
permit persons to whom the Software is furnished to do so, subject to
12+
the following conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
25+
*/
26+
27+
var util = require('util'),
28+
assert = require('assert'),
29+
argv = require('optimist').argv,
30+
colors = require('colors'),
31+
request = require('request'),
32+
vows = require('vows'),
33+
websocket = require('../vendor/websocket'),
34+
helpers = require('../helpers');
35+
36+
try {
37+
var utils = require('socket.io/lib/socket.io/utils'),
38+
io = require('socket.io');
39+
}
40+
catch (ex) {
41+
console.error('Socket.io is required for this example:');
42+
console.error('npm ' + 'install'.green + ' [email protected]'.magenta);
43+
process.exit(1);
44+
}
45+
46+
var protocol = argv.https ? 'https' : 'http',
47+
wsprotocol = argv.https ? 'wss' : 'ws',
48+
runner = new helpers.TestRunner(protocol);
49+
50+
vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
51+
"When using server created by httpProxy.createServer()": {
52+
"using proxy table with no latency": {
53+
"when an inbound message is sent from a WebSocket client": {
54+
topic: function () {
55+
var that = this
56+
headers = {};
57+
58+
runner.webSocketTestWithTable({
59+
io: io,
60+
host: 'localhost',
61+
wsprotocol: wsprotocol,
62+
protocol: protocol,
63+
router: {'localhost':'localhost:8230'},
64+
ports: {
65+
target: 8230,
66+
proxy: 8231
67+
},
68+
onListen: function (socket) {
69+
socket.on('connection', function (client) {
70+
client.on('message', function (msg) {
71+
that.callback(null, msg, headers);
72+
});
73+
});
74+
},
75+
onWsupgrade: function (req, res) {
76+
headers.request = req;
77+
headers.response = res.headers;
78+
},
79+
onOpen: function (ws) {
80+
ws.send(utils.encode('from client'));
81+
}
82+
});
83+
},
84+
"the target server should receive the message": function (err, msg, headers) {
85+
assert.equal(msg, 'from client');
86+
},
87+
"the origin and sec-websocket-origin headers should match": function (err, msg, headers) {
88+
assert.isString(headers.response['sec-websocket-location']);
89+
assert.isTrue(headers.response['sec-websocket-location'].indexOf(wsprotocol) !== -1);
90+
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
91+
}
92+
}
93+
}
94+
}
95+
}).addBatch({
96+
"When the tests are over": {
97+
topic: function () {
98+
return runner.closeServers();
99+
},
100+
"the servers should clean up": function () {
101+
assert.isTrue(true);
102+
}
103+
}
104+
}).export(module);

Diff for: test/web-socket-proxy-test.js renamed to test/websocket/websocket-proxy-test.js

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

3636
try {
3737
var utils = require('socket.io/lib/socket.io/utils'),
@@ -48,50 +48,6 @@ var protocol = argv.https ? 'https' : 'http',
4848
runner = new helpers.TestRunner(protocol);
4949

5050
vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
51-
"when using proxy table":{
52-
"with no latency" : {
53-
"when an inbound message is sent from a WebSocket client": {
54-
topic: function () {
55-
var that = this
56-
headers = {};
57-
58-
runner.webSocketTestWithTable({
59-
io: io,
60-
host: 'localhost',
61-
wsprotocol: wsprotocol,
62-
protocol: protocol,
63-
router: {'localhost':'localhost:8230'},
64-
ports: {
65-
target: 8230,
66-
proxy: 8231
67-
},
68-
onListen: function (socket) {
69-
socket.on('connection', function (client) {
70-
client.on('message', function (msg) {
71-
that.callback(null, msg, headers);
72-
});
73-
});
74-
},
75-
onWsupgrade: function (req, res) {
76-
headers.request = req;
77-
headers.response = res.headers;
78-
},
79-
onOpen: function (ws) {
80-
ws.send(utils.encode('from client'));
81-
}
82-
});
83-
},
84-
"the target server should receive the message": function (err, msg, headers) {
85-
assert.equal(msg, 'from client');
86-
},
87-
"the origin and sec-websocket-origin headers should match": function (err, msg, headers) {
88-
assert.isString(headers.response['sec-websocket-location']);
89-
assert.isTrue(headers.response['sec-websocket-location'].indexOf(wsprotocol) !== -1);
90-
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
91-
}
92-
}
93-
}
94-
},
9551
"When using server created by httpProxy.createServer()": {
9652
"with no latency" : {
9753
"when an inbound message is sent from a WebSocket client": {

0 commit comments

Comments
 (0)