CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
custom-parser.4.test.js240 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')7const jsonParser = require('fast-json-body')8const { getServerUrl } = require('./helper')9 10process.removeAllListeners('warning')11 12test('should prefer string content types over RegExp ones', t => {13  t.plan(7)14  const fastify = Fastify()15  t.teardown(fastify.close.bind(fastify))16 17  fastify.post('/', (req, reply) => {18    reply.send(req.body)19  })20 21  fastify.addContentTypeParser(/^application\/.*/, function (req, payload, done) {22    let data = ''23    payload.on('data', chunk => { data += chunk })24    payload.on('end', () => {25      done(null, data)26    })27  })28 29  fastify.addContentTypeParser('application/json', function (req, payload, done) {30    jsonParser(payload, function (err, body) {31      done(err, body)32    })33  })34 35  fastify.listen({ port: 0 }, err => {36    t.error(err)37 38    sget({39      method: 'POST',40      url: getServerUrl(fastify),41      body: '{"k1":"myValue", "k2": "myValue"}',42      headers: {43        'Content-Type': 'application/json'44      }45    }, (err, response, body) => {46      t.error(err)47      t.equal(response.statusCode, 200)48      t.same(body.toString(), JSON.stringify({ k1: 'myValue', k2: 'myValue' }))49    })50 51    sget({52      method: 'POST',53      url: getServerUrl(fastify),54      body: 'javascript',55      headers: {56        'Content-Type': 'application/javascript'57      }58    }, (err, response, body) => {59      t.error(err)60      t.equal(response.statusCode, 200)61      t.same(body.toString(), 'javascript')62    })63  })64})65 66test('removeContentTypeParser should support arrays of content types to remove', t => {67  t.plan(8)68 69  const fastify = Fastify()70  t.teardown(fastify.close.bind(fastify))71 72  fastify.addContentTypeParser('application/xml', function (req, payload, done) {73    payload.on('data', () => {})74    payload.on('end', () => {75      done(null, 'xml')76    })77  })78 79  fastify.addContentTypeParser(/^image\/.*/, function (req, payload, done) {80    payload.on('data', () => {})81    payload.on('end', () => {82      done(null, 'image')83    })84  })85 86  fastify.removeContentTypeParser([/^image\/.*/, 'application/json'])87 88  fastify.post('/', (req, reply) => {89    reply.send(req.body)90  })91 92  fastify.listen({ port: 0 }, err => {93    t.error(err)94 95    sget({96      method: 'POST',97      url: getServerUrl(fastify),98      body: '<?xml version="1.0">',99      headers: {100        'Content-Type': 'application/xml'101      }102    }, (err, response, body) => {103      t.error(err)104      t.equal(response.statusCode, 200)105      t.same(body.toString(), 'xml')106    })107 108    sget({109      method: 'POST',110      url: getServerUrl(fastify),111      body: '',112      headers: {113        'Content-Type': 'image/png'114      }115    }, (err, response, body) => {116      t.error(err)117      t.equal(response.statusCode, 415)118    })119 120    sget({121      method: 'POST',122      url: getServerUrl(fastify),123      body: '{test: "test"}',124      headers: {125        'Content-Type': 'application/json'126      }127    }, (err, response, body) => {128      t.error(err)129      t.equal(response.statusCode, 415)130    })131  })132})133 134test('removeContentTypeParser should support encapsulation', t => {135  t.plan(6)136 137  const fastify = Fastify()138 139  fastify.addContentTypeParser('application/xml', function (req, payload, done) {140    payload.on('data', () => {})141    payload.on('end', () => {142      done(null, 'xml')143    })144  })145 146  fastify.post('/', (req, reply) => {147    reply.send(req.body)148  })149 150  fastify.register(function (instance, options, done) {151    instance.removeContentTypeParser('application/xml')152 153    instance.post('/encapsulated', (req, reply) => {154      reply.send(req.body)155    })156 157    done()158  })159 160  fastify.listen({ port: 0 }, err => {161    t.error(err)162 163    sget({164      method: 'POST',165      url: getServerUrl(fastify) + '/encapsulated',166      body: '<?xml version="1.0">',167      headers: {168        'Content-Type': 'application/xml'169      }170    }, (err, response, body) => {171      t.error(err)172      t.equal(response.statusCode, 415)173    })174 175    sget({176      method: 'POST',177      url: getServerUrl(fastify),178      body: '<?xml version="1.0">',179      headers: {180        'Content-Type': 'application/xml'181      }182    }, (err, response, body) => {183      t.error(err)184      t.equal(response.statusCode, 200)185      t.same(body.toString(), 'xml')186      fastify.close()187    })188  })189})190 191test('removeAllContentTypeParsers should support encapsulation', t => {192  t.plan(6)193 194  const fastify = Fastify()195 196  fastify.post('/', (req, reply) => {197    reply.send(req.body)198  })199 200  fastify.register(function (instance, options, done) {201    instance.removeAllContentTypeParsers()202 203    instance.post('/encapsulated', (req, reply) => {204      reply.send(req.body)205    })206 207    done()208  })209 210  fastify.listen({ port: 0 }, err => {211    t.error(err)212 213    sget({214      method: 'POST',215      url: getServerUrl(fastify) + '/encapsulated',216      body: '{}',217      headers: {218        'Content-Type': 'application/json'219      }220    }, (err, response, body) => {221      t.error(err)222      t.equal(response.statusCode, 415)223    })224 225    sget({226      method: 'POST',227      url: getServerUrl(fastify),228      body: '{"test":1}',229      headers: {230        'Content-Type': 'application/json'231      }232    }, (err, response, body) => {233      t.error(err)234      t.equal(response.statusCode, 200)235      t.same(JSON.parse(body.toString()).test, 1)236      fastify.close()237    })238  })239})240