|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright 2026 Aaron K. Clark |
| 3 | +// |
| 4 | +// Tests for the global error handler + 404 fallthrough. |
| 5 | + |
| 6 | +import { describe, test, expect, vi, beforeAll } from 'vitest'; |
| 7 | +import request from 'supertest'; |
| 8 | +import express from 'express'; |
| 9 | +import { errorHandler, notFound } from '../../app/middleware/error-handler.js'; |
| 10 | + |
| 11 | +let app; |
| 12 | + |
| 13 | +beforeAll(() => { |
| 14 | + app = express(); |
| 15 | + app.use(express.json()); |
| 16 | + |
| 17 | + // Routes that intentionally throw / next(err) so we can exercise |
| 18 | + // the error handler from inside a test, without depending on the |
| 19 | + // whole router. |
| 20 | + app.get('/explode/500', (req, res, next) => { |
| 21 | + next(new Error('boom')); |
| 22 | + }); |
| 23 | + app.get('/explode/with-status', (req, res, next) => { |
| 24 | + const err = new Error('I am a teapot'); |
| 25 | + err.status = 418; |
| 26 | + next(err); |
| 27 | + }); |
| 28 | + app.get('/explode/leaky', (req, res, next) => { |
| 29 | + // Simulates a Sequelize-style error whose .message would leak |
| 30 | + // a hostname or stack frame if we passed it straight through. |
| 31 | + const err = new Error('SequelizeConnectionRefusedError: connect ECONNREFUSED 10.0.0.42:5432'); |
| 32 | + err.status = 500; |
| 33 | + next(err); |
| 34 | + }); |
| 35 | + app.use(notFound); |
| 36 | + app.use(errorHandler); |
| 37 | +}); |
| 38 | + |
| 39 | +describe('global error handler', () => { |
| 40 | + test('500 errors return JSON {message: "Error!"} not HTML', async () => { |
| 41 | + const res = await request(app).get('/explode/500'); |
| 42 | + expect(res.status).toBe(500); |
| 43 | + expect(res.headers['content-type']).toMatch(/application\/json/); |
| 44 | + expect(res.body.message).toBe('Error!'); |
| 45 | + }); |
| 46 | + |
| 47 | + test('500 errors never leak the original message (no stack info)', async () => { |
| 48 | + const res = await request(app).get('/explode/leaky'); |
| 49 | + const text = JSON.stringify(res.body); |
| 50 | + expect(text).not.toMatch(/ECONNREFUSED/); |
| 51 | + expect(text).not.toMatch(/10\.0\.0\.42/); |
| 52 | + expect(text).not.toMatch(/Sequelize/); |
| 53 | + }); |
| 54 | + |
| 55 | + test('honors a numeric err.status in 4xx range', async () => { |
| 56 | + const res = await request(app).get('/explode/with-status'); |
| 57 | + expect(res.status).toBe(418); |
| 58 | + // For 4xx the message goes through (it's a client error, not a server one) |
| 59 | + expect(res.body.message).toBe('I am a teapot'); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe('404 fallthrough', () => { |
| 64 | + test('unmatched route returns JSON 404 (not HTML)', async () => { |
| 65 | + const res = await request(app).get('/no/such/path'); |
| 66 | + expect(res.status).toBe(404); |
| 67 | + expect(res.headers['content-type']).toMatch(/application\/json/); |
| 68 | + expect(res.body.message).toMatch(/not found/i); |
| 69 | + expect(res.body.path).toBe('/no/such/path'); |
| 70 | + expect(res.body.method).toBe('GET'); |
| 71 | + }); |
| 72 | + |
| 73 | + test('unmatched method on a known path returns 404 (Express convention)', async () => { |
| 74 | + // We don't have an OPTIONS handler so this should fall to notFound. |
| 75 | + const res = await request(app).post('/explode/500'); |
| 76 | + expect(res.status).toBe(404); |
| 77 | + }); |
| 78 | +}); |
0 commit comments