|
1 | 1 | /* eslint-disable @typescript-eslint/no-floating-promises */ |
2 | 2 |
|
3 | | -import fastify, { FastifyInstance, FastifyPluginOptions } from 'fastify' |
| 3 | +import fastify, { FastifyInstance, FastifyPluginOptions, FastifyReply, FastifyRequest } from 'fastify' |
4 | 4 | import { |
5 | 5 | BadGatewayError, |
6 | 6 | BAD_GATEWAY, |
@@ -332,6 +332,40 @@ t.test('Plugin', t => { |
332 | 332 | } |
333 | 333 | ) |
334 | 334 |
|
| 335 | + t.test('should correctly install a not found error handler', async t => { |
| 336 | + const server = await buildServer() |
| 337 | + |
| 338 | + const response = await server.inject({ |
| 339 | + method: 'POST', |
| 340 | + url: '/not-found', |
| 341 | + headers: { 'content-type': 'image/png' } |
| 342 | + }) |
| 343 | + |
| 344 | + t.equal(response.statusCode, NOT_FOUND) |
| 345 | + t.same(JSON.parse(response.payload), { |
| 346 | + statusCode: 404, |
| 347 | + error: 'Not Found', |
| 348 | + message: 'Not found.' |
| 349 | + }) |
| 350 | + }) |
| 351 | + |
| 352 | + t.test('should correctly skip installing a not found error handler', async t => { |
| 353 | + const server = await buildServer({ handle404Errors: false }) |
| 354 | + |
| 355 | + server.setNotFoundHandler((_: FastifyRequest, reply: FastifyReply) => { |
| 356 | + reply.code(444).send('NOT FOUND') |
| 357 | + }) |
| 358 | + |
| 359 | + const response = await server.inject({ |
| 360 | + method: 'POST', |
| 361 | + url: '/not-found', |
| 362 | + headers: { 'content-type': 'image/png' } |
| 363 | + }) |
| 364 | + |
| 365 | + t.equal(response.statusCode, 444) |
| 366 | + t.same(response.payload, 'NOT FOUND') |
| 367 | + }) |
| 368 | + |
335 | 369 | t.test('should correctly parse invalid content type errors', async t => { |
336 | 370 | const server = await buildServer() |
337 | 371 |
|
|
0 commit comments