Home / Function/ testHandlerOrBeforeHandlerHook() — fastify Function Reference

testHandlerOrBeforeHandlerHook() — fastify Function Reference

Architecture documentation for the testHandlerOrBeforeHandlerHook() function in skip-reply-send.test.js from the fastify codebase.

Entity Profile

Dependency Diagram

graph TD
  1d87f356_1c28_e736_58f6_f9d2e1076ca9["testHandlerOrBeforeHandlerHook()"]
  55e3c2b9_d2ca_e414_a388_8757a5a41ba5["skip-reply-send.test.js"]
  1d87f356_1c28_e736_58f6_f9d2e1076ca9 -->|defined in| 55e3c2b9_d2ca_e414_a388_8757a5a41ba5
  style 1d87f356_1c28_e736_58f6_f9d2e1076ca9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

test/skip-reply-send.test.js lines 115–311

function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
  const idx = hookOrHandler === 'handler' ? lifecycleHooks.indexOf('preHandler') : lifecycleHooks.indexOf(hookOrHandler)
  const previousHooks = lifecycleHooks.slice(0, idx)
  const nextHooks = lifecycleHooks.slice(idx + 1)

  describe(`Hijacking inside ${hookOrHandler} skips all the following hooks and handler execution`, () => {
    test('Sending a response using reply.raw => onResponse hook is called', async (t) => {
      const stream = split(JSON.parse)
      const app = Fastify({
        logger: {
          stream
        }
      })

      stream.on('data', (line) => {
        t.assert.notStrictEqual(line.level, 40) // there are no errors
        t.assert.notStrictEqual(line.level, 50) // there are no errors
      })

      previousHooks.forEach(h => app.addHook(h, async (req, reply) => t.assert.ok(`${h} should be called`)))

      if (hookOrHandler === 'handler') {
        app.get('/', (req, reply) => {
          reply.hijack()
          reply.raw.end(`hello from ${hookOrHandler}`)
        })
      } else {
        app.addHook(hookOrHandler, async (req, reply) => {
          reply.hijack()
          reply.raw.end(`hello from ${hookOrHandler}`)
        })
        app.get('/', (req, reply) => t.assert.fail('Handler should not be called'))
      }

      nextHooks.forEach(h => {
        if (h === 'onResponse') {
          app.addHook(h, async (req, reply) => t.assert.ok(`${h} should be called`))
        } else {
          app.addHook(h, async (req, reply) => t.assert.fail(`${h} should not be called`))
        }
      })

      await app.inject({
        method: 'GET',
        url: '/'
      }).then((res) => {
        t.assert.strictEqual(res.statusCode, 200)
        t.assert.strictEqual(res.body, `hello from ${hookOrHandler}`)
      })
    })

    test('Sending a response using req.socket => onResponse not called', (t, testDone) => {
      const stream = split(JSON.parse)
      const app = Fastify({
        logger: {
          stream
        }
      })
      t.after(() => app.close())

      stream.on('data', (line) => {
        t.assert.notStrictEqual(line.level, 40) // there are no errors
        t.assert.notStrictEqual(line.level, 50) // there are no errors
      })

      previousHooks.forEach(h => app.addHook(h, async (req, reply) => t.assert.ok(`${h} should be called`)))

      if (hookOrHandler === 'handler') {
        app.get('/', (req, reply) => {
          reply.hijack()
          req.socket.write('HTTP/1.1 200 OK\r\n\r\n')
          req.socket.write(`hello from ${hookOrHandler}`)
          req.socket.end()
        })
      } else {
        app.addHook(hookOrHandler, async (req, reply) => {
          reply.hijack()
          req.socket.write('HTTP/1.1 200 OK\r\n\r\n')
          req.socket.write(`hello from ${hookOrHandler}`)
          req.socket.end()
        })

Domain

Subdomains

Frequently Asked Questions

What does testHandlerOrBeforeHandlerHook() do?
testHandlerOrBeforeHandlerHook() is a function in the fastify codebase, defined in test/skip-reply-send.test.js.
Where is testHandlerOrBeforeHandlerHook() defined?
testHandlerOrBeforeHandlerHook() is defined in test/skip-reply-send.test.js at line 115.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free