|
| 1 | +/** |
| 2 | + * Module dependencies. |
| 3 | + * |
| 4 | + * Unit tests for express.js initModulesServerRoutes — content-negotiated 404 |
| 5 | + * for unmatched routes (#3975). Unknown routes must no longer return an |
| 6 | + * implicit 200 HTML page; API paths and explicit JSON accepts must get a |
| 7 | + * JSON 404, everything else a minimal HTML 404, and root behavior must be |
| 8 | + * unchanged. |
| 9 | + */ |
| 10 | +import { jest, describe, test, expect, beforeEach } from '@jest/globals'; |
| 11 | +import express from 'express'; |
| 12 | +import request from 'supertest'; |
| 13 | + |
| 14 | +describe('express initModulesServerRoutes — content-negotiated 404 (#3975):', () => { |
| 15 | + beforeEach(() => { |
| 16 | + jest.resetModules(); |
| 17 | + }); |
| 18 | + |
| 19 | + /** |
| 20 | + * Helper: extract initModulesServerRoutes from express.js (with all heavy |
| 21 | + * deps mocked) and mount it on a fresh Express app. |
| 22 | + * @returns {Promise<import('express').Express>} A ready-to-request Express app |
| 23 | + */ |
| 24 | + const getApp = async () => { |
| 25 | + jest.unstable_mockModule('../../../config/index.js', () => ({ |
| 26 | + default: { |
| 27 | + domain: 'http://localhost:3000', |
| 28 | + app: { title: 'Test', description: '', keywords: '', url: '', logo: '' }, |
| 29 | + secure: { ssl: false }, |
| 30 | + log: {}, |
| 31 | + bodyParser: {}, |
| 32 | + csrf: {}, |
| 33 | + cors: { origin: [], credentials: false, optionsSuccessStatus: 200 }, |
| 34 | + trust: { proxy: false }, |
| 35 | + openapi: { enable: false }, |
| 36 | + files: { routes: [], configs: [], policies: [], preRoutes: [], openapi: [], guides: [] }, |
| 37 | + analytics: { posthog: { autoCapture: false } }, |
| 38 | + docs: {}, |
| 39 | + }, |
| 40 | + })); |
| 41 | + jest.unstable_mockModule('../../../lib/services/logger.js', () => ({ |
| 42 | + default: { |
| 43 | + warn: jest.fn(), |
| 44 | + error: jest.fn(), |
| 45 | + info: jest.fn(), |
| 46 | + debug: jest.fn(), |
| 47 | + getLogFormat: jest.fn().mockReturnValue('combined'), |
| 48 | + getMorganOptions: jest.fn().mockReturnValue({}), |
| 49 | + }, |
| 50 | + })); |
| 51 | + jest.unstable_mockModule('../../../lib/helpers/guides.js', () => ({ |
| 52 | + default: { loadGuides: jest.fn().mockReturnValue([]), mergeGuidesIntoSpec: jest.fn() }, |
| 53 | + })); |
| 54 | + jest.unstable_mockModule('../../../lib/middlewares/requestId.js', () => ({ |
| 55 | + default: jest.fn((req, res, next) => next()), |
| 56 | + })); |
| 57 | + jest.unstable_mockModule('../../../lib/middlewares/posthog-context.middleware.js', () => ({ |
| 58 | + posthogContextMiddleware: jest.fn((req, res, next) => next()), |
| 59 | + })); |
| 60 | + jest.unstable_mockModule('../../../lib/services/errorTracker.js', () => ({ |
| 61 | + default: { setupExpressErrorHandler: jest.fn() }, |
| 62 | + })); |
| 63 | + jest.unstable_mockModule('../../../lib/services/analytics.js', () => ({ |
| 64 | + default: { init: jest.fn().mockResolvedValue(undefined), identify: jest.fn(), groupIdentify: jest.fn() }, |
| 65 | + })); |
| 66 | + jest.unstable_mockModule('../../../lib/middlewares/analytics.js', () => ({ |
| 67 | + default: jest.fn((req, res, next) => next()), |
| 68 | + })); |
| 69 | + jest.unstable_mockModule('../../../lib/middlewares/policy.js', () => ({ |
| 70 | + default: { discoverPolicies: jest.fn().mockResolvedValue(undefined), defineAbilityFor: jest.fn().mockResolvedValue({}) }, |
| 71 | + })); |
| 72 | + |
| 73 | + const mod = await import('../../../lib/services/express.js'); |
| 74 | + const app = express(); |
| 75 | + await mod.default.initModulesServerRoutes(app); |
| 76 | + return app; |
| 77 | + }; |
| 78 | + |
| 79 | + test('GET /api/nope → 404 JSON { error: "not_found" }', async () => { |
| 80 | + const app = await getApp(); |
| 81 | + |
| 82 | + const res = await request(app).get('/api/nope').expect(404); |
| 83 | + |
| 84 | + expect(res.headers['content-type']).toMatch(/json/); |
| 85 | + expect(res.body).toEqual({ error: 'not_found' }); |
| 86 | + }); |
| 87 | + |
| 88 | + test('GET /api (no trailing slash) → 404 JSON', async () => { |
| 89 | + const app = await getApp(); |
| 90 | + |
| 91 | + const res = await request(app).get('/api').expect(404); |
| 92 | + |
| 93 | + expect(res.headers['content-type']).toMatch(/json/); |
| 94 | + expect(res.body).toEqual({ error: 'not_found' }); |
| 95 | + }); |
| 96 | + |
| 97 | + test('GET /nope with Accept: text/html → 404 HTML, no JSON', async () => { |
| 98 | + const app = await getApp(); |
| 99 | + |
| 100 | + const res = await request(app).get('/nope').set('Accept', 'text/html').expect(404); |
| 101 | + |
| 102 | + expect(res.headers['content-type']).toMatch(/html/); |
| 103 | + expect(res.text).toContain('404'); |
| 104 | + }); |
| 105 | + |
| 106 | + test('GET /nope with Accept: application/json → 404 JSON { error: "not_found" }', async () => { |
| 107 | + const app = await getApp(); |
| 108 | + |
| 109 | + const res = await request(app).get('/nope').set('Accept', 'application/json').expect(404); |
| 110 | + |
| 111 | + expect(res.headers['content-type']).toMatch(/json/); |
| 112 | + expect(res.body).toEqual({ error: 'not_found' }); |
| 113 | + }); |
| 114 | + |
| 115 | + test('GET / → 200, friendly root page unchanged', async () => { |
| 116 | + const app = await getApp(); |
| 117 | + |
| 118 | + const res = await request(app).get('/').expect(200); |
| 119 | + |
| 120 | + expect(res.text).toContain('Devkit Node Api'); |
| 121 | + }); |
| 122 | +}); |
0 commit comments