Skip to content

Commit 8b3fe32

Browse files
committed
[tests] added the ws passes test and the streams webscokets test
1 parent e0faaaf commit 8b3fe32

File tree

2 files changed

+196
-0
lines changed

2 files changed

+196
-0
lines changed

Diff for: 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+
});

Diff for: test/lib-caronte-streams-websockets-test.js

+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)