1 |
|
2 |
|
3 | var Code = require('code');
|
4 | var Hapi = require('hapi');
|
5 | var Lab = require('lab');
|
6 | var Ws = require('ws');
|
7 | var Tv = require('../');
|
8 | var Os = require('os');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | var internals = {};
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | var lab = exports.lab = Lab.script();
|
19 | var describe = lab.describe;
|
20 | var it = lab.it;
|
21 | var expect = Code.expect;
|
22 |
|
23 | internals.waitForSocketMessages = function(fn) {
|
24 | setTimeout(fn, 50);
|
25 | };
|
26 |
|
27 |
|
28 | it('reports a request event', function (done) {
|
29 |
|
30 | var server = new Hapi.Server();
|
31 | server.connection();
|
32 |
|
33 | server.route({
|
34 | method: 'GET',
|
35 | path: '/',
|
36 | handler: function (request, reply) {
|
37 |
|
38 | return reply('1');
|
39 | }
|
40 | });
|
41 |
|
42 | server.register({ register: Tv, options: { port: 0 } }, function (err) {
|
43 |
|
44 | expect(err).to.not.exist();
|
45 |
|
46 | server.inject('/debug/console', function (res) {
|
47 |
|
48 | expect(res.statusCode).to.equal(200);
|
49 | expect(res.result).to.contain('Debug Console');
|
50 |
|
51 | var host = res.result.match(/var host = '([^']+)'/)[1];
|
52 | var port = res.result.match(/var port = (\d+)/)[1];
|
53 | var ws = new Ws('ws://' + host + ':' + port);
|
54 |
|
55 | ws.once('open', function () {
|
56 |
|
57 | ws.send('subscribe:*');
|
58 |
|
59 | internals.waitForSocketMessages(function () {
|
60 |
|
61 | server.inject('/?debug=123', function (res) {
|
62 |
|
63 | expect(res.result).to.equal('1');
|
64 | });
|
65 | });
|
66 | });
|
67 |
|
68 | ws.once('message', function (data, flags) {
|
69 |
|
70 | expect(JSON.parse(data).data.agent).to.equal('shot');
|
71 | done();
|
72 | });
|
73 | });
|
74 | });
|
75 | });
|
76 |
|
77 | it('handles subscribe and unsubscribe', function(done) {
|
78 |
|
79 | var server = new Hapi.Server();
|
80 | server.connection();
|
81 |
|
82 | server.route({
|
83 | method: 'GET',
|
84 | path: '/',
|
85 | handler: function (request, reply) {
|
86 |
|
87 | return reply('1');
|
88 | }
|
89 | });
|
90 |
|
91 | server.register({ register: Tv, options: { port: 0 } }, function (err) {
|
92 |
|
93 | expect(err).to.not.exist();
|
94 |
|
95 | server.inject('/debug/console', function (res) {
|
96 |
|
97 | expect(res.statusCode).to.equal(200);
|
98 | expect(res.result).to.contain('Debug Console');
|
99 |
|
100 | var host = res.result.match(/var host = '([^']+)'/)[1];
|
101 | var port = res.result.match(/var port = (\d+)/)[1];
|
102 | var ws = new Ws('ws://' + host + ':' + port);
|
103 | var messageCount = 0;
|
104 |
|
105 | ws.once('open', function () {
|
106 |
|
107 | ws.send('subscribe:*');
|
108 |
|
109 | internals.waitForSocketMessages(function () {
|
110 |
|
111 | server.inject('/?debug=123', function() {
|
112 |
|
113 | internals.waitForSocketMessages(function() {
|
114 |
|
115 | var singleRequestMessageCount = messageCount;
|
116 | ws.send('unsubscribe:*');
|
117 |
|
118 | internals.waitForSocketMessages(function() {
|
119 |
|
120 | server.inject('/?debug=123', function() {
|
121 |
|
122 | internals.waitForSocketMessages(function() {
|
123 |
|
124 | expect(messageCount).to.equal(singleRequestMessageCount);
|
125 |
|
126 | done();
|
127 | });
|
128 | });
|
129 | });
|
130 | });
|
131 | });
|
132 | });
|
133 | });
|
134 |
|
135 | ws.on('message', function (data, flags) {
|
136 | ++messageCount;
|
137 | });
|
138 | });
|
139 | });
|
140 | });
|
141 |
|
142 | it('does not resubscribe for the same socket', function(done) {
|
143 |
|
144 | var server = new Hapi.Server();
|
145 | server.connection();
|
146 |
|
147 | server.route({
|
148 | method: 'GET',
|
149 | path: '/',
|
150 | handler: function (request, reply) {
|
151 |
|
152 | return reply('1');
|
153 | }
|
154 | });
|
155 |
|
156 | server.register({ register: Tv, options: { port: 0 } }, function (err) {
|
157 |
|
158 | expect(err).to.not.exist();
|
159 |
|
160 | server.inject('/debug/console', function (res) {
|
161 |
|
162 | expect(res.statusCode).to.equal(200);
|
163 | expect(res.result).to.contain('Debug Console');
|
164 |
|
165 | var host = res.result.match(/var host = '([^']+)'/)[1];
|
166 | var port = res.result.match(/var port = (\d+)/)[1];
|
167 | var ws = new Ws('ws://' + host + ':' + port);
|
168 | var messageCount = 0;
|
169 |
|
170 | ws.once('open', function () {
|
171 |
|
172 | ws.send('subscribe:*');
|
173 |
|
174 | internals.waitForSocketMessages(function () {
|
175 |
|
176 | server.inject('/?debug=123', function() {
|
177 |
|
178 | internals.waitForSocketMessages(function() {
|
179 |
|
180 | var singleRequestMessageCount = messageCount;
|
181 | ws.send('subscribe:*');
|
182 |
|
183 | internals.waitForSocketMessages(function() {
|
184 |
|
185 | server.inject('/?debug=123', function() {
|
186 |
|
187 | internals.waitForSocketMessages(function() {
|
188 |
|
189 | expect(messageCount).to.equal(singleRequestMessageCount * 2);
|
190 |
|
191 | done();
|
192 | });
|
193 | });
|
194 | });
|
195 | });
|
196 | });
|
197 | });
|
198 | });
|
199 |
|
200 | ws.on('message', function (data, flags) {
|
201 | ++messageCount;
|
202 | });
|
203 | });
|
204 | });
|
205 | });
|
206 |
|
207 | it('handles reconnects gracefully', function (done) {
|
208 |
|
209 | var server = new Hapi.Server();
|
210 | server.connection();
|
211 |
|
212 | server.route({
|
213 | method: 'GET',
|
214 | path: '/',
|
215 | handler: function (request, reply) {
|
216 |
|
217 | return reply('1');
|
218 | }
|
219 | });
|
220 |
|
221 | server.register({ register: Tv, options: { port: 0, host: 'localhost' }}, function (err) {
|
222 |
|
223 | expect(err).to.not.exist();
|
224 |
|
225 | server.inject('/debug/console', function (res) {
|
226 |
|
227 | expect(res.statusCode).to.equal(200);
|
228 | expect(res.result).to.contain('Debug Console');
|
229 |
|
230 | var host = res.result.match(/var host = '([^']+)'/)[1];
|
231 | var port = res.result.match(/var port = (\d+)/)[1];
|
232 | var ws1 = new Ws('ws://' + host + ':' + port);
|
233 |
|
234 | ws1.once('open', function () {
|
235 |
|
236 | ws1.send('subscribe:*');
|
237 | ws1.close();
|
238 | var ws2 = new Ws('ws://' + host + ':' + port);
|
239 |
|
240 | ws2.once('open', function () {
|
241 |
|
242 | ws2.send('subscribe:*');
|
243 | internals.waitForSocketMessages(function () {
|
244 |
|
245 | server.inject('/?debug=123', function (res) {
|
246 |
|
247 | expect(res.result).to.equal('1');
|
248 | });
|
249 | });
|
250 | });
|
251 |
|
252 |
|
253 | ws2.once('message', function (data, flags) {
|
254 |
|
255 | expect(JSON.parse(data).data.agent).to.equal('shot');
|
256 | done();
|
257 | });
|
258 | });
|
259 | });
|
260 | });
|
261 | });
|
262 |
|
263 | it('uses specified hostname', function (done) {
|
264 |
|
265 | var server = new Hapi.Server();
|
266 | server.connection();
|
267 |
|
268 | server.route({
|
269 | method: 'GET',
|
270 | path: '/',
|
271 | handler: function (request, reply) {
|
272 |
|
273 | return reply('1');
|
274 | }
|
275 | });
|
276 |
|
277 | server.register({register: Tv, options: { host: '127.0.0.1', port: 0 } }, function (err) {
|
278 |
|
279 | expect(err).to.not.exist();
|
280 |
|
281 | server.inject('/debug/console', function (res) {
|
282 |
|
283 | expect(res.statusCode).to.equal(200);
|
284 | expect(res.result).to.contain('Debug Console');
|
285 |
|
286 | var host = res.result.match(/var host = '([^']+)'/)[1];
|
287 | expect(host).to.equal('127.0.0.1');
|
288 | done();
|
289 |
|
290 | });
|
291 | });
|
292 | });
|
293 |
|
294 | it('uses specified public hostname', function (done) {
|
295 |
|
296 | var server = new Hapi.Server();
|
297 | server.connection();
|
298 |
|
299 | server.route({
|
300 | method: 'GET',
|
301 | path: '/',
|
302 | handler: function (request, reply) {
|
303 |
|
304 | return reply('1');
|
305 | }
|
306 | });
|
307 |
|
308 | server.register({ register: Tv, options: { port: 0, host: 'localhost', publicHost: '127.0.0.1' }}, function (err) {
|
309 |
|
310 | expect(err).to.not.exist();
|
311 |
|
312 | server.inject('/debug/console', function (res) {
|
313 |
|
314 | expect(res.statusCode).to.equal(200);
|
315 | expect(res.result).to.contain('Debug Console');
|
316 |
|
317 | var host = res.result.match(/var host = '([^']+)'/)[1];
|
318 | expect(host).to.equal('127.0.0.1');
|
319 | done();
|
320 |
|
321 | });
|
322 | });
|
323 | });
|
324 |
|
325 | it('defaults to os hostname if unspecified', function (done) {
|
326 |
|
327 | var server = new Hapi.Server();
|
328 | server.connection();
|
329 |
|
330 | server.route({
|
331 | method: 'GET',
|
332 | path: '/',
|
333 | handler: function (request, reply) {
|
334 |
|
335 | return reply('1');
|
336 | }
|
337 | });
|
338 |
|
339 | server.register({ register: Tv, options: { port: 0, host: 'localhost', publicHost: '0.0.0.0' }}, function (err) {
|
340 |
|
341 | expect(err).to.not.exist();
|
342 |
|
343 | server.inject('/debug/console', function (res) {
|
344 |
|
345 | expect(res.statusCode).to.equal(200);
|
346 | expect(res.result).to.contain('Debug Console');
|
347 |
|
348 | var host = res.result.match(/var host = '([^']+)'/)[1];
|
349 | expect(host).to.equal(Os.hostname());
|
350 | done();
|
351 |
|
352 | });
|
353 | });
|
354 | });
|