Home / File/ custom-parser.0.test.js — fastify Source File

custom-parser.0.test.js — fastify Source File

Architecture documentation for custom-parser.0.test.js, a javascript file in the fastify codebase.

Entity Profile

Source Code

'use strict'

const fs = require('node:fs')
const { test } = require('node:test')
const Fastify = require('../fastify')
const jsonParser = require('fast-json-body')
const { plainTextParser } = require('./helper')

process.removeAllListeners('warning')

test('contentTypeParser method should exist', t => {
  t.plan(1)
  const fastify = Fastify()
  t.assert.ok(fastify.addContentTypeParser)
})

test('contentTypeParser should add a custom parser', async (t) => {
  t.plan(2)
  const fastify = Fastify()

  fastify.post('/', (req, reply) => {
    reply.send(req.body)
  })

  fastify.options('/', (req, reply) => {
    reply.send(req.body)
  })

  fastify.addContentTypeParser('application/jsoff', function (req, payload, done) {
    jsonParser(payload, function (err, body) {
      done(err, body)
    })
  })

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

  await t.test('in POST', async (t) => {
    t.plan(3)

    const result = await fetch(fastifyServer, {
      method: 'POST',
      body: '{"hello":"world"}',
      headers: {
        'Content-Type': 'application/jsoff'
      }
    })

    t.assert.ok(result.ok)
    t.assert.strictEqual(result.status, 200)
    t.assert.deepStrictEqual(await result.json(), { hello: 'world' })
  })

  await t.test('in OPTIONS', async (t) => {
    t.plan(2)

    const result = await fetch(fastifyServer, {
      method: 'OPTIONS',
      body: '{"hello":"world"}',
      headers: {
// ... (642 more lines)

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free