CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
promises.test.js141 linesDownload Raw Back to test
1'use strict'2 3const t = require('tap')4const test = t.test5const sget = require('simple-get').concat6const fastify = require('..')()7 8const opts = {9  schema: {10    response: {11      '2xx': {12        type: 'object',13        properties: {14          hello: {15            type: 'string'16          }17        }18      }19    }20  }21}22 23fastify.get('/return', opts, function (req, reply) {24  const promise = new Promise((resolve, reject) => {25    resolve({ hello: 'world' })26  })27  return promise28})29 30fastify.get('/return-error', opts, function (req, reply) {31  const promise = new Promise((resolve, reject) => {32    reject(new Error('some error'))33  })34  return promise35})36 37fastify.get('/double', function (req, reply) {38  setTimeout(function () {39    // this should not throw40    reply.send({ hello: 'world' })41  }, 20)42  return Promise.resolve({ hello: '42' })43})44 45fastify.get('/thenable', opts, function (req, reply) {46  setImmediate(function () {47    reply.send({ hello: 'world' })48  })49  return reply50})51 52fastify.get('/thenable-error', opts, function (req, reply) {53  setImmediate(function () {54    reply.send(new Error('kaboom'))55  })56  return reply57})58 59fastify.get('/return-reply', opts, function (req, reply) {60  return reply.send({ hello: 'world' })61})62 63fastify.listen({ port: 0 }, err => {64  t.error(err)65  t.teardown(() => { fastify.close() })66 67  test('shorthand - sget return promise es6 get', t => {68    t.plan(4)69    sget({70      method: 'GET',71      url: 'http://localhost:' + fastify.server.address().port + '/return'72    }, (err, response, body) => {73      t.error(err)74      t.equal(response.statusCode, 200)75      t.equal(response.headers['content-length'], '' + body.length)76      t.same(JSON.parse(body), { hello: 'world' })77    })78  })79 80  test('shorthand - sget promise es6 get return error', t => {81    t.plan(2)82    sget({83      method: 'GET',84      url: 'http://localhost:' + fastify.server.address().port + '/return-error'85    }, (err, response, body) => {86      t.error(err)87      t.equal(response.statusCode, 500)88    })89  })90 91  test('sget promise double send', t => {92    t.plan(3)93 94    sget({95      method: 'GET',96      url: 'http://localhost:' + fastify.server.address().port + '/double'97    }, (err, response, body) => {98      t.error(err)99      t.equal(response.statusCode, 200)100      t.same(JSON.parse(body), { hello: '42' })101    })102  })103 104  test('thenable', t => {105    t.plan(4)106    sget({107      method: 'GET',108      url: 'http://localhost:' + fastify.server.address().port + '/thenable'109    }, (err, response, body) => {110      t.error(err)111      t.equal(response.statusCode, 200)112      t.equal(response.headers['content-length'], '' + body.length)113      t.same(JSON.parse(body), { hello: 'world' })114    })115  })116 117  test('thenable (error)', t => {118    t.plan(2)119    sget({120      method: 'GET',121      url: 'http://localhost:' + fastify.server.address().port + '/thenable-error'122    }, (err, response, body) => {123      t.error(err)124      t.equal(response.statusCode, 500)125    })126  })127 128  test('return-reply', t => {129    t.plan(4)130    sget({131      method: 'GET',132      url: 'http://localhost:' + fastify.server.address().port + '/return-reply'133    }, (err, response, body) => {134      t.error(err)135      t.equal(response.statusCode, 200)136      t.equal(response.headers['content-length'], '' + body.length)137      t.same(JSON.parse(body), { hello: 'world' })138    })139  })140})141