Back to Repositories

Validating Route Registration and Handler Configuration in Fastify

This test suite validates route handling functionality in Fastify, focusing on prefix management, path specifications, and request handling configurations. The tests ensure proper route registration, parameter validation, and handler execution across different routing scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of Fastify’s routing mechanisms, including:

  • Multiple prefix handling for identical route definitions
  • Path vs URI specification compatibility
  • Body limit validation for route configurations
  • Shorthand route handler implementations

Implementation Analysis

The testing approach employs Node’s native test framework to validate Fastify’s routing capabilities. Tests utilize async/await patterns alongside callback-based assertions, demonstrating both modern and traditional testing methodologies.

Key patterns include route registration validation, hook execution verification, and response payload testing using Fastify’s inject utility.

Technical Details

  • Testing Framework: Node.js native test module
  • Assertion Methods: strictEqual, deepStrictEqual, ifError
  • Fastify Features: route registration, hooks, inject for HTTP simulation
  • Test Utilities: async/await, callback patterns

Best Practices Demonstrated

The test suite exemplifies robust testing practices through isolated test cases, clear assertion messages, and comprehensive error handling validation.

  • Proper test planning with t.plan()
  • Error scenario validation
  • Async operation handling
  • Clean test case separation

fastify/fastify

test/route.2.test.js

            
'use strict'

const { test } = require('node:test')
const Fastify = require('../fastify')

test('same route definition object on multiple prefixes', async t => {
  t.plan(2)

  const routeObject = {
    handler: () => { },
    method: 'GET',
    url: '/simple'
  }

  const fastify = Fastify({ exposeHeadRoutes: false })

  fastify.register(async function (f) {
    f.addHook('onRoute', (routeOptions) => {
      t.assert.strictEqual(routeOptions.url, '/v1/simple')
    })
    f.route(routeObject)
  }, { prefix: '/v1' })
  fastify.register(async function (f) {
    f.addHook('onRoute', (routeOptions) => {
      t.assert.strictEqual(routeOptions.url, '/v2/simple')
    })
    f.route(routeObject)
  }, { prefix: '/v2' })

  await fastify.ready()
})

test('path can be specified in place of uri', (t, done) => {
  t.plan(3)
  const fastify = Fastify()

  fastify.route({
    method: 'GET',
    path: '/path',
    handler: function (req, reply) {
      reply.send({ hello: 'world' })
    }
  })

  const reqOpts = {
    method: 'GET',
    url: '/path'
  }

  fastify.inject(reqOpts, (err, res) => {
    t.assert.ifError(err)
    t.assert.strictEqual(res.statusCode, 200)
    t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'world' })
    done()
  })
})

test('invalid bodyLimit option - route', t => {
  t.plan(2)
  const fastify = Fastify()

  try {
    fastify.route({
      bodyLimit: false,
      method: 'PUT',
      handler: () => null
    })
    t.assert.fail('bodyLimit must be an integer')
  } catch (err) {
    t.assert.strictEqual(err.message, "'bodyLimit' option must be an integer > 0. Got 'false'")
  }

  try {
    fastify.post('/url', { bodyLimit: 10000.1 }, () => null)
    t.assert.fail('bodyLimit must be an integer')
  } catch (err) {
    t.assert.strictEqual(err.message, "'bodyLimit' option must be an integer > 0. Got '10000.1'")
  }
})

test('handler function in options of shorthand route should works correctly', (t, done) => {
  t.plan(3)

  const fastify = Fastify()
  fastify.get('/foo', {
    handler: (req, reply) => {
      reply.send({ hello: 'world' })
    }
  })

  fastify.inject({
    method: 'GET',
    url: '/foo'
  }, (err, res) => {
    t.assert.ifError(err)
    t.assert.strictEqual(res.statusCode, 200)
    t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'world' })
    done()
  })
})