Home / File/ body-limit.test.js — fastify Source File

body-limit.test.js — fastify Source File

Architecture documentation for body-limit.test.js, a javascript file in the fastify codebase.

Entity Profile

Source Code

'use strict'

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

test('bodyLimit', async t => {
  t.plan(4)

  try {
    Fastify({ bodyLimit: 1.3 })
    t.assert.fail('option must be an integer')
  } catch (err) {
    t.assert.ok(err)
  }

  try {
    Fastify({ bodyLimit: [] })
    t.assert.fail('option must be an integer')
  } catch (err) {
    t.assert.ok(err)
  }

  const fastify = Fastify({ bodyLimit: 1 })

  fastify.post('/', (request, reply) => {
    reply.send({ error: 'handler should not be called' })
  })

  const fastifyServer = await fastify.listen({ port: 0 })
  t.after(() => { fastify.close() })

  const result = await fetch(fastifyServer, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify([])
  })

  t.assert.ok(!result.ok)
  t.assert.strictEqual(result.status, 413)
})

test('bodyLimit is applied to decoded content', async (t) => {
  t.plan(6)

  const body = { x: 'x'.repeat(30000) }
  const json = JSON.stringify(body)
  const encoded = zlib.gzipSync(json)

  const fastify = Fastify()

  fastify.addHook('preParsing', async (req, reply, payload) => {
    t.assert.strictEqual(req.headers['content-length'], `${encoded.length}`)
    const unzip = zlib.createGunzip()
    Object.defineProperty(unzip, 'receivedEncodedLength', {
      get () {
        return unzip.bytesWritten
      }
    })
    payload.pipe(unzip)
// ... (165 more lines)

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free