Skip to content

Commit 5bb83b9

Browse files
committed
[tests] make the tests run with the last refactor
1 parent 275a519 commit 5bb83b9

2 files changed

+104
-46
lines changed

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() {
+103-45
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,145 @@
1-
var caronte = require('../lib/caronte/passes/ws'),
1+
var caronte = require('../lib/caronte/passes/ws-incoming'),
22
expect = require('expect.js');
33

4-
describe('lib/caronte/passes/ws.js', function () {
4+
describe('lib/caronte/passes/ws-incoming.js', function () {
55
describe('#checkMethodAndHeader', function () {
66
it('should drop non-GET connections', function () {
7-
var endCalled = false,
7+
var destroyCalled = false,
88
stubRequest = {
99
method: 'DELETE',
10-
headers: {},
11-
end: function () {
12-
// Simulate Stream.end() method when call
13-
endCalled = true;
14-
}
10+
headers: {}
1511
},
16-
returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {});
12+
stubSocket = {
13+
destroy: function () {
14+
// Simulate Socket.destroy() method when call
15+
destroyCalled = true;
16+
}
17+
}
18+
returnValue = caronte.checkMethodAndHeader(stubRequest, stubSocket);
1719
expect(returnValue).to.be(true);
18-
expect(endCalled).to.be(true);
20+
expect(destroyCalled).to.be(true);
1921
})
2022

2123
it('should drop connections when no upgrade header', function () {
22-
var endCalled = false,
24+
var destroyCalled = false,
2325
stubRequest = {
2426
method: 'GET',
25-
headers: {},
26-
end: function () {
27-
// Simulate Stream.end() method when call
28-
endCalled = true;
29-
}
27+
headers: {}
3028
},
31-
returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {});
29+
stubSocket = {
30+
destroy: function () {
31+
// Simulate Socket.destroy() method when call
32+
destroyCalled = true;
33+
}
34+
}
35+
returnValue = caronte.checkMethodAndHeader(stubRequest, stubSocket);
3236
expect(returnValue).to.be(true);
33-
expect(endCalled).to.be(true);
37+
expect(destroyCalled).to.be(true);
3438
})
3539

3640
it('should drop connections when upgrade header is different of `websocket`', function () {
37-
var endCalled = false,
41+
var destroyCalled = false,
3842
stubRequest = {
3943
method: 'GET',
4044
headers: {
4145
upgrade: 'anotherprotocol'
42-
},
43-
end: function () {
44-
// Simulate Stream.end() method when call
45-
endCalled = true;
4646
}
4747
},
48-
returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {});
48+
stubSocket = {
49+
destroy: function () {
50+
// Simulate Socket.destroy() method when call
51+
destroyCalled = true;
52+
}
53+
}
54+
returnValue = caronte.checkMethodAndHeader(stubRequest, stubSocket);
4955
expect(returnValue).to.be(true);
50-
expect(endCalled).to.be(true);
56+
expect(destroyCalled).to.be(true);
5157
})
5258

5359
it('should return nothing when all is ok', function () {
54-
var endCalled = false,
60+
var destroyCalled = false,
5561
stubRequest = {
5662
method: 'GET',
5763
headers: {
5864
upgrade: 'websocket'
59-
},
60-
end: function () {
61-
// Simulate Stream.end() method when call
62-
endCalled = true;
6365
}
6466
},
65-
returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {});
67+
stubSocket = {
68+
destroy: function () {
69+
// Simulate Socket.destroy() method when call
70+
destroyCalled = true;
71+
}
72+
}
73+
returnValue = caronte.checkMethodAndHeader(stubRequest, stubSocket);
6674
expect(returnValue).to.be(undefined);
67-
expect(endCalled).to.be(false);
75+
expect(destroyCalled).to.be(false);
6876
})
6977
});
7078

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+
71108
describe('#XHeaders', function () {
72-
// var stubRequest = {
73-
// connection: {
74-
// remoteAddress: '192.168.1.2',
75-
// remotePort: '8080'
76-
// },
77-
// headers: {}
78-
// }
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+
});
79127

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-
// });
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+
});
86144
});
87145
});

0 commit comments

Comments
 (0)