CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
throw.test.js364 linesDownload Raw Back to test
1'use strict'2 3const { test } = require('tap')4const Fastify = require('..')5 6test('Fastify should throw on wrong options', t => {7  t.plan(2)8  try {9    Fastify('lol')10    t.fail()11  } catch (e) {12    t.equal(e.message, 'Options must be an object')13    t.pass()14  }15})16 17test('Fastify should throw on multiple assignment to the same route', t => {18  t.plan(1)19  const fastify = Fastify()20 21  fastify.get('/', () => {})22 23  try {24    fastify.get('/', () => {})25    t.fail('Should throw fastify duplicated route declaration')26  } catch (error) {27    t.equal(error.code, 'FST_ERR_DUPLICATED_ROUTE')28  }29})30 31test('Fastify should throw for an invalid schema, printing the error route - headers', t => {32  t.plan(2)33 34  const badSchema = {35    type: 'object',36    properties: {37      bad: {38        type: 'bad-type'39      }40    }41  }42 43  const fastify = Fastify()44  fastify.get('/', { schema: { headers: badSchema } }, () => {})45  fastify.get('/not-loaded', { schema: { headers: badSchema } }, () => {})46 47  fastify.ready(err => {48    t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')49    t.match(err.message, /Failed building the validation schema for GET: \//)50  })51})52 53test('Fastify should throw for an invalid schema, printing the error route - body', t => {54  t.plan(2)55 56  const badSchema = {57    type: 'object',58    properties: {59      bad: {60        type: 'bad-type'61      }62    }63  }64 65  const fastify = Fastify()66  fastify.register((instance, opts, done) => {67    instance.post('/form', { schema: { body: badSchema } }, () => {})68    done()69  }, { prefix: 'hello' })70 71  fastify.ready(err => {72    t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')73    t.match(err.message, /Failed building the validation schema for POST: \/hello\/form/)74  })75})76 77test('Should throw on unsupported method', t => {78  t.plan(1)79  const fastify = Fastify()80  try {81    fastify.route({82      method: 'TROLL',83      url: '/',84      schema: {},85      handler: function (req, reply) {}86    })87    t.fail()88  } catch (e) {89    t.pass()90  }91})92 93test('Should throw on missing handler', t => {94  t.plan(1)95  const fastify = Fastify()96  try {97    fastify.route({98      method: 'GET',99      url: '/'100    })101    t.fail()102  } catch (e) {103    t.pass()104  }105})106 107test('Should throw if one method is unsupported', t => {108  const fastify = Fastify()109  t.plan(1)110  try {111    fastify.route({112      method: ['GET', 'TROLL'],113      url: '/',114      schema: {},115      handler: function (req, reply) {}116    })117    t.fail()118  } catch (e) {119    t.pass()120  }121})122 123test('Should throw on duplicate content type parser', t => {124  t.plan(1)125 126  const fastify = Fastify()127  function customParser (req, payload, done) { done(null, '') }128 129  fastify.addContentTypeParser('application/qq', customParser)130  try {131    fastify.addContentTypeParser('application/qq', customParser)132    t.fail()133  } catch (e) {134    t.pass()135  }136})137 138test('Should throw on duplicate decorator', t => {139  t.plan(1)140 141  const fastify = Fastify()142  const fooObj = {}143 144  fastify.decorate('foo', fooObj)145  try {146    fastify.decorate('foo', fooObj)147    t.fail()148  } catch (e) {149    t.pass()150  }151})152 153test('Should not throw on duplicate decorator encapsulation', t => {154  t.plan(1)155 156  const fastify = Fastify()157  const foo2Obj = {}158 159  fastify.decorate('foo2', foo2Obj)160 161  fastify.register(function (fastify, opts, done) {162    t.doesNotThrow(() => {163      fastify.decorate('foo2', foo2Obj)164    })165    done()166  })167 168  fastify.ready()169})170 171test('Should throw on duplicate request decorator', t => {172  t.plan(2)173 174  const fastify = Fastify()175 176  fastify.decorateRequest('foo', null)177  try {178    fastify.decorateRequest('foo', null)179    t.fail()180  } catch (e) {181    t.equal(e.code, 'FST_ERR_DEC_ALREADY_PRESENT')182    t.equal(e.message, 'The decorator \'foo\' has already been added!')183  }184})185 186test('Should throw if request decorator dependencies are not met', t => {187  t.plan(2)188 189  const fastify = Fastify()190 191  try {192    fastify.decorateRequest('bar', null, ['world'])193    t.fail()194  } catch (e) {195    t.equal(e.code, 'FST_ERR_DEC_MISSING_DEPENDENCY')196    t.equal(e.message, 'The decorator is missing dependency \'world\'.')197  }198})199 200test('Should throw on duplicate reply decorator', t => {201  t.plan(1)202 203  const fastify = Fastify()204 205  fastify.decorateReply('foo', null)206  try {207    fastify.decorateReply('foo', null)208    t.fail()209  } catch (e) {210    t.ok(/has already been added/.test(e.message))211  }212})213 214test('Should throw if reply decorator dependencies are not met', t => {215  t.plan(1)216 217  const fastify = Fastify()218 219  try {220    fastify.decorateReply('bar', null, ['world'])221    t.fail()222  } catch (e) {223    t.ok(/missing dependency/.test(e.message))224  }225})226 227test('Should throw if handler as the third parameter to the shortcut method is missing and the second parameter is not a function and also not an object', t => {228  t.plan(5)229 230  const fastify = Fastify()231 232  try {233    fastify.get('/foo/1', '')234    t.fail()235  } catch (e) {236    t.pass()237  }238 239  try {240    fastify.get('/foo/2', 1)241    t.fail()242  } catch (e) {243    t.pass()244  }245 246  try {247    fastify.get('/foo/3', [])248    t.fail()249  } catch (e) {250    t.pass()251  }252 253  try {254    fastify.get('/foo/4', undefined)255    t.fail()256  } catch (e) {257    t.pass()258  }259 260  try {261    fastify.get('/foo/5', null)262    t.fail()263  } catch (e) {264    t.pass()265  }266})267 268test('Should throw if handler as the third parameter to the shortcut method is missing and the second parameter is not a function and also not an object', t => {269  t.plan(5)270 271  const fastify = Fastify()272 273  try {274    fastify.get('/foo/1', '')275    t.fail()276  } catch (e) {277    t.pass()278  }279 280  try {281    fastify.get('/foo/2', 1)282    t.fail()283  } catch (e) {284    t.pass()285  }286 287  try {288    fastify.get('/foo/3', [])289    t.fail()290  } catch (e) {291    t.pass()292  }293 294  try {295    fastify.get('/foo/4', undefined)296    t.fail()297  } catch (e) {298    t.pass()299  }300 301  try {302    fastify.get('/foo/5', null)303    t.fail()304  } catch (e) {305    t.pass()306  }307})308 309test('Should throw if there is handler function as the third parameter to the shortcut method and options as the second parameter is not an object', t => {310  t.plan(5)311 312  const fastify = Fastify()313 314  try {315    fastify.get('/foo/1', '', (req, res) => {})316    t.fail()317  } catch (e) {318    t.pass()319  }320 321  try {322    fastify.get('/foo/2', 1, (req, res) => {})323    t.fail()324  } catch (e) {325    t.pass()326  }327 328  try {329    fastify.get('/foo/3', [], (req, res) => {})330    t.fail()331  } catch (e) {332    t.pass()333  }334 335  try {336    fastify.get('/foo/4', undefined, (req, res) => {})337    t.fail()338  } catch (e) {339    t.pass()340  }341 342  try {343    fastify.get('/foo/5', null, (req, res) => {})344    t.fail()345  } catch (e) {346    t.pass()347  }348})349 350test('Should throw if found duplicate handler as the third parameter to the shortcut method and in options', t => {351  t.plan(1)352 353  const fastify = Fastify()354 355  try {356    fastify.get('/foo/abc', {357      handler: (req, res) => {}358    }, (req, res) => {})359    t.fail()360  } catch (e) {361    t.pass()362  }363})364