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