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

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

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

Entity Profile

Source Code

'use strict'

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

process.removeAllListeners('warning')

test('should prefer string content types over RegExp ones', async (t) => {
  t.plan(6)
  const fastify = Fastify()
  t.after(() => { fastify.close() })
  fastify.post('/', (req, reply) => {
    reply.send(req.body)
  })

  fastify.addContentTypeParser(/^application\/.*/, function (req, payload, done) {
    let data = ''
    payload.on('data', chunk => { data += chunk })
    payload.on('end', () => {
      done(null, data)
    })
  })

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

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

  const result1 = await fetch(fastifyServer, {
    method: 'POST',
    body: '{"k1":"myValue", "k2": "myValue"}',
    headers: {
      'Content-Type': 'application/json'
    }
  })

  t.assert.ok(result1.ok)
  t.assert.strictEqual(result1.status, 200)
  t.assert.equal(await result1.text(), JSON.stringify({ k1: 'myValue', k2: 'myValue' }))

  const result2 = await fetch(fastifyServer, {
    method: 'POST',
    body: 'javascript',
    headers: {
      'Content-Type': 'application/javascript'
    }
  })

  t.assert.ok(result2.ok)
  t.assert.strictEqual(result2.status, 200)
  t.assert.equal(await result2.text(), 'javascript')
})

test('removeContentTypeParser should support arrays of content types to remove', async (t) => {
  t.plan(7)

// ... (159 more lines)

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free