Skip to content

Commit 0fb3381

Browse files
committed
merge with @cronopio
2 parents c9cd6d2 + 8b3fe32 commit 0fb3381

File tree

5 files changed

+209
-12
lines changed

5 files changed

+209
-12
lines changed

lib/caronte/passes/ws.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ var passes = exports;
2020
*/
2121

2222
function checkMethodAndHeader (req, res, options) {
23-
if (req.method !== 'GET' || req.headers.upgrade.toLowerCase() !== 'websocket') {
24-
req.end();
25-
26-
return true;
23+
if (req.method !== 'GET' || !req.headers.upgrade) {
24+
req.end(); return true;
25+
}
26+
27+
if (req.headers.upgrade.toLowerCase() !== 'websocket') {
28+
req.end(); return true;
2729
}
2830
},
2931

lib/caronte/streams/proxy.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var Duplex = require('stream').Duplex,
33
http = require('http'),
44
https = require('https');
55

6+
module.exports = ProxyStream;
7+
68
function ProxyStream(options, res) {
79
Duplex.call(this);
810

@@ -100,5 +102,3 @@ ProxyStream.prototype._read = function(size) {
100102

101103
this.push(chunk);
102104
};
103-
104-
module.exports = ProxyStream;

lib/caronte/streams/websocket.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
var Duplex = require('stream').Duplex,
2-
common = require('common'),
2+
common = require('../common'),
33
http = require('http'),
44
https = require('https');
55

6+
module.exports = WebsocketStream;
7+
68
function WebsocketStream(options, res) {
79
Duplex.call(this);
810

@@ -34,7 +36,7 @@ WebsocketStream.prototype.onPipe = function(req) {
3436
});
3537
};
3638

37-
WebsocketStream.prototye.onFinish = function() {
39+
WebsocketStream.prototype.onFinish = function() {
3840
this.proxyReq.end();
3941
};
4042

@@ -55,7 +57,4 @@ WebsocketStream.prototype._write = function(chunk, encoding, callback) {
5557

5658
WebsocketStream.prototype._read = function(size) {
5759

58-
};
59-
60-
61-
WebsocketStream.prototype
60+
};

test/lib-caronte-passes-ws-test.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
var caronte = require('../lib/caronte/passes/ws'),
2+
expect = require('expect.js');
3+
4+
describe('lib/caronte/passes/ws.js', function () {
5+
describe('#checkMethodAndHeader', function () {
6+
it('should drop non-GET connections', function () {
7+
var endCalled = false,
8+
stubRequest = {
9+
method: 'DELETE',
10+
headers: {},
11+
end: function () {
12+
// Simulate Stream.end() method when call
13+
endCalled = true;
14+
}
15+
},
16+
returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {});
17+
expect(returnValue).to.be(true);
18+
expect(endCalled).to.be(true);
19+
})
20+
21+
it('should drop connections when no upgrade header', function () {
22+
var endCalled = false,
23+
stubRequest = {
24+
method: 'GET',
25+
headers: {},
26+
end: function () {
27+
// Simulate Stream.end() method when call
28+
endCalled = true;
29+
}
30+
},
31+
returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {});
32+
expect(returnValue).to.be(true);
33+
expect(endCalled).to.be(true);
34+
})
35+
36+
it('should drop connections when upgrade header is different of `websocket`', function () {
37+
var endCalled = false,
38+
stubRequest = {
39+
method: 'GET',
40+
headers: {
41+
upgrade: 'anotherprotocol'
42+
},
43+
end: function () {
44+
// Simulate Stream.end() method when call
45+
endCalled = true;
46+
}
47+
},
48+
returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {});
49+
expect(returnValue).to.be(true);
50+
expect(endCalled).to.be(true);
51+
})
52+
53+
it('should return nothing when all is ok', function () {
54+
var endCalled = false,
55+
stubRequest = {
56+
method: 'GET',
57+
headers: {
58+
upgrade: 'websocket'
59+
},
60+
end: function () {
61+
// Simulate Stream.end() method when call
62+
endCalled = true;
63+
}
64+
},
65+
returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {});
66+
expect(returnValue).to.be(undefined);
67+
expect(endCalled).to.be(false);
68+
})
69+
});
70+
71+
describe('#XHeaders', function () {
72+
// var stubRequest = {
73+
// connection: {
74+
// remoteAddress: '192.168.1.2',
75+
// remotePort: '8080'
76+
// },
77+
// headers: {}
78+
// }
79+
80+
// it('set the correct x-forwarded-* headers', function () {
81+
// caronte.XHeaders(stubRequest, {}, { xfwd: true });
82+
// expect(stubRequest.headers['x-forwarded-for']).to.be('192.168.1.2');
83+
// expect(stubRequest.headers['x-forwarded-port']).to.be('8080');
84+
// expect(stubRequest.headers['x-forwarded-proto']).to.be('http');
85+
// });
86+
});
87+
});
+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
var caronte = require('../'),
2+
WebSocket = require('../lib/caronte/streams/websocket');
3+
expect = require('expect.js'),
4+
Duplex = require('stream').Duplex,
5+
http = require('http');
6+
7+
8+
describe('lib/caronte/streams/websocket.js', function () {
9+
describe('WebSocket stream constructor', function () {
10+
it('should be an instance of Duplex stream and get the correct options and methods', function () {
11+
var stubOptions = {
12+
key: 'value'
13+
};
14+
var WebSocketStream = new WebSocket(stubOptions);
15+
16+
expect(WebSocketStream).to.be.a(Duplex);
17+
expect(WebSocketStream.options).to.eql({ key: 'value' });
18+
expect(WebSocketStream.onPipe).to.be.a('function');
19+
expect(WebSocketStream.onFinish).to.be.a('function');
20+
expect(WebSocketStream._events).to.have.property('pipe');
21+
expect(WebSocketStream._events).to.have.property('finish');
22+
});
23+
});
24+
25+
describe('caronte createWebSocketServer() method', function () {
26+
// it('should make the request on pipe and finish it', function(done) {
27+
// var proxy = caronte.createProxyServer({
28+
// target: 'http://127.0.0.1:8080'
29+
// }).listen('8081');
30+
31+
// var source = http.createServer(function(req, res) {
32+
// expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1');
33+
// source.close();
34+
// proxy.close();
35+
// done();
36+
// });
37+
38+
// source.listen('8080');
39+
40+
// http.request({
41+
// hostname: '127.0.0.1',
42+
// port: '8081',
43+
// method: 'POST',
44+
// headers: {
45+
// 'x-forwarded-for': '127.0.0.1'
46+
// }
47+
// }, function() {}).end();
48+
// });
49+
});
50+
51+
describe('caronte createProxyServer() method with response', function () {
52+
// it('should make the request, handle response and finish it', function(done) {
53+
// var proxy = caronte.createProxyServer({
54+
// target: 'http://127.0.0.1:8080'
55+
// }).listen('8081');
56+
57+
// var source = http.createServer(function(req, res) {
58+
// expect(req.method).to.eql('GET');
59+
// res.writeHead(200, {'Content-Type': 'text/plain'})
60+
// res.end('Hello from ' + source.address().port);
61+
// });
62+
63+
// source.listen('8080');
64+
65+
// http.request({
66+
// hostname: '127.0.0.1',
67+
// port: '8081',
68+
// method: 'GET',
69+
// }, function(res) {
70+
// expect(res.statusCode).to.eql(200);
71+
72+
// res.on('data', function (data) {
73+
// expect(data.toString()).to.eql('Hello from 8080');
74+
// });
75+
76+
// res.on('end', function () {
77+
// source.close();
78+
// proxy.close();
79+
// done();
80+
// });
81+
// }).end();
82+
// });
83+
});
84+
85+
describe('caronte createProxyServer() method with error response', function () {
86+
// it('should make the request and response with error', function(done) {
87+
// var proxy = caronte.createProxyServer({
88+
// target: 'http://127.0.0.1:8080'
89+
// }).listen('8081');
90+
91+
// http.request({
92+
// hostname: '127.0.0.1',
93+
// port: '8081',
94+
// method: 'GET',
95+
// }, function(res) {
96+
// expect(res.statusCode).to.eql(500);
97+
98+
// res.on('data', function (data) {
99+
// expect(data.toString()).to.eql('Internal Server Error');
100+
// });
101+
102+
// res.on('end', function () {
103+
// proxy.close();
104+
// done();
105+
// });
106+
// }).end();
107+
// });
108+
});
109+
});

0 commit comments

Comments
 (0)