1 | 'use strict'
|
2 |
|
3 |
|
4 |
|
5 | const t = require('tap')
|
6 | const test = t.test
|
7 | const sget = require('simple-get').concat
|
8 | const Fastify = require('..')
|
9 |
|
10 | test('register', t => {
|
11 | t.plan(17)
|
12 |
|
13 | const fastify = Fastify()
|
14 |
|
15 | fastify.register(function (instance, opts, done) {
|
16 | t.notEqual(instance, fastify)
|
17 | t.ok(fastify.isPrototypeOf(instance))
|
18 |
|
19 | t.is(typeof opts, 'object')
|
20 | t.is(typeof done, 'function')
|
21 |
|
22 | instance.get('/first', function (req, reply) {
|
23 | reply.send({ hello: 'world' })
|
24 | })
|
25 | done()
|
26 | })
|
27 |
|
28 | fastify.register(function (instance, opts, done) {
|
29 | t.notEqual(instance, fastify)
|
30 | t.ok(fastify.isPrototypeOf(instance))
|
31 |
|
32 | t.is(typeof opts, 'object')
|
33 | t.is(typeof done, 'function')
|
34 |
|
35 | instance.get('/second', function (req, reply) {
|
36 | reply.send({ hello: 'world' })
|
37 | })
|
38 | done()
|
39 | })
|
40 |
|
41 | fastify.listen(0, err => {
|
42 | t.error(err)
|
43 | fastify.server.unref()
|
44 |
|
45 | makeRequest('first')
|
46 | makeRequest('second')
|
47 | })
|
48 |
|
49 | function makeRequest (path) {
|
50 | sget({
|
51 | method: 'GET',
|
52 | url: 'http://localhost:' + fastify.server.address().port + '/' + path
|
53 | }, (err, response, body) => {
|
54 | t.error(err)
|
55 | t.strictEqual(response.statusCode, 200)
|
56 | t.strictEqual(response.headers['content-length'], '' + body.length)
|
57 | t.deepEqual(JSON.parse(body), { hello: 'world' })
|
58 | })
|
59 | }
|
60 | })
|
61 |
|
62 | test('internal route declaration should pass the error generated by the register to the next handler / 1', t => {
|
63 | t.plan(1)
|
64 | const fastify = Fastify()
|
65 |
|
66 | fastify.register((instance, opts, next) => {
|
67 | next(new Error('kaboom'))
|
68 | })
|
69 |
|
70 | fastify.get('/', (req, reply) => {
|
71 | reply.send({ hello: 'world' })
|
72 | })
|
73 |
|
74 | fastify.listen(0, err => {
|
75 | fastify.close()
|
76 | t.is(err.message, 'kaboom')
|
77 | })
|
78 | })
|
79 |
|
80 | test('internal route declaration should pass the error generated by the register to the next handler / 2', t => {
|
81 | t.plan(2)
|
82 | const fastify = Fastify()
|
83 |
|
84 | fastify.register((instance, opts, next) => {
|
85 | next(new Error('kaboom'))
|
86 | })
|
87 |
|
88 | fastify.get('/', (req, reply) => {
|
89 | reply.send({ hello: 'world' })
|
90 | })
|
91 |
|
92 | fastify.after(err => {
|
93 | t.is(err.message, 'kaboom')
|
94 | })
|
95 |
|
96 | fastify.listen(0, err => {
|
97 | fastify.close()
|
98 | t.error(err)
|
99 | })
|
100 | })
|