CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
custom-parser-async.test.js67 linesDownload Raw Back to test
1'use strict'2 3const t = require('tap')4const test = t.test5const sget = require('simple-get').concat6const Fastify = require('../fastify')7 8process.removeAllListeners('warning')9 10test('contentTypeParser should add a custom async parser', t => {11  t.plan(3)12  const fastify = Fastify()13 14  fastify.post('/', (req, reply) => {15    reply.send(req.body)16  })17 18  fastify.options('/', (req, reply) => {19    reply.send(req.body)20  })21 22  fastify.addContentTypeParser('application/jsoff', async function (req, payload) {23    const res = await new Promise((resolve, reject) => resolve(payload))24    return res25  })26 27  fastify.listen({ port: 0 }, err => {28    t.error(err)29 30    t.teardown(() => fastify.close())31 32    t.test('in POST', t => {33      t.plan(3)34 35      sget({36        method: 'POST',37        url: 'http://localhost:' + fastify.server.address().port,38        body: '{"hello":"world"}',39        headers: {40          'Content-Type': 'application/jsoff'41        }42      }, (err, response, body) => {43        t.error(err)44        t.equal(response.statusCode, 200)45        t.same(body.toString(), JSON.stringify({ hello: 'world' }))46      })47    })48 49    t.test('in OPTIONS', t => {50      t.plan(3)51 52      sget({53        method: 'OPTIONS',54        url: 'http://localhost:' + fastify.server.address().port,55        body: '{"hello":"world"}',56        headers: {57          'Content-Type': 'application/jsoff'58        }59      }, (err, response, body) => {60        t.error(err)61        t.equal(response.statusCode, 200)62        t.same(body.toString(), JSON.stringify({ hello: 'world' }))63      })64    })65  })66})67