Home / File/ buffer.test.js — fastify Source File

buffer.test.js — fastify Source File

Architecture documentation for buffer.test.js, a javascript file in the fastify codebase.

Entity Profile

Source Code

'use strict'

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

test('Buffer test', async t => {
  const fastify = Fastify()
  fastify.addContentTypeParser('application/json', { parseAs: 'buffer' }, fastify.getDefaultJsonParser('error', 'ignore'))

  fastify.delete('/', async (request) => {
    return request.body
  })

  await test('should return 200 if the body is not empty', async t => {
    t.plan(3)

    const response = await fastify.inject({
      method: 'DELETE',
      url: '/',
      payload: Buffer.from('{"hello":"world"}'),
      headers: {
        'content-type': 'application/json'
      }
    })

    t.assert.ifError(response.error)
    t.assert.strictEqual(response.statusCode, 200)
    t.assert.deepStrictEqual(response.payload.toString(), '{"hello":"world"}')
  })

  await test('should return 400 if the body is empty', async t => {
    t.plan(3)

    const response = await fastify.inject({
      method: 'DELETE',
      url: '/',
      payload: Buffer.alloc(0),
      headers: {
        'content-type': 'application/json'
      }
    })

    t.assert.ifError(response.error)
    t.assert.strictEqual(response.statusCode, 400)
    t.assert.deepStrictEqual(JSON.parse(response.payload.toString()), {
      error: 'Bad Request',
      code: 'FST_ERR_CTP_EMPTY_JSON_BODY',
      message: 'Body cannot be empty when content-type is set to \'application/json\'',
      statusCode: 400
    })
  })

  await test('should return 400 if the body is invalid json', async t => {
    t.plan(3)

    const response = await fastify.inject({
      method: 'DELETE',
      url: '/',
      payload: Buffer.from(']'),
      headers: {
        'content-type': 'application/json'
      }
    })

    t.assert.ifError(response.error)
    t.assert.strictEqual(response.statusCode, 400)
    t.assert.deepStrictEqual(JSON.parse(response.payload.toString()), {
      error: 'Bad Request',
      code: 'FST_ERR_CTP_INVALID_JSON_BODY',
      message: 'Body is not valid JSON but content-type is set to \'application/json\'',
      statusCode: 400
    })
  })
})

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free