|
1 |
| -var caronte = require('../lib/caronte/passes/ws'), |
| 1 | +var caronte = require('../lib/caronte/passes/ws-incoming'), |
2 | 2 | expect = require('expect.js');
|
3 | 3 |
|
4 |
| -describe('lib/caronte/passes/ws.js', function () { |
| 4 | +describe('lib/caronte/passes/ws-incoming.js', function () { |
5 | 5 | describe('#checkMethodAndHeader', function () {
|
6 | 6 | it('should drop non-GET connections', function () {
|
7 |
| - var endCalled = false, |
| 7 | + var destroyCalled = false, |
8 | 8 | stubRequest = {
|
9 | 9 | method: 'DELETE',
|
10 |
| - headers: {}, |
11 |
| - end: function () { |
12 |
| - // Simulate Stream.end() method when call |
13 |
| - endCalled = true; |
14 |
| - } |
| 10 | + headers: {} |
15 | 11 | },
|
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); |
17 | 19 | expect(returnValue).to.be(true);
|
18 |
| - expect(endCalled).to.be(true); |
| 20 | + expect(destroyCalled).to.be(true); |
19 | 21 | })
|
20 | 22 |
|
21 | 23 | it('should drop connections when no upgrade header', function () {
|
22 |
| - var endCalled = false, |
| 24 | + var destroyCalled = false, |
23 | 25 | stubRequest = {
|
24 | 26 | method: 'GET',
|
25 |
| - headers: {}, |
26 |
| - end: function () { |
27 |
| - // Simulate Stream.end() method when call |
28 |
| - endCalled = true; |
29 |
| - } |
| 27 | + headers: {} |
30 | 28 | },
|
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); |
32 | 36 | expect(returnValue).to.be(true);
|
33 |
| - expect(endCalled).to.be(true); |
| 37 | + expect(destroyCalled).to.be(true); |
34 | 38 | })
|
35 | 39 |
|
36 | 40 | it('should drop connections when upgrade header is different of `websocket`', function () {
|
37 |
| - var endCalled = false, |
| 41 | + var destroyCalled = false, |
38 | 42 | stubRequest = {
|
39 | 43 | method: 'GET',
|
40 | 44 | headers: {
|
41 | 45 | upgrade: 'anotherprotocol'
|
42 |
| - }, |
43 |
| - end: function () { |
44 |
| - // Simulate Stream.end() method when call |
45 |
| - endCalled = true; |
46 | 46 | }
|
47 | 47 | },
|
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); |
49 | 55 | expect(returnValue).to.be(true);
|
50 |
| - expect(endCalled).to.be(true); |
| 56 | + expect(destroyCalled).to.be(true); |
51 | 57 | })
|
52 | 58 |
|
53 | 59 | it('should return nothing when all is ok', function () {
|
54 |
| - var endCalled = false, |
| 60 | + var destroyCalled = false, |
55 | 61 | stubRequest = {
|
56 | 62 | method: 'GET',
|
57 | 63 | headers: {
|
58 | 64 | upgrade: 'websocket'
|
59 |
| - }, |
60 |
| - end: function () { |
61 |
| - // Simulate Stream.end() method when call |
62 |
| - endCalled = true; |
63 | 65 | }
|
64 | 66 | },
|
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); |
66 | 74 | expect(returnValue).to.be(undefined);
|
67 |
| - expect(endCalled).to.be(false); |
| 75 | + expect(destroyCalled).to.be(false); |
68 | 76 | })
|
69 | 77 | });
|
70 | 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 | + |
71 | 108 | 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 | + }); |
79 | 127 |
|
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 | + }); |
86 | 144 | });
|
87 | 145 | });
|
0 commit comments