Skip to content

Commit 4d18ac1

Browse files
committed
[test] Add WebSocket tests
1 parent 7cbf447 commit 4d18ac1

File tree

4 files changed

+115
-30
lines changed

4 files changed

+115
-30
lines changed

examples/web-socket-proxy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
var sys = require('sys'),
2828
http = require('http'),
29-
websocket = require('./websocket'),
29+
websocket = require('./../vendor/websocket'),
3030
utils = require('socket.io/lib/socket.io/utils'),
3131
io = require('socket.io'),
3232
httpProxy = require('./../lib/node-http-proxy');

test/helpers.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ TestRunner.prototype.startProxyServer = function (port, targetPort, host, callba
9090

9191
proxyServer.listen(port, function () {
9292
that.testServers.push(proxyServer);
93-
callback();
93+
callback(null, proxyServer);
9494
});
9595
};
9696

@@ -160,7 +160,7 @@ TestRunner.prototype.startProxyServerWithForwarding = function (port, targetPort
160160
var that = this, proxyServer = httpProxy.createServer(targetPort, host, options);
161161
proxyServer.listen(port, function () {
162162
that.testServers.push(proxyServer);
163-
callback();
163+
callback(null, proxyServer);
164164
});
165165
};
166166

@@ -176,7 +176,7 @@ TestRunner.prototype.startTargetServer = function (port, output, callback) {
176176

177177
targetServer.listen(port, function () {
178178
that.testServers.push(targetServer);
179-
callback();
179+
callback(null, targetServer);
180180
});
181181
};
182182

test/node-http-proxy-test.js

-26
Original file line numberDiff line numberDiff line change
@@ -80,32 +80,6 @@ vows.describe('node-http-proxy').addBatch({
8080
}
8181
}
8282
}
83-
}).addBatch({
84-
"When using server created by httpProxy.createServer()": {
85-
"an incoming WebSocket request to the helloNode server": {
86-
"with no latency" : {
87-
// Remark: This test is not working
88-
/*topic: function () {
89-
runner.startProxyServer(8086, 8087, 'localhost'),
90-
runner.startTargetServer(8087, 'hello world');
91-
var options = {
92-
method: 'GET',
93-
uri: 'http://localhost:8086',
94-
headers: {
95-
'Upgrade': 'WebSocket',
96-
'Connection': 'WebSocket',
97-
'Host': 'localhost'
98-
}
99-
};
100-
101-
request(options, this.callback);
102-
},
103-
"should receive 'hello world'": function (err, res, body) {
104-
assert.equal(body, 'hello world');
105-
}*/
106-
}
107-
}
108-
}
10983
}).addBatch({
11084
"When the tests are over": {
11185
topic: function () {

test/web-socket-proxy-test.js

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 vows = require('vows'),
28+
util = require('util'),
29+
request = require('request'),
30+
assert = require('assert'),
31+
io = require('socket.io'),
32+
utils = require('socket.io/lib/socket.io/utils'),
33+
websocket = require('./../vendor/websocket'),
34+
helpers = require('./helpers');
35+
36+
var runner = new helpers.TestRunner();
37+
38+
vows.describe('node-http-proxy/websocket').addBatch({
39+
"When using server created by httpProxy.createServer()": {
40+
"with no latency" : {
41+
"when an inbound message is sent from a WebSocket client": {
42+
topic: function () {
43+
var that = this;
44+
45+
runner.startTargetServer(8130, 'hello websocket', function (err, target) {
46+
var socket = io.listen(target);
47+
48+
socket.on('connection', function (client) {
49+
client.on('message', function (msg) {
50+
that.callback(null, msg);
51+
});
52+
});
53+
54+
runner.startProxyServer(8131, 8130, 'localhost', function (err, proxy) {
55+
//
56+
// Setup the web socket against our proxy
57+
//
58+
var ws = new websocket.WebSocket('ws://localhost:8131/socket.io/websocket/', 'borf');
59+
60+
ws.on('open', function () {
61+
ws.send(utils.encode('from client'));
62+
});
63+
});
64+
})
65+
},
66+
"the target server should receive the message": function (err, msg) {
67+
assert.equal(msg, 'from client');
68+
}
69+
},
70+
"when an outbound message is sent from the target server": {
71+
topic: function () {
72+
var that = this;
73+
74+
runner.startTargetServer(8132, 'hello websocket', function (err, target) {
75+
var socket = io.listen(target);
76+
77+
socket.on('connection', function (client) {
78+
socket.broadcast('from server');
79+
});
80+
81+
runner.startProxyServer(8133, 8132, 'localhost', function (err, proxy) {
82+
//
83+
// Setup the web socket against our proxy
84+
//
85+
var ws = new websocket.WebSocket('ws://localhost:8133/socket.io/websocket/', 'borf');
86+
87+
ws.on('message', function (msg) {
88+
msg = utils.decode(msg);
89+
if (!/\d+/.test(msg)) {
90+
that.callback(null, msg);
91+
}
92+
});
93+
});
94+
})
95+
},
96+
"the client should receive the message": function (err, msg) {
97+
assert.equal(msg, 'from server');
98+
}
99+
}
100+
}
101+
}
102+
}).addBatch({
103+
"When the tests are over": {
104+
topic: function () {
105+
return runner.closeServers();
106+
},
107+
"the servers should clean up": function () {
108+
assert.isTrue(true);
109+
}
110+
}
111+
}).export(module);

0 commit comments

Comments
 (0)