strong-tie/inbound-calls
0
1'use strict'2 3const t = require('tap')4const test = t.test5const Fastify = require('../fastify')6const sget = require('simple-get').concat7const fp = require('fastify-plugin')8 9test('if a plugin raises an error and there is not a callback to handle it, the server must not start', t => {10 t.plan(2)11 const fastify = Fastify()12 13 fastify.register((instance, opts, done) => {14 done(new Error('err'))15 })16 17 fastify.listen({ port: 0 }, err => {18 t.ok(err instanceof Error)19 t.equal(err.message, 'err')20 })21})22 23test('add hooks after route declaration', t => {24 t.plan(3)25 const fastify = Fastify()26 27 function plugin (instance, opts, done) {28 instance.decorateRequest('check', null)29 instance.addHook('onRequest', (req, reply, done) => {30 req.check = {}31 done()32 })33 setImmediate(done)34 }35 fastify.register(fp(plugin))36 37 fastify.register((instance, options, done) => {38 instance.addHook('preHandler', function b (req, res, done) {39 req.check.hook2 = true40 done()41 })42 43 instance.get('/', (req, reply) => {44 reply.send(req.check)45 })46 47 instance.addHook('preHandler', function c (req, res, done) {48 req.check.hook3 = true49 done()50 })51 52 done()53 })54 55 fastify.addHook('preHandler', function a (req, res, done) {56 req.check.hook1 = true57 done()58 })59 60 fastify.listen({ port: 0 }, err => {61 t.error(err)62 63 sget({64 method: 'GET',65 url: 'http://localhost:' + fastify.server.address().port66 }, (err, response, body) => {67 t.error(err)68 t.same(JSON.parse(body), { hook1: true, hook2: true, hook3: true })69 fastify.close()70 })71 })72})73 74test('nested plugins', t => {75 t.plan(5)76 77 const fastify = Fastify()78 79 t.teardown(fastify.close.bind(fastify))80 81 fastify.register(function (fastify, opts, done) {82 fastify.register((fastify, opts, done) => {83 fastify.get('/', function (req, reply) {84 reply.send('I am child 1')85 })86 done()87 }, { prefix: '/child1' })88 89 fastify.register((fastify, opts, done) => {90 fastify.get('/', function (req, reply) {91 reply.send('I am child 2')92 })93 done()94 }, { prefix: '/child2' })95 96 done()97 }, { prefix: '/parent' })98 99 fastify.listen({ port: 0 }, err => {100 t.error(err)101 102 sget({103 method: 'GET',104 url: 'http://localhost:' + fastify.server.address().port + '/parent/child1'105 }, (err, response, body) => {106 t.error(err)107 t.same(body.toString(), 'I am child 1')108 })109 110 sget({111 method: 'GET',112 url: 'http://localhost:' + fastify.server.address().port + '/parent/child2'113 }, (err, response, body) => {114 t.error(err)115 t.same(body.toString(), 'I am child 2')116 })117 })118})119 120test('nested plugins awaited', t => {121 t.plan(5)122 123 const fastify = Fastify()124 125 t.teardown(fastify.close.bind(fastify))126 127 fastify.register(async function wrap (fastify, opts) {128 await fastify.register(async function child1 (fastify, opts) {129 fastify.get('/', function (req, reply) {130 reply.send('I am child 1')131 })132 }, { prefix: '/child1' })133 134 await fastify.register(async function child2 (fastify, opts) {135 fastify.get('/', function (req, reply) {136 reply.send('I am child 2')137 })138 }, { prefix: '/child2' })139 }, { prefix: '/parent' })140 141 fastify.listen({ port: 0 }, err => {142 t.error(err)143 144 sget({145 method: 'GET',146 url: 'http://localhost:' + fastify.server.address().port + '/parent/child1'147 }, (err, response, body) => {148 t.error(err)149 t.same(body.toString(), 'I am child 1')150 })151 152 sget({153 method: 'GET',154 url: 'http://localhost:' + fastify.server.address().port + '/parent/child2'155 }, (err, response, body) => {156 t.error(err)157 t.same(body.toString(), 'I am child 2')158 })159 })160})161 162test('plugin metadata - decorators', t => {163 t.plan(1)164 const fastify = Fastify()165 166 fastify.decorate('plugin1', true)167 fastify.decorateReply('plugin1', true)168 fastify.decorateRequest('plugin1', true)169 170 plugin[Symbol.for('skip-override')] = true171 plugin[Symbol.for('plugin-meta')] = {172 decorators: {173 fastify: ['plugin1'],174 reply: ['plugin1'],175 request: ['plugin1']176 }177 }178 179 fastify.register(plugin)180 181 fastify.ready(() => {182 t.ok(fastify.plugin)183 })184 185 function plugin (instance, opts, done) {186 instance.decorate('plugin', true)187 done()188 }189})190 191test('plugin metadata - decorators - should throw', t => {192 t.plan(1)193 const fastify = Fastify()194 195 fastify.decorate('plugin1', true)196 fastify.decorateReply('plugin1', true)197 198 plugin[Symbol.for('skip-override')] = true199 plugin[Symbol.for('plugin-meta')] = {200 decorators: {201 fastify: ['plugin1'],202 reply: ['plugin1'],203 request: ['plugin1']204 }205 }206 207 fastify.register(plugin)208 fastify.ready((err) => {209 t.equal(err.message, "The decorator 'plugin1' is not present in Request")210 })211 212 function plugin (instance, opts, done) {213 instance.decorate('plugin', true)214 done()215 }216})217 218test('plugin metadata - decorators - should throw with plugin name', t => {219 t.plan(1)220 const fastify = Fastify()221 222 fastify.decorate('plugin1', true)223 fastify.decorateReply('plugin1', true)224 225 plugin[Symbol.for('skip-override')] = true226 plugin[Symbol.for('plugin-meta')] = {227 name: 'the-plugin',228 decorators: {229 fastify: ['plugin1'],230 reply: ['plugin1'],231 request: ['plugin1']232 }233 }234 235 fastify.register(plugin)236 fastify.ready((err) => {237 t.equal(err.message, "The decorator 'plugin1' required by 'the-plugin' is not present in Request")238 })239 240 function plugin (instance, opts, done) {241 instance.decorate('plugin', true)242 done()243 }244})245 246test('plugin metadata - dependencies', t => {247 t.plan(1)248 const fastify = Fastify()249 250 dependency[Symbol.for('skip-override')] = true251 dependency[Symbol.for('plugin-meta')] = {252 name: 'plugin'253 }254 255 plugin[Symbol.for('skip-override')] = true256 plugin[Symbol.for('plugin-meta')] = {257 dependencies: ['plugin']258 }259 260 fastify.register(dependency)261 fastify.register(plugin)262 263 fastify.ready(() => {264 t.pass('everything right')265 })266 267 function dependency (instance, opts, done) {268 done()269 }270 271 function plugin (instance, opts, done) {272 done()273 }274})275 276test('plugin metadata - dependencies (nested)', t => {277 t.plan(1)278 const fastify = Fastify()279 280 dependency[Symbol.for('skip-override')] = true281 dependency[Symbol.for('plugin-meta')] = {282 name: 'plugin'283 }284 285 nested[Symbol.for('skip-override')] = true286 nested[Symbol.for('plugin-meta')] = {287 dependencies: ['plugin']288 }289 290 fastify.register(dependency)291 fastify.register(plugin)292 293 fastify.ready(() => {294 t.pass('everything right')295 })296 297 function dependency (instance, opts, done) {298 done()299 }300 301 function plugin (instance, opts, done) {302 instance.register(nested)303 done()304 }305 306 function nested (instance, opts, done) {307 done()308 }309})310 