Home / File/ allow-unsafe-regex.test.js — fastify Source File

allow-unsafe-regex.test.js — fastify Source File

Architecture documentation for allow-unsafe-regex.test.js, a javascript file in the fastify codebase.

Entity Profile

Source Code

'use strict'

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

test('allow unsafe regex', async t => {
  t.plan(2)

  const fastify = Fastify({
    allowUnsafeRegex: false
  })
  t.after(() => fastify.close())

  fastify.get('/:foo(^[0-9]*$)', (req, reply) => {
    reply.send({ foo: req.params.foo })
  })

  await fastify.listen({ port: 0 })

  const result = await fetch(`http://localhost:${fastify.server.address().port}/1234`)
  t.assert.strictEqual(result.status, 200)
  t.assert.deepStrictEqual(await result.json(), { foo: '1234' })
})

test('allow unsafe regex not match', async t => {
  t.plan(1)

  const fastify = Fastify({
    allowUnsafeRegex: false
  })
  t.after(() => fastify.close())

  fastify.get('/:foo(^[0-9]*$)', (req, reply) => {
    reply.send({ foo: req.params.foo })
  })

  await fastify.listen({ port: 0 })

  const result = await fetch(`http://localhost:${fastify.server.address().port}/a1234`)
  t.assert.strictEqual(result.status, 404)
})

test('allow unsafe regex not safe', (t, done) => {
  t.plan(1)

  const fastify = Fastify({
    allowUnsafeRegex: false
  })
  t.after(() => fastify.close())

  t.assert.throws(() => {
    fastify.get('/:foo(^([0-9]+){4}$)', (req, reply) => {
      reply.send({ foo: req.params.foo })
    })
  })
  done()
})

test('allow unsafe regex not safe by default', (t, done) => {
  t.plan(1)

  const fastify = Fastify()
  t.after(() => fastify.close())

  t.assert.throws(() => {
    fastify.get('/:foo(^([0-9]+){4}$)', (req, reply) => {
      reply.send({ foo: req.params.foo })
    })
  })
  done()
})

test('allow unsafe regex allow unsafe', async t => {
  t.plan(3)

  const fastify = Fastify({
    allowUnsafeRegex: true
  })
  t.after(() => fastify.close())

  t.assert.doesNotThrow(() => {
    fastify.get('/:foo(^([0-9]+){4}$)', (req, reply) => {
      reply.send({ foo: req.params.foo })
    })
  })

  await fastify.listen({ port: 0 })

  const result = await fetch(`http://localhost:${fastify.server.address().port}/1234`)
  t.assert.strictEqual(result.status, 200)
  t.assert.deepStrictEqual(await result.json(), { foo: '1234' })
})

Frequently Asked Questions

What does allow-unsafe-regex.test.js do?
allow-unsafe-regex.test.js is a source file in the fastify codebase, written in javascript.
Where is allow-unsafe-regex.test.js in the architecture?
allow-unsafe-regex.test.js is located at test/allow-unsafe-regex.test.js (directory: test).

Analyze Your Own Codebase

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

Try Supermodel Free