|
| 1 | +/** |
| 2 | + * body-size-limit.middleware resolves its per-group overrides from envConfig |
| 3 | + * once, at module load — matching how envConfig itself is a one-time |
| 4 | + * envSchema.parse(process.env) snapshot. Each test that needs a different |
| 5 | + * envConfig shape therefore mocks '../config' and re-imports the module |
| 6 | + * under test fresh via jest.resetModules(), rather than mutating envConfig |
| 7 | + * after the fact (which the real module never observes, by design). |
| 8 | + */ |
| 9 | +function loadWithEnvConfig(envConfig: { |
| 10 | + BODY_SIZE_LIMIT_DEFAULT: string; |
| 11 | + BODY_SIZE_LIMIT_AUTH?: string; |
| 12 | + BODY_SIZE_LIMIT_ADMIN?: string; |
| 13 | + BODY_SIZE_LIMIT_CREATORS?: string; |
| 14 | +}) { |
| 15 | + jest.resetModules(); |
| 16 | + jest.doMock('../config', () => ({ envConfig })); |
| 17 | + |
| 18 | + return require('./body-size-limit.middleware') as typeof import('./body-size-limit.middleware'); |
| 19 | +} |
| 20 | + |
| 21 | +describe('getBodySizeLimit', () => { |
| 22 | + afterEach(() => { |
| 23 | + jest.dontMock('../config'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('returns BODY_SIZE_LIMIT_DEFAULT for the "default" group', () => { |
| 27 | + const { getBodySizeLimit } = loadWithEnvConfig({ |
| 28 | + BODY_SIZE_LIMIT_DEFAULT: '10mb', |
| 29 | + }); |
| 30 | + expect(getBodySizeLimit('default')).toBe('10mb'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('falls back to the default for a group with no override configured', () => { |
| 34 | + const { getBodySizeLimit } = loadWithEnvConfig({ |
| 35 | + BODY_SIZE_LIMIT_DEFAULT: '10mb', |
| 36 | + }); |
| 37 | + expect(getBodySizeLimit('auth')).toBe('10mb'); |
| 38 | + expect(getBodySizeLimit('admin')).toBe('10mb'); |
| 39 | + expect(getBodySizeLimit('creators')).toBe('10mb'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('uses the group-specific override when configured', () => { |
| 43 | + const { getBodySizeLimit } = loadWithEnvConfig({ |
| 44 | + BODY_SIZE_LIMIT_DEFAULT: '10mb', |
| 45 | + BODY_SIZE_LIMIT_AUTH: '100kb', |
| 46 | + }); |
| 47 | + |
| 48 | + expect(getBodySizeLimit('auth')).toBe('100kb'); |
| 49 | + // Unrelated groups are unaffected. |
| 50 | + expect(getBodySizeLimit('admin')).toBe('10mb'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('supports a distinct override per group simultaneously', () => { |
| 54 | + const { getBodySizeLimit } = loadWithEnvConfig({ |
| 55 | + BODY_SIZE_LIMIT_DEFAULT: '10mb', |
| 56 | + BODY_SIZE_LIMIT_AUTH: '100kb', |
| 57 | + BODY_SIZE_LIMIT_ADMIN: '20mb', |
| 58 | + }); |
| 59 | + |
| 60 | + expect(getBodySizeLimit('auth')).toBe('100kb'); |
| 61 | + expect(getBodySizeLimit('admin')).toBe('20mb'); |
| 62 | + // creators has no override in this config, still falls back. |
| 63 | + expect(getBodySizeLimit('creators')).toBe('10mb'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('reflects a non-default BODY_SIZE_LIMIT_DEFAULT for groups with no override', () => { |
| 67 | + const { getBodySizeLimit } = loadWithEnvConfig({ |
| 68 | + BODY_SIZE_LIMIT_DEFAULT: '5mb', |
| 69 | + }); |
| 70 | + |
| 71 | + expect(getBodySizeLimit('default')).toBe('5mb'); |
| 72 | + expect(getBodySizeLimit('admin')).toBe('5mb'); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +describe('routeBodySizeLimit', () => { |
| 77 | + afterEach(() => { |
| 78 | + jest.dontMock('../config'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('returns an express.json middleware function', () => { |
| 82 | + const { routeBodySizeLimit } = loadWithEnvConfig({ |
| 83 | + BODY_SIZE_LIMIT_DEFAULT: '10mb', |
| 84 | + }); |
| 85 | + const middleware = routeBodySizeLimit('default'); |
| 86 | + expect(typeof middleware).toBe('function'); |
| 87 | + // express.json() returns a function with this arity: (req, res, next) |
| 88 | + expect(middleware.length).toBe(3); |
| 89 | + }); |
| 90 | + |
| 91 | + it('produces a distinct middleware instance per call (no shared limit state)', () => { |
| 92 | + const { routeBodySizeLimit } = loadWithEnvConfig({ |
| 93 | + BODY_SIZE_LIMIT_DEFAULT: '10mb', |
| 94 | + BODY_SIZE_LIMIT_AUTH: '100kb', |
| 95 | + BODY_SIZE_LIMIT_ADMIN: '20mb', |
| 96 | + }); |
| 97 | + const first = routeBodySizeLimit('auth'); |
| 98 | + const second = routeBodySizeLimit('admin'); |
| 99 | + expect(first).not.toBe(second); |
| 100 | + }); |
| 101 | +}); |
0 commit comments