Skip to content

Commit 9efa40a

Browse files
committed
Merge pull request #476 from nodejitsu/caronte-tests
[merge] caronte tests
2 parents c01fd2c + 8eff1a1 commit 9efa40a

11 files changed

+444
-363
lines changed

Diff for: .travis.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: node_js
2+
node_js:
3+
- "0.10"
4+
- "0.11"
5+
6+
notifications:
7+
email:
8+
9+
irc: "irc.freenode.org#nodejitsu"

Diff for: lib/caronte/passes/ws-incoming.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function XHeaders(req, socket, options) {
5555
if(!options.xfwd) return;
5656

5757
var values = {
58-
for : req.connection.remoteAddsockets || req.socket.remoteAddsockets,
58+
for : req.connection.remoteAddress || req.socket.remoteAddress,
5959
port : req.connection.remotePort || req.socket.remotePort,
6060
proto: req.connection.pair ? 'wss' : 'ws'
6161
};

Diff for: package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
"coveralls" : "*",
1717
"mocha-lcov-reporter": "*",
1818
"blanket" : "*",
19-
"ws" : "*"
19+
"ws" : "*",
20+
"socket.io" : "*",
21+
"socket.io-client" : "*"
2022
},
2123
"scripts" : {
22-
"blanket" : { "pattern": "caronte/lib" },
23-
"test" : "mocha -R landing test/*-test.js",
24-
"test-cov" : "mocha --require blanket -R html-cov > cov/coverage.html"
24+
"blanket" : { "pattern": "lib/caronte" },
25+
"test" : "./node_modules/.bin/mocha -R landing test/*-test.js",
26+
"test-cov" : "./node_modules/.bin/mocha --require blanket -R html-cov > cov/coverage.html"
2527
},
2628

