CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
custom-parser.5.test.js150 linesDownload Raw Back to test
1'use strict'2 3const { test } = require('node:test')4const sget = require('simple-get').concat5const Fastify = require('../fastify')6const jsonParser = require('fast-json-body')7const { getServerUrl, plainTextParser } = require('./helper')8 9process.removeAllListeners('warning')10 11test('cannot remove all content type parsers after binding', (t, done) => {12  t.plan(2)13 14  const fastify = Fastify()15 16  fastify.listen({ port: 0 }, function (err) {17    t.assert.ifError(err)18    t.assert.throws(() => fastify.removeAllContentTypeParsers())19    fastify.close()20    done()21  })22})23 24test('cannot remove content type parsers after binding', (t, done) => {25  t.plan(2)26 27  const fastify = Fastify()28 29  t.after(() => fastify.close())30 31  fastify.listen({ port: 0 }, function (err) {32    t.assert.ifError(err)33    t.assert.throws(() => fastify.removeContentTypeParser('application/json'))34    done()35  })36})37 38test('should be able to override the default json parser after removeAllContentTypeParsers', (t, done) => {39  t.plan(5)40 41  const fastify = Fastify()42 43  fastify.post('/', (req, reply) => {44    reply.send(req.body)45  })46 47  fastify.removeAllContentTypeParsers()48 49  fastify.addContentTypeParser('application/json', function (req, payload, done) {50    t.assert.ok('called')51    jsonParser(payload, function (err, body) {52      done(err, body)53    })54  })55 56  fastify.listen({ port: 0 }, err => {57    t.assert.ifError(err)58 59    sget({60      method: 'POST',61      url: getServerUrl(fastify),62      body: '{"hello":"world"}',63      headers: {64        'Content-Type': 'application/json'65      }66    }, (err, response, body) => {67      t.assert.ifError(err)68      t.assert.strictEqual(response.statusCode, 200)69      t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))70      fastify.close()71      done()72    })73  })74})75 76test('should be able to override the default plain text parser after removeAllContentTypeParsers', (t, done) => {77  t.plan(5)78 79  const fastify = Fastify()80 81  fastify.post('/', (req, reply) => {82    reply.send(req.body)83  })84 85  fastify.removeAllContentTypeParsers()86 87  fastify.addContentTypeParser('text/plain', function (req, payload, done) {88    t.assert.ok('called')89    plainTextParser(payload, function (err, body) {90      done(err, body)91    })92  })93 94  fastify.listen({ port: 0 }, err => {95    t.assert.ifError(err)96 97    sget({98      method: 'POST',99      url: getServerUrl(fastify),100      body: 'hello world',101      headers: {102        'Content-Type': 'text/plain'103      }104    }, (err, response, body) => {105      t.assert.ifError(err)106      t.assert.strictEqual(response.statusCode, 200)107      t.assert.strictEqual(body.toString(), 'hello world')108      fastify.close()109      done()110    })111  })112})113 114test('should be able to add a custom content type parser after removeAllContentTypeParsers', (t, done) => {115  t.plan(5)116 117  const fastify = Fastify()118 119  fastify.post('/', (req, reply) => {120    reply.send(req.body)121  })122 123  fastify.removeAllContentTypeParsers()124  fastify.addContentTypeParser('application/jsoff', function (req, payload, done) {125    t.assert.ok('called')126    jsonParser(payload, function (err, body) {127      done(err, body)128    })129  })130 131  fastify.listen({ port: 0 }, err => {132    t.assert.ifError(err)133 134    sget({135      method: 'POST',136      url: getServerUrl(fastify),137      body: '{"hello":"world"}',138      headers: {139        'Content-Type': 'application/jsoff'140      }141    }, (err, response, body) => {142      t.assert.ifError(err)143      t.assert.strictEqual(response.statusCode, 200)144      t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))145      fastify.close()146      done()147    })148  })149})150