CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
custom-parser.3.test.js257 linesDownload Raw Back to test
1'use strict'2 3const { test } = require('node:test')4const sget = require('simple-get').concat5const Fastify = require('..')6const jsonParser = require('fast-json-body')7const { getServerUrl } = require('./helper')8 9process.removeAllListeners('warning')10 11test('should be able to use default parser for extra content type', (t, done) => {12  t.plan(4)13  const fastify = Fastify()14  t.after(() => fastify.close())15 16  fastify.post('/', (request, reply) => {17    reply.send(request.body)18  })19 20  fastify.addContentTypeParser('text/json', { parseAs: 'string' }, fastify.getDefaultJsonParser('ignore', 'ignore'))21 22  fastify.listen({ port: 0 }, err => {23    t.assert.ifError(err)24 25    sget({26      method: 'POST',27      url: getServerUrl(fastify),28      body: '{"hello":"world"}',29      headers: {30        'Content-Type': 'text/json'31      }32    }, (err, response, body) => {33      t.assert.ifError(err)34      t.assert.strictEqual(response.statusCode, 200)35      t.assert.deepStrictEqual(JSON.parse(body.toString()), { hello: 'world' })36      done()37    })38  })39})40 41test('contentTypeParser should add a custom parser with RegExp value', async (t) => {42  const fastify = Fastify()43  t.after(() => fastify.close())44 45  fastify.post('/', (req, reply) => {46    reply.send(req.body)47  })48 49  fastify.options('/', (req, reply) => {50    reply.send(req.body)51  })52 53  fastify.addContentTypeParser(/.*\+json$/, function (req, payload, done) {54    jsonParser(payload, function (err, body) {55      done(err, body)56    })57  })58 59  fastify.listen({ port: 0 }, async err => {60    t.assert.ifError(err)61 62    await t.test('in POST', (t, done) => {63      t.plan(3)64      t.after(() => fastify.close())65 66      sget({67        method: 'POST',68        url: getServerUrl(fastify),69        body: '{"hello":"world"}',70        headers: {71          'Content-Type': 'application/vnd.test+json'72        }73      }, (err, response, body) => {74        t.assert.ifError(err)75        t.assert.strictEqual(response.statusCode, 200)76        t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))77        done()78      })79    })80 81    await t.test('in OPTIONS', (t, done) => {82      t.plan(3)83      t.after(() => fastify.close())84 85      sget({86        method: 'OPTIONS',87        url: getServerUrl(fastify),88        body: '{"hello":"world"}',89        headers: {90          'Content-Type': 'weird/content-type+json'91        }92      }, (err, response, body) => {93        t.assert.ifError(err)94        t.assert.strictEqual(response.statusCode, 200)95        t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))96        done()97      })98    })99  })100})101 102test('contentTypeParser should add multiple custom parsers with RegExp values', async t => {103  t.plan(6)104  const fastify = Fastify()105  t.after(() => fastify.close())106 107  fastify.post('/', (req, reply) => {108    reply.send(req.body)109  })110 111  fastify.addContentTypeParser(/.*\+json$/, function (req, payload, done) {112    jsonParser(payload, function (err, body) {113      done(err, body)114    })115  })116 117  fastify.addContentTypeParser(/.*\+xml$/, function (req, payload, done) {118    done(null, 'xml')119  })120 121  fastify.addContentTypeParser(/.*\+myExtension$/i, function (req, payload, done) {122    let data = ''123    payload.on('data', chunk => { data += chunk })124    payload.on('end', () => {125      done(null, data + 'myExtension')126    })127  })128 129  await fastify.ready()130 131  {132    const response = await fastify.inject({133      method: 'POST',134      url: '/',135      body: '{"hello":"world"}',136      headers: {137        'Content-Type': 'application/vnd.hello+json'138      }139    })140    t.assert.strictEqual(response.statusCode, 200)141    t.assert.deepStrictEqual(response.payload.toString(), '{"hello":"world"}')142  }143 144  {145    const response = await fastify.inject({146      method: 'POST',147      url: '/',148      body: '{"hello":"world"}',149      headers: {150        'Content-Type': 'application/test+xml'151      }152    })153    t.assert.strictEqual(response.statusCode, 200)154    t.assert.deepStrictEqual(response.payload.toString(), 'xml')155  }156 157  await fastify.inject({158    method: 'POST',159    path: '/',160    payload: 'abcdefg',161    headers: {162      'Content-Type': 'application/+myExtension'163    }164  }).then((response) => {165    t.assert.strictEqual(response.statusCode, 200)166    t.assert.deepStrictEqual(response.payload.toString(), 'abcdefgmyExtension')167  }).catch((err) => {168    t.assert.ifError(err)169  })170})171 172test('catch all content type parser should not interfere with content type parser', (t, done) => {173  t.plan(10)174  const fastify = Fastify()175  t.after(() => fastify.close())176 177  fastify.post('/', (req, reply) => {178    reply.send(req.body)179  })180 181  fastify.addContentTypeParser('*', function (req, payload, done) {182    let data = ''183    payload.on('data', chunk => { data += chunk })184    payload.on('end', () => {185      done(null, data)186    })187  })188 189  fastify.addContentTypeParser(/^application\/.*/, function (req, payload, done) {190    jsonParser(payload, function (err, body) {191      done(err, body)192    })193  })194 195  fastify.addContentTypeParser('text/html', function (req, payload, done) {196    let data = ''197    payload.on('data', chunk => { data += chunk })198    payload.on('end', () => {199      done(null, data + 'html')200    })201  })202 203  fastify.listen({ port: 0 }, err => {204    t.assert.ifError(err)205 206    let pending = 3207 208    function completed () {209      if (--pending === 0) {210        done()211      }212    }213 214    sget({215      method: 'POST',216      url: getServerUrl(fastify),217      body: '{"myKey":"myValue"}',218      headers: {219        'Content-Type': 'application/json'220      }221    }, (err, response, body) => {222      t.assert.ifError(err)223      t.assert.strictEqual(response.statusCode, 200)224      t.assert.deepStrictEqual(body.toString(), JSON.stringify({ myKey: 'myValue' }))225      completed()226    })227 228    sget({229      method: 'POST',230      url: getServerUrl(fastify),231      body: 'body',232      headers: {233        'Content-Type': 'very-weird-content-type'234      }235    }, (err, response, body) => {236      t.assert.ifError(err)237      t.assert.strictEqual(response.statusCode, 200)238      t.assert.deepStrictEqual(body.toString(), 'body')239      completed()240    })241 242    sget({243      method: 'POST',244      url: getServerUrl(fastify),245      body: 'my text',246      headers: {247        'Content-Type': 'text/html'248      }249    }, (err, response, body) => {250      t.assert.ifError(err)251      t.assert.strictEqual(response.statusCode, 200)252      t.assert.deepStrictEqual(body.toString(), 'my texthtml')253      completed()254    })255  })256})257