2729
"engines" : {

Diff for: test/lib-caronte-common-test.js

+61-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
var common = require('../lib/caronte/common'),
22
expect = require('expect.js');
33

4-
describe('lib/caronte/common.js', function() {
5-
describe('#setupOutgoing', function() {
6-
it('should setup the right headers', function() {
4+
describe('lib/caronte/common.js', function () {
5+
describe('#setupOutgoing', function () {
6+
it('should setup the correct headers', function () {
77
var outgoing = {};
88
common.setupOutgoing(outgoing,
99
{
@@ -17,19 +17,75 @@ describe('lib/caronte/common.js', function() {
1717
},
1818
{
1919
method : 'i',
20-
path : 'am',
20+
url : 'am',
2121
headers : 'proxy'
2222
});
2323

2424
expect(outgoing.host).to.eql('hey');
2525
expect(outgoing.hostname).to.eql('how');
2626
expect(outgoing.socketPath).to.eql('are');
2727
expect(outgoing.port).to.eql('you');
28-
//expect(outgoing.agent).to.eql('?');
28+
expect(outgoing.agent).to.eql('?');
2929

3030
expect(outgoing.method).to.eql('i');
3131
expect(outgoing.path).to.eql('am');
3232
expect(outgoing.headers).to.eql('proxy')
3333
});
34+
35+
it('set the port according to the protocol', function () {
36+
var outgoing = {};
37+
common.setupOutgoing(outgoing,
38+
{
39+
target: {
40+
host : 'how',
41+
hostname : 'are',
42+
socketPath: 'you',
43+
agent : '?',
44+
protocol: 'https:'
45+
}
46+
},
47+
{
48+
method : 'i',
49+
url : 'am',
50+
headers : 'proxy'
51+
});
52+
53+
expect(outgoing.host).to.eql('how');
54+
expect(outgoing.hostname).to.eql('are');
55+
expect(outgoing.socketPath).to.eql('you');
56+
expect(outgoing.agent).to.eql('?');
57+
58+
expect(outgoing.method).to.eql('i');
59+
expect(outgoing.path).to.eql('am');
60+
expect(outgoing.headers).to.eql('proxy')
61+
62+
expect(outgoing.port).to.eql(443);
63+
});
64+
});
65+
66+
describe('#setupSocket', function () {
67+
it('should setup a socket', function () {
68+
var socketConfig = {
69+
timeout: null,
70+
nodelay: false,
71+
keepalive: false
72+
},
73+
stubSocket = {
74+
setTimeout: function (num) {
75+
socketConfig.timeout = num;
76+
},
77+
setNoDelay: function (bol) {
78+
socketConfig.nodelay = bol;
79+
},
80+
setKeepAlive: function (bol) {
81+
socketConfig.keepalive = bol;
82+
}
83+
}
84+
returnValue = common.setupSocket(stubSocket);
85+
86+
expect(socketConfig.timeout).to.eql(0);
87+
expect(socketConfig.nodelay).to.eql(true);
88+
expect(socketConfig.keepalive).to.eql(true);
89+
});
3490
});
3591
});

Diff for: test/lib-caronte-passes-web-test.js renamed to test/lib-caronte-passes-web-incoming-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var caronte = require('../lib/caronte/passes/web'),
1+
var caronte = require('../lib/caronte/passes/web-incoming'),
22
expect = require('expect.js');
33

44
describe('lib/caronte/passes/web.js', function() {

Diff for: test/lib-caronte-passes-ws-incoming-test.js

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
var caronte = require('../lib/caronte/passes/ws-incoming'),
2+
expect = require('expect.js');
3+
4+
describe('lib/caronte/passes/ws-incoming.js', function () {
5+
describe('#checkMethodAndHeader', function () {
6+
it('should drop non-GET connections', function () {
7+
var destroyCalled = false,
8+
stubRequest = {
9+
method: 'DELETE',
10+
headers: {}
11+
},
12+
stubSocket = {
13+
destroy: function () {
14+
// Simulate Socket.destroy() method when call
15+
destroyCalled = true;
16+
}
17+
}
18+
returnValue = caronte.checkMethodAndHeader(stubRequest, stubSocket);
19+
expect(returnValue).to.be(true);
20+
expect(destroyCalled).to.be(true);
21+
})
22+
23+
it('should drop connections when no upgrade header', function () {
24+
var destroyCalled = false,
25+
stubRequest = {
26+
method: 'GET',
27+
headers: {}
28+
},
29+
stubSocket = {
30+
destroy: function () {
31+
// Simulate Socket.destroy() method when call
32+
destroyCalled = true;
33+
}
34+
}
35+
returnValue = caronte.checkMethodAndHeader(stubRequest, stubSocket);
36+
expect(returnValue).to.be(true);
37+
expect(destroyCalled).to.be(true);
38+
})
39+
40+
it('should drop connections when upgrade header is different of `websocket`', function () {
41+
var destroyCalled = false,
42+
stubRequest = {
43+
method: 'GET',
44+
headers: {
45+
upgrade: 'anotherprotocol'
46+
}
47+
},
48+
stubSocket = {
49+
destroy: function () {
50+
// Simulate Socket.destroy() method when call
51+
destroyCalled = true;
52+
}
53+
}
54+
returnValue = caronte.checkMethodAndHeader(stubRequest, stubSocket);
55+
expect(returnValue).to.be(true);
56+
expect(destroyCalled).to.be(true);
57+
})
58+
59+
it('should return nothing when all is ok', function () {
60+
var destroyCalled = false,
61+
stubRequest = {
62+
method: 'GET',
63+
headers: {
64+
upgrade: 'websocket'
65+
}
66+
},
67+
stubSocket = {
68+
destroy: function () {
69+
// Simulate Socket.destroy() method when call
70+
destroyCalled = true;
71+
}
72+
}
73+
returnValue = caronte.checkMethodAndHeader(stubRequest, stubSocket);
74+
expect(returnValue).to.be(undefined);
75+
expect(destroyCalled).to.be(false);
76+
})
77+
});
78+
79+
describe('#setupSocket', function () {
80+
it('Set the correct config to the socket', function () {
81+
var stubSocket = {
82+
setTimeout: function (num) {
83+
// Simulate Socket.setTimeout()
84+
socketConfig.timeout = num;
85+
},
86+
setNoDelay: function (bol) {
87+
// Simulate Socket.setNoDelay()
88+
socketConfig.nodelay = bol;
89+
},
90+
setKeepAlive: function (bol) {
91+
// Simulate Socket.setKeepAlive()
92+
socketConfig.keepalive = bol;
93+
}
94+
},
95+
socketConfig = {
96+
timeout: null,
97+
nodelay: false,
98+
keepalive: false
99+
},
100+
returnValue = caronte.setupSocket({}, stubSocket);
101+
expect(returnValue).to.be(undefined);
102+
expect(socketConfig.timeout).to.eql(0);
103+
expect(socketConfig.nodelay).to.eql(true);
104+
expect(socketConfig.keepalive).to.eql(true);
105+
});
106+
});
107+
108+
describe('#XHeaders', function () {
109+
it('return if no forward request', function () {
110+
var returnValue = caronte.XHeaders({}, {}, {});
111+
expect(returnValue).to.be(undefined);
112+
});
113+
114+
it('set the correct x-forwarded-* headers from req.connection', function () {
115+
var stubRequest = {
116+
connection: {
117+
remoteAddress: '192.168.1.2',
118+
remotePort: '8080'
119+
},
120+
headers: {}
121+
}
122+
caronte.XHeaders(stubRequest, {}, { xfwd: true });
123+
expect(stubRequest.headers['x-forwarded-for']).to.be('192.168.1.2');
124+
expect(stubRequest.headers['x-forwarded-port']).to.be('8080');
125+
expect(stubRequest.headers['x-forwarded-proto']).to.be('ws');
126+
});
127+
128+
it('set the correct x-forwarded-* headers from req.socket', function () {
129+
var stubRequest = {
130+
socket: {
131+
remoteAddress: '192.168.1.3',
132+
remotePort: '8181'
133+
},
134+
connection: {
135+
pair: true
136+
},
137+
headers: {}
138+
};
139+
caronte.XHeaders(stubRequest, {}, { xfwd: true });
140+
expect(stubRequest.headers['x-forwarded-for']).to.be('192.168.1.3');
141+
expect(stubRequest.headers['x-forwarded-port']).to.be('8181');
142+
expect(stubRequest.headers['x-forwarded-proto']).to.be('wss');
143+
});
144+
});
145+
});

Diff for: test/lib-caronte-passes-ws-test.js

-87
This file was deleted.

0 commit comments

Comments
 (0)