1 | 'use strict';
|
2 |
|
3 |
|
4 | const Code = require('code');
|
5 | const Glue = require('..');
|
6 | const Lab = require('lab');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const internals = {};
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const lab = exports.lab = Lab.script();
|
17 | const describe = lab.describe;
|
18 | const it = lab.it;
|
19 | const expect = Code.expect;
|
20 |
|
21 |
|
22 | describe('compose()', () => {
|
23 |
|
24 | it('composes a server with an empty manifest', (done) => {
|
25 |
|
26 | const manifest = {};
|
27 |
|
28 | Glue.compose(manifest, (err, server) => {
|
29 |
|
30 | expect(err).to.not.exist();
|
31 | expect(server.connections).length(1);
|
32 | done();
|
33 | });
|
34 | });
|
35 |
|
36 | it('returns a promise if no options and no callback is provided', (done) => {
|
37 |
|
38 | const manifest = {};
|
39 |
|
40 | Glue.compose(manifest).then((server) => {
|
41 |
|
42 | expect(server.connections).length(1);
|
43 | done();
|
44 | });
|
45 | });
|
46 |
|
47 | it('returns a promise if no callback is provided', (done) => {
|
48 |
|
49 | const manifest = {};
|
50 | const options = {};
|
51 |
|
52 | Glue.compose(manifest, options).then((server) => {
|
53 |
|
54 | expect(server.connections).length(1);
|
55 | done();
|
56 | });
|
57 | });
|
58 |
|
59 | it('rejects a promise if an error is thrown', (done) => {
|
60 |
|
61 | const manifest = {
|
62 | registrations: [
|
63 | {
|
64 | plugin: './invalid-plugin'
|
65 | }
|
66 | ]
|
67 | };
|
68 |
|
69 | Glue.compose(manifest).catch((err) => {
|
70 |
|
71 | expect(err).to.exist();
|
72 | expect(err.code).to.equal('MODULE_NOT_FOUND');
|
73 | done();
|
74 | });
|
75 | });
|
76 |
|
77 | it('rejects a promise if an error is returned', (done) => {
|
78 |
|
79 | const manifest = {};
|
80 | const options = {
|
81 | preRegister: function (server, callback) {
|
82 |
|
83 | callback({ error: 'failed' });
|
84 | }
|
85 | };
|
86 |
|
87 | Glue.compose(manifest, options).then(null, (err) => {
|
88 |
|
89 | expect(err).to.exist();
|
90 | done();
|
91 | });
|
92 | });
|
93 |
|
94 | it('composes a server with server.cache as a string', (done) => {
|
95 |
|
96 | const manifest = {
|
97 | server: {
|
98 | cache: '../node_modules/catbox-memory'
|
99 | }
|
100 | };
|
101 |
|
102 | Glue.compose(manifest, (err, server) => {
|
103 |
|
104 | expect(err).to.not.exist();
|
105 | done();
|
106 | });
|
107 | });
|
108 |
|
109 | it('composes a server with server.cache as an array', (done) => {
|
110 |
|
111 | const manifest = {
|
112 | server: {
|
113 | cache: ['../node_modules/catbox-memory']
|
114 | }
|
115 | };
|
116 |
|
117 | Glue.compose(manifest, (err, server) => {
|
118 |
|
119 | expect(err).to.not.exist();
|
120 | done();
|
121 | });
|
122 | });
|
123 |
|
124 | it('composes a server with server.cache.engine as a string', (done) => {
|
125 |
|
126 | const manifest = {
|
127 | server: {
|
128 | cache: {
|
129 | engine: '../node_modules/catbox-memory'
|
130 | }
|
131 | }
|
132 | };
|
133 |
|
134 | Glue.compose(manifest, (err, server) => {
|
135 |
|
136 | expect(err).to.not.exist();
|
137 | done();
|
138 | });
|
139 | });
|
140 |
|
141 | it('composes a server with server.cache.engine as a function', (done) => {
|
142 |
|
143 | const manifest = {
|
144 | server: {
|
145 | cache: [{
|
146 | engine: require('catbox-memory')
|
147 | }]
|
148 | }
|
149 | };
|
150 |
|
151 | Glue.compose(manifest, (err, server) => {
|
152 |
|
153 | expect(err).to.not.exist();
|
154 | done();
|
155 | });
|
156 | });
|
157 |
|
158 | it('composes a server with server.cache.engine resolved using options.relativeTo', (done) => {
|
159 |
|
160 | const manifest = {
|
161 | server: {
|
162 | cache: '../../node_modules/catbox-memory'
|
163 | }
|
164 | };
|
165 |
|
166 | Glue.compose(manifest, { relativeTo: __dirname + '/plugins' }, (err, server) => {
|
167 |
|
168 | expect(err).to.not.exist();
|
169 | done();
|
170 | });
|
171 | });
|
172 |
|
173 | describe('composes a server\'s connections', () => {
|
174 |
|
175 | it('has no entries', (done) => {
|
176 |
|
177 | const manifest = {
|
178 | connections: []
|
179 | };
|
180 |
|
181 | Glue.compose(manifest, (err, server) => {
|
182 |
|
183 | expect(err).to.not.exist();
|
184 | expect(server.connections).length(1);
|
185 | done();
|
186 | });
|
187 | });
|
188 |
|
189 | it('has a single entry', (done) => {
|
190 |
|
191 | const manifest = {
|
192 | connections: [
|
193 | { labels: 'a' }
|
194 | ]
|
195 | };
|
196 |
|
197 | Glue.compose(manifest, (err, server) => {
|
198 |
|
199 | expect(err).to.not.exist();
|
200 | expect(server.connections).length(1);
|
201 | done();
|
202 | });
|
203 | });
|
204 |
|
205 | it('has multiple entries', (done) => {
|
206 |
|
207 | const manifest = {
|
208 | connections: [
|
209 | { labels: 'a' },
|
210 | { labels: 'b' }
|
211 | ]
|
212 | };
|
213 |
|
214 | Glue.compose(manifest, (err, server) => {
|
215 |
|
216 | expect(err).to.not.exist();
|
217 | expect(server.connections).length(2);
|
218 | done();
|
219 | });
|
220 | });
|
221 | });
|
222 |
|
223 | describe('composes a server\'s registrations', () => {
|
224 |
|
225 | it('has no registrations', (done) => {
|
226 |
|
227 | const manifest = {
|
228 | registrations: []
|
229 | };
|
230 |
|
231 | Glue.compose(manifest, (err, server) => {
|
232 |
|
233 | expect(err).to.not.exist();
|
234 | expect(server.plugins).length(0);
|
235 | done();
|
236 | });
|
237 | });
|
238 |
|
239 | it('has a registration with no configuration', (done) => {
|
240 |
|
241 | const manifest = {
|
242 | registrations: [
|
243 | {
|
244 | plugin: '../test/plugins/helloworld.js'
|
245 | }
|
246 | ]
|
247 | };
|
248 |
|
249 | Glue.compose(manifest, (err, server) => {
|
250 |
|
251 | expect(err).to.not.exist();
|
252 | expect(server.plugins.helloworld).to.exist();
|
253 | expect(server.plugins.helloworld.hello).to.equal('world');
|
254 | done();
|
255 | });
|
256 | });
|
257 |
|
258 | it('passes through unknown plugin object fields', (done) => {
|
259 |
|
260 | const manifest = {
|
261 | registrations: [
|
262 | {
|
263 | plugin: {
|
264 | register: '../test/plugins/helloworld.js'
|
265 | }
|
266 | }
|
267 | ]
|
268 | };
|
269 |
|
270 | Glue.compose(manifest, (err, server) => {
|
271 |
|
272 | expect(err).to.not.exist();
|
273 | expect(server.plugins.helloworld).to.exist();
|
274 | expect(server.plugins.helloworld.hello).to.equal('world');
|
275 | done();
|
276 | });
|
277 | });
|
278 |
|
279 | it('has a registration with no plugin options and no register options', (done) => {
|
280 |
|
281 | const manifest = {
|
282 | registrations: [
|
283 | {
|
284 | plugin: {
|
285 | register: '../test/plugins/helloworld.js'
|
286 | }
|
287 | }
|
288 | ]
|
289 | };
|
290 |
|
291 | Glue.compose(manifest, (err, server) => {
|
292 |
|
293 | expect(err).to.not.exist();
|
294 | expect(server.plugins.helloworld).to.exist();
|
295 | expect(server.plugins.helloworld.hello).to.equal('world');
|
296 | done();
|
297 | });
|
298 | });
|
299 |
|
300 | it('has a registration with plugin options and no register options', (done) => {
|
301 |
|
302 | const manifest = {
|
303 | registrations: [
|
304 | {
|
305 | plugin: {
|
306 | register: '../test/plugins/helloworld.js',
|
307 | options: { who: 'earth' }
|
308 | }
|
309 | }
|
310 | ]
|
311 | };
|
312 |
|
313 | Glue.compose(manifest, (err, server) => {
|
314 |
|
315 | expect(err).to.not.exist();
|
316 | expect(server.plugins.helloworld).to.exist();
|
317 | expect(server.plugins.helloworld.hello).to.equal('earth');
|
318 | done();
|
319 | });
|
320 | });
|
321 |
|
322 | it('has a registration with register options and no plugin options', (done) => {
|
323 |
|
324 | const manifest = {
|
325 | registrations: [
|
326 | {
|
327 | plugin: '../test/plugins/route.js',
|
328 | options: {
|
329 | routes: { prefix: '/test/' }
|
330 | }
|
331 | }
|
332 | ]
|
333 | };
|
334 |
|
335 | Glue.compose(manifest, (err, server) => {
|
336 |
|
337 | expect(err).to.not.exist();
|
338 | server.inject('/test/plugin', (response) => {
|
339 |
|
340 | expect(response.statusCode).to.equal(200);
|
341 | done();
|
342 | });
|
343 | });
|
344 | });
|
345 |
|
346 | it('has registrations having the same plugin loaded multiple times', (done) => {
|
347 |
|
348 | const manifest = {
|
349 | connections: [
|
350 | { labels: 'a' },
|
351 | { labels: 'b' }
|
352 | ],
|
353 | registrations: [
|
354 | {
|
355 | plugin: '../test/plugins/route.js',
|
356 | options: {
|
357 | select: 'a',
|
358 | routes: { prefix: '/a/' }
|
359 | }
|
360 | },
|
361 | {
|
362 | plugin: '../test/plugins/route.js',
|
363 | options: {
|
364 | select: 'b',
|
365 | routes: { prefix: '/b/' }
|
366 | }
|
367 | }
|
368 | ]
|
369 | };
|
370 |
|
371 | Glue.compose(manifest, (err, server) => {
|
372 |
|
373 | expect(err).to.not.exist();
|
374 | server.select('a').inject('/a/plugin', (responseA) => {
|
375 |
|
376 | expect(responseA.statusCode).to.equal(200);
|
377 | server.select('b').inject('/b/plugin', (responseB) => {
|
378 |
|
379 | expect(responseB.statusCode).to.equal(200);
|
380 | done();
|
381 | });
|
382 | });
|
383 | });
|
384 | });
|
385 |
|
386 | it('has a registration with the plugin resolved using options.relativeTo', (done) => {
|
387 |
|
388 | const manifest = {
|
389 | registrations: [
|
390 | {
|
391 | plugin: './helloworld.js'
|
392 | }
|
393 | ]
|
394 | };
|
395 |
|
396 | Glue.compose(manifest, { relativeTo: __dirname + '/plugins' }, (err, server) => {
|
397 |
|
398 | expect(err).to.not.exist();
|
399 | expect(server.plugins.helloworld.hello).to.equal('world');
|
400 | done();
|
401 | });
|
402 | });
|
403 | });
|
404 |
|
405 | it('composes a server with a preConnections handler', (done) => {
|
406 |
|
407 | const manifest = {};
|
408 | const options = {
|
409 | preConnections: function (server, callback) {
|
410 |
|
411 | callback();
|
412 | }
|
413 | };
|
414 |
|
415 | Glue.compose(manifest, options, (err, server) => {
|
416 |
|
417 | expect(err).to.not.exist();
|
418 | done();
|
419 | });
|
420 | });
|
421 |
|
422 | it('composes a server with a preRegister handler', (done) => {
|
423 |
|
424 | const manifest = {};
|
425 | const options = {
|
426 | preRegister: function (server, callback) {
|
427 |
|
428 | callback();
|
429 | }
|
430 | };
|
431 |
|
432 | Glue.compose(manifest, options, (err, server) => {
|
433 |
|
434 | expect(err).to.not.exist();
|
435 | done();
|
436 | });
|
437 | });
|
438 |
|
439 | it('errors on failed pre handler', (done) => {
|
440 |
|
441 | const manifest = {};
|
442 | const options = {
|
443 | preRegister: function (server, callback) {
|
444 |
|
445 | callback({ error: 'failed' });
|
446 | }
|
447 | };
|
448 |
|
449 | Glue.compose(manifest, options, (err, server) => {
|
450 |
|
451 | expect(err).to.exist();
|
452 | done();
|
453 | });
|
454 | });
|
455 |
|
456 | it('throws on bogus options.realativeTo path (server.cache)', (done) => {
|
457 |
|
458 | const manifest = {
|
459 | server: {
|
460 | cache: './catbox-memory'
|
461 | }
|
462 | };
|
463 |
|
464 | expect(() => {
|
465 |
|
466 | Glue.compose(manifest, { relativeTo: __dirname + '/badpath' }, () => { });
|
467 | }).to.throw(/Cannot find module/);
|
468 | done();
|
469 | });
|
470 |
|
471 | it('throws on bogus options.realativeTo path (plugins)', (done) => {
|
472 |
|
473 | const manifest = {
|
474 | registrations: [
|
475 | {
|
476 | plugin: './helloworld.js'
|
477 | }
|
478 | ]
|
479 | };
|
480 |
|
481 | expect(() => {
|
482 |
|
483 | Glue.compose(manifest, { relativeTo: __dirname + '/badpath' }, () => { });
|
484 | }).to.throw(/Cannot find module/);
|
485 | done();
|
486 | });
|
487 |
|
488 | it('throws on options not an object', (done) => {
|
489 |
|
490 | const manifest = {};
|
491 |
|
492 | expect(() => {
|
493 |
|
494 | Glue.compose(manifest, 'hello', () => { });
|
495 | }).to.throw(/Invalid options/);
|
496 | done();
|
497 | });
|
498 |
|
499 | it('throws on invalid manifest (not an object)', (done) => {
|
500 |
|
501 | const manifest = 'hello';
|
502 |
|
503 | expect(() => {
|
504 |
|
505 | Glue.compose(manifest, () => { });
|
506 | }).to.throw(/Invalid manifest/);
|
507 | done();
|
508 | });
|
509 |
|
510 | it('throws on invalid manifest (server not an object)', (done) => {
|
511 |
|
512 | const manifest = {
|
513 | server: 'hello'
|
514 | };
|
515 |
|
516 | expect(() => {
|
517 |
|
518 | Glue.compose(manifest, () => { });
|
519 | }).to.throw(/Invalid manifest/);
|
520 | done();
|
521 | });
|
522 |
|
523 | it('throws on invalid manifest (connections not an array)', (done) => {
|
524 |
|
525 | const manifest = {
|
526 | connections: 'hello'
|
527 | };
|
528 |
|
529 | expect(() => {
|
530 |
|
531 | Glue.compose(manifest, () => { });
|
532 | }).to.throw(/Invalid manifest/);
|
533 | done();
|
534 | });
|
535 |
|
536 | it('throws on invalid manifest (registrations not an array)', (done) => {
|
537 |
|
538 | const manifest = {
|
539 | registrations: 'hello'
|
540 | };
|
541 |
|
542 | expect(() => {
|
543 |
|
544 | Glue.compose(manifest, () => { });
|
545 | }).to.throw(/Invalid manifest/);
|
546 | done();
|
547 | });
|
548 | });
|