strong-tie/inbound-calls
0
1'use strict'2 3const { test } = require('node:test')4const { S } = require('fluent-json-schema')5const Fastify = require('../fastify')6const sjson = require('secure-json-parse')7 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', async t => {109 t.plan(2)110 111 const fastify = Fastify({})112 113 fastify.post('/', options, handler)114 const response = await fastify.inject({115 method: 'POST',116 url: '/'117 })118 119 t.assert.strictEqual(response.statusCode, 400)120 t.assert.deepStrictEqual(sjson(response.body), {121 statusCode: 400,122 error: 'Bad Request',123 message: 'body must be object'124 })125})126 127test('serialize the response for a Not Found error, as defined on the schema', async t => {128 t.plan(2)129 130 const fastify = Fastify({})131 132 fastify.post('/', options, handler)133 134 const response = await fastify.inject({135 method: 'POST',136 url: '/',137 body: { id: '404' }138 })139 140 t.assert.strictEqual(response.statusCode, 404)141 t.assert.deepStrictEqual(sjson(response.body), {142 statusCode: 404,143 error: 'Not Found',144 message: 'Custom Not Found'145 })146})147 148test('serialize the response for a Internal Server Error error, as defined on the schema', async t => {149 t.plan(2)150 151 const fastify = Fastify({})152 153 fastify.post('/', options, handler)154 155 const response = await fastify.inject({156 method: 'POST',157 url: '/',158 body: { id: '500' }159 })160 161 t.assert.strictEqual(response.statusCode, 500)162 t.assert.deepStrictEqual(sjson(response.body), {163 statusCode: 500,164 error: 'Internal Server Error',165 message: 'Custom Internal Server Error'166 })167})168 169test('serialize the success response, as defined on the schema', async t => {170 t.plan(2)171 172 const fastify = Fastify({})173 174 fastify.post('/', options, handler)175 176 const response = await fastify.inject({177 method: 'POST',178 url: '/',179 body: { id: 'test' }180 })181 182 t.assert.strictEqual(response.statusCode, 200)183 t.assert.deepStrictEqual(sjson(response.body), {184 id: 'test'185 })186})187 