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

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

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

Entity Profile

Source Code

'use strict'

const { test } = require('node:test')
const Fastify = require('..')
const jsonParser = require('fast-json-body')

process.removeAllListeners('warning')

test('should be able to use default parser for extra content type', async t => {
  t.plan(3)
  const fastify = Fastify()
  t.after(() => fastify.close())

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

  fastify.addContentTypeParser('text/json', { parseAs: 'string' }, fastify.getDefaultJsonParser('ignore', 'ignore'))

  const fastifyServer = await fastify.listen({ port: 0 })

  const response = await fetch(fastifyServer, {
    method: 'POST',
    body: '{"hello":"world"}',
    headers: {
      'Content-Type': 'text/json'
    }
  })
  t.assert.ok(response.ok)
  t.assert.strictEqual(response.status, 200)
  t.assert.deepStrictEqual(await response.json(), { hello: 'world' })
})

test('contentTypeParser should add a custom parser with RegExp value', async (t) => {
  const fastify = Fastify()
  t.after(() => fastify.close())

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

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

  fastify.addContentTypeParser(/.*\+json$/, function (req, payload, done) {
    jsonParser(payload, function (err, body) {
      done(err, body)
    })
  })

  const fastifyServer = await fastify.listen({ port: 0 })

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

    const response = await fetch(fastifyServer, {
      method: 'POST',
      body: '{"hello":"world"}',
      headers: {
// ... (149 more lines)

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free