strong-tie/inbound-calls
0
1'use strict'2 3const t = require('tap')4const test = t.test5const sget = require('simple-get').concat6const Fastify = require('..')7 8test('register', t => {9 t.plan(17)10 11 const fastify = Fastify()12 13 fastify.register(function (instance, opts, done) {14 t.not(instance, fastify)15 t.ok(Object.prototype.isPrototypeOf.call(fastify, instance))16 17 t.equal(typeof opts, 'object')18 t.equal(typeof done, 'function')19 20 instance.get('/first', function (req, reply) {21 reply.send({ hello: 'world' })22 })23 done()24 })25 26 fastify.register(function (instance, opts, done) {27 t.not(instance, fastify)28 t.ok(Object.prototype.isPrototypeOf.call(fastify, instance))29 30 t.equal(typeof opts, 'object')31 t.equal(typeof done, 'function')32 33 instance.get('/second', function (req, reply) {34 reply.send({ hello: 'world' })35 })36 done()37 })38 39 fastify.listen({ port: 0 }, err => {40 t.error(err)41 t.teardown(() => { fastify.close() })42 43 makeRequest('first')44 makeRequest('second')45 })46 47 function makeRequest (path) {48 sget({49 method: 'GET',50 url: 'http://localhost:' + fastify.server.address().port + '/' + path51 }, (err, response, body) => {52 t.error(err)53 t.equal(response.statusCode, 200)54 t.equal(response.headers['content-length'], '' + body.length)55 t.same(JSON.parse(body), { hello: 'world' })56 })57 }58})59 60test('internal route declaration should pass the error generated by the register to the done handler / 1', t => {61 t.plan(1)62 const fastify = Fastify()63 64 fastify.register((instance, opts, done) => {65 done(new Error('kaboom'))66 })67 68 fastify.get('/', (req, reply) => {69 reply.send({ hello: 'world' })70 })71 72 fastify.listen({ port: 0 }, err => {73 fastify.close()74 t.equal(err.message, 'kaboom')75 })76})77 78test('internal route declaration should pass the error generated by the register to the done handler / 2', t => {79 t.plan(2)80 const fastify = Fastify()81 82 fastify.register((instance, opts, done) => {83 done(new Error('kaboom'))84 })85 86 fastify.get('/', (req, reply) => {87 reply.send({ hello: 'world' })88 })89 90 fastify.after(err => {91 t.equal(err.message, 'kaboom')92 })93 94 fastify.listen({ port: 0 }, err => {95 fastify.close()96 t.error(err)97 })98})99 100test('awaitable register and after', async t => {101 const fastify = Fastify()102 let first = false103 let second = false104 let third = false105 106 await fastify.register(async (instance, opts) => {107 first = true108 })109 110 t.equal(first, true)111 112 fastify.register(async (instance, opts) => {113 second = true114 })115 116 await fastify.after()117 t.equal(second, true)118 119 fastify.register(async (instance, opts) => {120 third = true121 })122 123 await fastify.ready()124 t.equal(third, true)125})126 127function thenableRejects (t, promise, error) {128 return t.rejects(async () => { await promise }, error)129}130 131test('awaitable register error handling', async t => {132 const fastify = Fastify()133 134 const e = new Error('kaboom')135 136 await thenableRejects(t, fastify.register(async (instance, opts) => {137 throw e138 }), e)139 140 fastify.register(async (instance, opts) => {141 t.fail('should not be executed')142 })143 144 await t.rejects(fastify.after(), e)145 146 fastify.register(async (instance, opts) => {147 t.fail('should not be executed')148 })149 150 await thenableRejects(t, fastify.ready(), e)151})152 153test('awaitable after error handling', async t => {154 const fastify = Fastify()155 156 const e = new Error('kaboom')157 158 fastify.register(async (instance, opts) => {159 throw e160 })161 162 fastify.register(async (instance, opts) => {163 t.fail('should not be executed')164 })165 166 await t.rejects(fastify.after(), e)167 168 fastify.register(async (instance, opts) => {169 t.fail('should not be executed')170 })171 172 await t.rejects(fastify.ready())173})174 175test('chainable register', async t => {176 t.plan(3)177 178 const fastify = Fastify()179 180 fastify.register(async () => {181 t.pass('first loaded')182 }).register(async () => {183 t.pass('second loaded')184 }).register(async () => {185 t.pass('third loaded')186 })187 188 await fastify.ready()189})190 