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('check dependencies - should not throw', t => {10 t.plan(12)11 const fastify = Fastify()12 13 fastify.register((instance, opts, done) => {14 instance.register(fp((i, o, n) => {15 i.decorate('test', () => {})16 t.ok(i.test)17 n()18 }))19 20 instance.register(fp((i, o, n) => {21 try {22 i.decorate('otherTest', () => {}, ['test'])23 t.ok(i.test)24 t.ok(i.otherTest)25 n()26 } catch (e) {27 t.fail()28 }29 }))30 31 instance.get('/', (req, reply) => {32 t.ok(instance.test)33 t.ok(instance.otherTest)34 reply.send({ hello: 'world' })35 })36 37 done()38 })39 40 fastify.ready(() => {41 t.notOk(fastify.test)42 t.notOk(fastify.otherTest)43 })44 45 fastify.listen({ port: 0 }, err => {46 t.error(err)47 t.teardown(() => { fastify.close() })48 49 sget({50 method: 'GET',51 url: 'http://localhost:' + fastify.server.address().port52 }, (err, response, body) => {53 t.error(err)54 t.equal(response.statusCode, 200)55 t.equal(response.headers['content-length'], '' + body.length)56 t.same(JSON.parse(body), { hello: 'world' })57 })58 })59})60 61test('check dependencies - should throw', t => {62 t.plan(12)63 const fastify = Fastify()64 65 fastify.register((instance, opts, done) => {66 instance.register(fp((i, o, n) => {67 try {68 i.decorate('otherTest', () => {}, ['test'])69 t.fail()70 } catch (e) {71 t.equal(e.code, 'FST_ERR_DEC_MISSING_DEPENDENCY')72 t.equal(e.message, 'The decorator is missing dependency \'test\'.')73 }74 n()75 }))76 77 instance.register(fp((i, o, n) => {78 i.decorate('test', () => {})79 t.ok(i.test)80 t.notOk(i.otherTest)81 n()82 }))83 84 instance.get('/', (req, reply) => {85 t.ok(instance.test)86 t.notOk(instance.otherTest)87 reply.send({ hello: 'world' })88 })89 90 done()91 })92 93 fastify.ready(() => {94 t.notOk(fastify.test)95 })96 97 fastify.listen({ port: 0 }, err => {98 t.error(err)99 t.teardown(() => { fastify.close() })100 101 sget({102 method: 'GET',103 url: 'http://localhost:' + fastify.server.address().port104 }, (err, response, body) => {105 t.error(err)106 t.equal(response.statusCode, 200)107 t.equal(response.headers['content-length'], '' + body.length)108 t.same(JSON.parse(body), { hello: 'world' })109 })110 })111})112 113test('set the plugin name based on the plugin displayName symbol', t => {114 t.plan(6)115 const fastify = Fastify()116 117 fastify.register(fp((fastify, opts, done) => {118 t.equal(fastify.pluginName, 'fastify -> plugin-A')119 fastify.register(fp((fastify, opts, done) => {120 t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB')121 done()122 }, { name: 'plugin-AB' }))123 fastify.register(fp((fastify, opts, done) => {124 t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')125 done()126 }, { name: 'plugin-AC' }))127 done()128 }, { name: 'plugin-A' }))129 130 fastify.register(fp((fastify, opts, done) => {131 t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC -> plugin-B')132 done()133 }, { name: 'plugin-B' }))134 135 t.equal(fastify.pluginName, 'fastify')136 137 fastify.listen({ port: 0 }, err => {138 t.error(err)139 fastify.close()140 })141})142 143test('plugin name will change when using no encapsulation', t => {144 t.plan(6)145 const fastify = Fastify()146 147 fastify.register(fp((fastify, opts, done) => {148 // store it in a different variable will hold the correct name149 const pluginName = fastify.pluginName150 fastify.register(fp((fastify, opts, done) => {151 t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB')152 done()153 }, { name: 'plugin-AB' }))154 fastify.register(fp((fastify, opts, done) => {155 t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')156 done()157 }, { name: 'plugin-AC' }))158 setImmediate(() => {159 // normally we would expect the name plugin-A160 // but we operate on the same instance in each plugin161 t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')162 t.equal(pluginName, 'fastify -> plugin-A')163 })164 done()165 }, { name: 'plugin-A' }))166 167 t.equal(fastify.pluginName, 'fastify')168 169 fastify.listen({ port: 0 }, err => {170 t.error(err)171 fastify.close()172 })173})174 175test('plugin name is undefined when accessing in no plugin context', t => {176 t.plan(2)177 const fastify = Fastify()178 179 t.equal(fastify.pluginName, 'fastify')180 181 fastify.listen({ port: 0 }, err => {182 t.error(err)183 fastify.close()184 })185})186 187test('set the plugin name based on the plugin function name', t => {188 t.plan(5)189 const fastify = Fastify()190 191 fastify.register(function myPluginA (fastify, opts, done) {192 t.equal(fastify.pluginName, 'myPluginA')193 fastify.register(function myPluginAB (fastify, opts, done) {194 t.equal(fastify.pluginName, 'myPluginAB')195 done()196 })197 setImmediate(() => {198 // exact name due to encapsulation199 t.equal(fastify.pluginName, 'myPluginA')200 })201 done()202 })203 204 fastify.register(function myPluginB (fastify, opts, done) {205 t.equal(fastify.pluginName, 'myPluginB')206 done()207 })208 209 fastify.listen({ port: 0 }, err => {210 t.error(err)211 fastify.close()212 })213})214 215test('approximate a plugin name when no meta data is available', t => {216 t.plan(7)217 const fastify = Fastify()218 219 fastify.register((fastify, opts, done) => {220 // A221 t.equal(fastify.pluginName.startsWith('(fastify, opts, done)'), true)222 t.equal(fastify.pluginName.includes('// A'), true)223 fastify.register((fastify, opts, done) => {224 // B225 t.equal(fastify.pluginName.startsWith('(fastify, opts, done)'), true)226 t.equal(fastify.pluginName.includes('// B'), true)227 done()228 })229 setImmediate(() => {230 t.equal(fastify.pluginName.startsWith('(fastify, opts, done)'), true)231 t.equal(fastify.pluginName.includes('// A'), true)232 })233 done()234 })235 236 fastify.listen({ port: 0 }, err => {237 t.error(err)238 fastify.close()239 })240})241 242test('approximate a plugin name also when fastify-plugin has no meta data', t => {243 t.plan(4)244 const fastify = Fastify()245 // plugin name is got from current file name246 const pluginName = /plugin\.2\.test/247 const pluginNameWithFunction = /plugin\.2\.test-auto-\d+ -> B/248 249 fastify.register(fp((fastify, opts, done) => {250 t.match(fastify.pluginName, pluginName)251 fastify.register(fp(function B (fastify, opts, done) {252 // function has name253 t.match(fastify.pluginName, pluginNameWithFunction)254 done()255 }))256 setImmediate(() => {257 t.match(fastify.pluginName, pluginNameWithFunction)258 })259 done()260 }))261 262 fastify.listen({ port: 0 }, err => {263 t.error(err)264 fastify.close()265 })266})267 268test('plugin encapsulation', t => {269 t.plan(10)270 const fastify = Fastify()271 272 fastify.register((instance, opts, done) => {273 instance.register(fp((i, o, n) => {274 i.decorate('test', 'first')275 n()276 }))277 278 instance.get('/first', (req, reply) => {279 reply.send({ plugin: instance.test })280 })281 282 done()283 })284 285 fastify.register((instance, opts, done) => {286 instance.register(fp((i, o, n) => {287 i.decorate('test', 'second')288 n()289 }))290 291 instance.get('/second', (req, reply) => {292 reply.send({ plugin: instance.test })293 })294 295 done()296 })297 298 fastify.ready(() => {299 t.notOk(fastify.test)300 })301 302 fastify.listen({ port: 0 }, err => {303 t.error(err)304 t.teardown(() => { fastify.close() })305 306 sget({307 method: 'GET',308 url: 'http://localhost:' + fastify.server.address().port + '/first'309 }, (err, response, body) => {310 t.error(err)311 t.equal(response.statusCode, 200)312 t.equal(response.headers['content-length'], '' + body.length)313 t.same(JSON.parse(body), { plugin: 'first' })314 })315 316 sget({317 method: 'GET',318 url: 'http://localhost:' + fastify.server.address().port + '/second'319 }, (err, response, body) => {320 t.error(err)321 t.equal(response.statusCode, 200)322 t.equal(response.headers['content-length'], '' + body.length)323 t.same(JSON.parse(body), { plugin: 'second' })324 })325 })326})327 