strong-tie/inbound-calls
0
1'use strict'2 3const { test } = require('node:test')4const Fastify = require('..')5 6test('pretty print - static routes', (t, done) => {7 t.plan(2)8 9 const fastify = Fastify({ exposeHeadRoutes: false })10 fastify.get('/test', () => {})11 fastify.get('/test/hello', () => {})12 fastify.get('/hello/world', () => {})13 14 fastify.ready(() => {15 const tree = fastify.printRoutes()16 17 const expected = `\18└── /19 ├── test (GET)20 │ └── /hello (GET)21 └── hello/world (GET)22`23 24 t.assert.strictEqual(typeof tree, 'string')25 t.assert.strictEqual(tree, expected)26 done()27 })28})29 30test('pretty print - internal tree - static routes', (t, done) => {31 t.plan(4)32 33 const fastify = Fastify({ exposeHeadRoutes: false })34 fastify.get('/test', () => {})35 fastify.get('/test/hello', () => {})36 fastify.get('/hello/world', () => {})37 38 fastify.put('/test', () => {})39 fastify.put('/test/foo', () => {})40 41 fastify.ready(() => {42 const getTree = fastify.printRoutes({ method: 'GET' })43 const expectedGetTree = `\44└── /45 ├── test (GET)46 │ └── /hello (GET)47 └── hello/world (GET)48`49 50 t.assert.strictEqual(typeof getTree, 'string')51 t.assert.strictEqual(getTree, expectedGetTree)52 53 const putTree = fastify.printRoutes({ method: 'PUT' })54 const expectedPutTree = `\55└── /56 └── test (PUT)57 └── /foo (PUT)58`59 60 t.assert.strictEqual(typeof putTree, 'string')61 t.assert.strictEqual(putTree, expectedPutTree)62 done()63 })64})65 66test('pretty print - parametric routes', (t, done) => {67 t.plan(2)68 69 const fastify = Fastify({ exposeHeadRoutes: false })70 fastify.get('/test', () => {})71 fastify.get('/test/:hello', () => {})72 fastify.get('/hello/:world', () => {})73 74 fastify.ready(() => {75 const tree = fastify.printRoutes()76 77 const expected = `\78└── /79 ├── test (GET)80 │ └── /81 │ └── :hello (GET)82 └── hello/83 └── :world (GET)84`85 86 t.assert.strictEqual(typeof tree, 'string')87 t.assert.strictEqual(tree, expected)88 done()89 })90})91 92test('pretty print - internal tree - parametric routes', (t, done) => {93 t.plan(4)94 95 const fastify = Fastify({ exposeHeadRoutes: false })96 fastify.get('/test', () => {})97 fastify.get('/test/:hello', () => {})98 fastify.get('/hello/:world', () => {})99 100 fastify.put('/test', () => {})101 fastify.put('/test/:hello', () => {})102 103 fastify.ready(() => {104 const getTree = fastify.printRoutes({ method: 'GET' })105 const expectedGetTree = `\106└── /107 ├── test (GET)108 │ └── /109 │ └── :hello (GET)110 └── hello/111 └── :world (GET)112`113 114 t.assert.strictEqual(typeof getTree, 'string')115 t.assert.strictEqual(getTree, expectedGetTree)116 117 const putTree = fastify.printRoutes({ method: 'PUT' })118 const expectedPutTree = `\119└── /120 └── test (PUT)121 └── /122 └── :hello (PUT)123`124 125 t.assert.strictEqual(typeof putTree, 'string')126 t.assert.strictEqual(putTree, expectedPutTree)127 done()128 })129})130 131test('pretty print - mixed parametric routes', (t, done) => {132 t.plan(2)133 134 const fastify = Fastify({ exposeHeadRoutes: false })135 fastify.get('/test', () => {})136 fastify.get('/test/:hello', () => {})137 fastify.post('/test/:hello', () => {})138 fastify.get('/test/:hello/world', () => {})139 140 fastify.ready(() => {141 const tree = fastify.printRoutes()142 143 const expected = `\144└── /145 └── test (GET)146 └── /147 └── :hello (GET, POST)148 └── /world (GET)149`150 151 t.assert.strictEqual(typeof tree, 'string')152 t.assert.strictEqual(tree, expected)153 done()154 })155})156 157test('pretty print - wildcard routes', (t, done) => {158 t.plan(2)159 160 const fastify = Fastify({ exposeHeadRoutes: false })161 fastify.get('/test', () => {})162 fastify.get('/test/*', () => {})163 fastify.get('/hello/*', () => {})164 165 fastify.ready(() => {166 const tree = fastify.printRoutes()167 168 const expected = `\169└── /170 ├── test (GET)171 │ └── /172 │ └── * (GET)173 └── hello/174 └── * (GET)175`176 177 t.assert.strictEqual(typeof tree, 'string')178 t.assert.strictEqual(tree, expected)179 done()180 })181})182 183test('pretty print - internal tree - wildcard routes', (t, done) => {184 t.plan(4)185 186 const fastify = Fastify({ exposeHeadRoutes: false })187 fastify.get('/test', () => {})188 fastify.get('/test/*', () => {})189 fastify.get('/hello/*', () => {})190 191 fastify.put('/*', () => {})192 fastify.put('/test/*', () => {})193 194 fastify.ready(() => {195 const getTree = fastify.printRoutes({ method: 'GET' })196 const expectedGetTree = `\197└── /198 ├── test (GET)199 │ └── /200 │ └── * (GET)201 └── hello/202 └── * (GET)203`204 205 t.assert.strictEqual(typeof getTree, 'string')206 t.assert.strictEqual(getTree, expectedGetTree)207 208 const putTree = fastify.printRoutes({ method: 'PUT' })209 const expectedPutTree = `\210└── /211 ├── test/212 │ └── * (PUT)213 └── * (PUT)214`215 216 t.assert.strictEqual(typeof putTree, 'string')217 t.assert.strictEqual(putTree, expectedPutTree)218 done()219 })220})221 222test('pretty print - empty plugins', (t, done) => {223 t.plan(2)224 225 const fastify = Fastify()226 fastify.ready(() => {227 const tree = fastify.printPlugins()228 t.assert.strictEqual(typeof tree, 'string')229 t.assert.match(tree, /root \d+ ms\n└── bound _after \d+ ms/m)230 done()231 })232})233 234test('pretty print - nested plugins', (t, done) => {235 t.plan(4)236 237 const fastify = Fastify()238 fastify.register(async function foo (instance) {239 instance.register(async function bar () {})240 instance.register(async function baz () {})241 })242 fastify.ready(() => {243 const tree = fastify.printPlugins()244 t.assert.strictEqual(typeof tree, 'string')245 t.assert.match(tree, /foo/)246 t.assert.match(tree, /bar/)247 t.assert.match(tree, /baz/)248 done()249 })250})251 252test('pretty print - commonPrefix', (t, done) => {253 t.plan(4)254 255 const fastify = Fastify()256 fastify.get('/hello', () => {})257 fastify.put('/hello', () => {})258 fastify.get('/helicopter', () => {})259 260 fastify.ready(() => {261 const radixTree = fastify.printRoutes()262 const flatTree = fastify.printRoutes({ commonPrefix: false })263 264 const radixExpected = `\265└── /266 └── hel267 ├── lo (GET, HEAD, PUT)268 └── icopter (GET, HEAD)269`270 const flatExpected = `\271├── /hello (GET, HEAD, PUT)272└── /helicopter (GET, HEAD)273`274 t.assert.strictEqual(typeof radixTree, 'string')275 t.assert.strictEqual(typeof flatTree, 'string')276 t.assert.strictEqual(radixTree, radixExpected)277 t.assert.strictEqual(flatTree, flatExpected)278 done()279 })280})281 282test('pretty print - includeMeta, includeHooks', (t, done) => {283 t.plan(6)284 285 const fastify = Fastify()286 const onTimeout = () => {}287 fastify.get('/hello', () => {})288 fastify.put('/hello', () => {})289 fastify.get('/helicopter', () => {})290 291 fastify.addHook('onRequest', () => {})292 fastify.addHook('onTimeout', onTimeout)293 294 fastify.ready(() => {295 const radixTree = fastify.printRoutes({ includeHooks: true, includeMeta: ['errorHandler'] })296 const flatTree = fastify.printRoutes({ commonPrefix: false, includeHooks: true, includeMeta: ['errorHandler'] })297 const hooksOnly = fastify.printRoutes({ commonPrefix: false, includeHooks: true })298 299 const radixExpected = `\300└── /301 └── hel302 ├── lo (GET, PUT)303 │ • (onTimeout) ["onTimeout()"]304 │ • (onRequest) ["anonymous()"]305 │ • (errorHandler) "defaultErrorHandler()"306 │ lo (HEAD)307 │ • (onTimeout) ["onTimeout()"]308 │ • (onRequest) ["anonymous()"]309 │ • (onSend) ["headRouteOnSendHandler()"]310 │ • (errorHandler) "defaultErrorHandler()"311 └── icopter (GET)312 • (onTimeout) ["onTimeout()"]313 • (onRequest) ["anonymous()"]314 • (errorHandler) "defaultErrorHandler()"315 icopter (HEAD)316 • (onTimeout) ["onTimeout()"]317 • (onRequest) ["anonymous()"]318 • (onSend) ["headRouteOnSendHandler()"]319 • (errorHandler) "defaultErrorHandler()"320`321 const flatExpected = `\322├── /hello (GET, PUT)323│ • (onTimeout) ["onTimeout()"]324│ • (onRequest) ["anonymous()"]325│ • (errorHandler) "defaultErrorHandler()"326│ /hello (HEAD)327│ • (onTimeout) ["onTimeout()"]328│ • (onRequest) ["anonymous()"]329│ • (onSend) ["headRouteOnSendHandler()"]330│ • (errorHandler) "defaultErrorHandler()"331└── /helicopter (GET)332 • (onTimeout) ["onTimeout()"]333 • (onRequest) ["anonymous()"]334 • (errorHandler) "defaultErrorHandler()"335 /helicopter (HEAD)336 • (onTimeout) ["onTimeout()"]337 • (onRequest) ["anonymous()"]338 • (onSend) ["headRouteOnSendHandler()"]339 • (errorHandler) "defaultErrorHandler()"340`341 342 const hooksOnlyExpected = `\343├── /hello (GET, PUT)344│ • (onTimeout) ["onTimeout()"]345│ • (onRequest) ["anonymous()"]346│ /hello (HEAD)347│ • (onTimeout) ["onTimeout()"]348│ • (onRequest) ["anonymous()"]349│ • (onSend) ["headRouteOnSendHandler()"]350└── /helicopter (GET)351 • (onTimeout) ["onTimeout()"]352 • (onRequest) ["anonymous()"]353 /helicopter (HEAD)354 • (onTimeout) ["onTimeout()"]355 • (onRequest) ["anonymous()"]356 • (onSend) ["headRouteOnSendHandler()"]357`358 t.assert.strictEqual(typeof radixTree, 'string')359 t.assert.strictEqual(typeof flatTree, 'string')360 t.assert.strictEqual(typeof hooksOnlyExpected, 'string')361 t.assert.strictEqual(radixTree, radixExpected)362 t.assert.strictEqual(flatTree, flatExpected)363 t.assert.strictEqual(hooksOnly, hooksOnlyExpected)364 done()365 })366})367 