CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
check.test.js224 linesDownload Raw Back to test
1'use strict'2 3const { test } = require('node:test')4const { S } = require('fluent-json-schema')5const Fastify = require('..')6const sget = require('simple-get').concat7 8const BadRequestSchema = S.object()9  .prop('statusCode', S.number())10  .prop('error', S.string())11  .prop('message', S.string())12 13const InternalServerErrorSchema = S.object()14  .prop('statusCode', S.number())15  .prop('error', S.string())16  .prop('message', S.string())17 18const NotFoundSchema = S.object()19  .prop('statusCode', S.number())20  .prop('error', S.string())21  .prop('message', S.string())22 23const options = {24  schema: {25    body: {26      type: 'object',27      properties: {28        id: { type: 'string' }29      }30    },31    response: {32      200: {33        type: 'object',34        properties: {35          id: { type: 'string' }36        }37      },38      400: {39        description: 'Bad Request',40        content: {41          'application/json': {42            schema: BadRequestSchema.valueOf()43          }44        }45      },46      404: {47        description: 'Resource not found',48        content: {49          'application/json': {50            schema: NotFoundSchema.valueOf(),51            example: {52              statusCode: 404,53              error: 'Not Found',54              message: 'Not Found'55            }56          }57        }58      },59      500: {60        description: 'Internal Server Error',61        content: {62          'application/json': {63            schema: InternalServerErrorSchema.valueOf(),64            example: {65              message: 'Internal Server Error'66            }67          }68        }69      }70    }71  }72}73 74const handler = (request, reply) => {75  if (request.body.id === '400') {76    return reply.status(400).send({77      statusCode: 400,78      error: 'Bad Request',79      message: 'Custom message',80      extra: 'This should not be in the response'81    })82  }83 84  if (request.body.id === '404') {85    return reply.status(404).send({86      statusCode: 404,87      error: 'Not Found',88      message: 'Custom Not Found',89      extra: 'This should not be in the response'90    })91  }92 93  if (request.body.id === '500') {94    reply.status(500).send({95      statusCode: 500,96      error: 'Internal Server Error',97      message: 'Custom Internal Server Error',98      extra: 'This should not be in the response'99    })100  }101 102  reply.send({103    id: request.body.id,104    extra: 'This should not be in the response'105  })106}107 108test('serialize the response for a Bad Request error, as defined on the schema', (t, done) => {109  const fastify = Fastify({})110 111  t.after(() => fastify.close())112 113  fastify.post('/', options, handler)114 115  fastify.listen({ port: 0 }, err => {116    t.assert.ifError(err)117 118    const url = `http://localhost:${fastify.server.address().port}/`119 120    sget({121      method: 'POST',122      url,123      json: true124    }, (err, response, body) => {125      t.assert.ifError(err)126      t.assert.strictEqual(response.statusCode, 400)127      t.assert.deepStrictEqual(body, {128        statusCode: 400,129        error: 'Bad Request',130        message: 'body must be object'131      })132      done()133    })134  })135})136 137// test('serialize the response for a Not Found error, as defined on the schema', t => {138//   const fastify = Fastify({})139 140//   t.teardown(fastify.close.bind(fastify))141 142//   fastify.post('/', options, handler)143 144//   fastify.listen({ port: 0 }, err => {145//     t.error(err)146 147//     const url = `http://localhost:${fastify.server.address().port}/`148 149//     sget({150//       method: 'POST',151//       url,152//       json: true,153//       body: { id: '404' }154//     }, (err, response, body) => {155//       t.error(err)156//       t.equal(response.statusCode, 404)157//       t.same(body, {158//         statusCode: 404,159//         error: 'Not Found',160//         message: 'Custom Not Found'161//       })162//       t.end()163//     })164//   })165// })166 167// test('serialize the response for a Internal Server Error error, as defined on the schema', t => {168//   const fastify = Fastify({})169 170//   t.teardown(fastify.close.bind(fastify))171 172//   fastify.post('/', options, handler)173 174//   fastify.listen({ port: 0 }, err => {175//     t.error(err)176 177//     const url = `http://localhost:${fastify.server.address().port}/`178 179//     sget({180//       method: 'POST',181//       url,182//       json: true,183//       body: { id: '500' }184//     }, (err, response, body) => {185//       t.error(err)186//       t.equal(response.statusCode, 500)187//       t.same(body, {188//         statusCode: 500,189//         error: 'Internal Server Error',190//         message: 'Custom Internal Server Error'191//       })192//       t.end()193//     })194//   })195// })196 197// test('serialize the success response, as defined on the schema', t => {198//   const fastify = Fastify({})199 200//   t.teardown(fastify.close.bind(fastify))201 202//   fastify.post('/', options, handler)203 204//   fastify.listen({ port: 0 }, err => {205//     t.error(err)206 207//     const url = `http://localhost:${fastify.server.address().port}/`208 209//     sget({210//       method: 'POST',211//       url,212//       json: true,213//       body: { id: 'test' }214//     }, (err, response, body) => {215//       t.error(err)216//       t.equal(response.statusCode, 200)217//       t.same(body, {218//         id: 'test'219//       })220//       t.end()221//     })222//   })223// })224