|
| 1 | +import { describe, expect, it } from '@jest/globals'; |
| 2 | +import { |
| 3 | + boundedRetryAfterMs, |
| 4 | + isPublicErrorCode, |
| 5 | + normalizeError, |
| 6 | + normalizePublicCode, |
| 7 | + publicCodeForStatus, |
| 8 | + safePublicMessage, |
| 9 | + safeValidationDetails, |
| 10 | +} from './errorEnvelopePolicy.js'; |
| 11 | + |
| 12 | +describe('error envelope policy', () => { |
| 13 | + it('maps all supported HTTP statuses to stable public codes', () => { |
| 14 | + const expected: Array<[number, string]> = [ |
| 15 | + [400, 'BAD_REQUEST'], [401, 'UNAUTHORIZED'], [402, 'PAYMENT_REQUIRED'], |
| 16 | + [403, 'FORBIDDEN'], [404, 'NOT_FOUND'], [408, 'REQUEST_TIMEOUT'], |
| 17 | + [409, 'CONFLICT'], [413, 'REQUEST_BODY_TOO_LARGE'], [415, 'UNSUPPORTED_MEDIA_TYPE'], |
| 18 | + [422, 'UNPROCESSABLE_ENTITY'], [429, 'TOO_MANY_REQUESTS'], [500, 'INTERNAL_SERVER_ERROR'], |
| 19 | + [502, 'BAD_GATEWAY'], [503, 'SERVICE_UNAVAILABLE'], [504, 'GATEWAY_TIMEOUT'], |
| 20 | + ]; |
| 21 | + for (const [status, code] of expected) expect(publicCodeForStatus(status)).toBe(code); |
| 22 | + }); |
| 23 | + |
| 24 | + it('uses safe fallback codes for unknown statuses and invalid supplied codes', () => { |
| 25 | + expect(publicCodeForStatus(501)).toBe('INTERNAL_SERVER_ERROR'); |
| 26 | + expect(publicCodeForStatus(418)).toBe('BAD_REQUEST'); |
| 27 | + expect(normalizePublicCode('NOT_A_PUBLIC_CODE', 502)).toBe('BAD_GATEWAY'); |
| 28 | + expect(normalizePublicCode('CONFLICT', 500)).toBe('CONFLICT'); |
| 29 | + expect(isPublicErrorCode('NOT_FOUND')).toBe(true); |
| 30 | + expect(isPublicErrorCode('database_error')).toBe(false); |
| 31 | + }); |
| 32 | + |
| 33 | + it('preserves trusted application messages', () => { |
| 34 | + expect(safePublicMessage('Wallet is suspended', 403, true)).toBe('Wallet is suspended'); |
| 35 | + expect(safePublicMessage('', 403, true)).toBe('Request failed'); |
| 36 | + expect(safePublicMessage('body too large', 413, true)).toBe('Request body too large'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('hides unknown production failures and sensitive development messages', () => { |
| 40 | + expect(safePublicMessage('database password=secret', 500, false, true)).toBe('Internal server error'); |
| 41 | + expect(safePublicMessage('connection string postgres://...', 502, false, true)).toBe('Internal server error'); |
| 42 | + expect(safePublicMessage('upstream unavailable', 502, false, false)).toBe('Internal server error'); |
| 43 | + expect(safePublicMessage('bad input', 400, false, true)).toBe('bad input'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('normalizes and bounds validation details', () => { |
| 47 | + expect(safeValidationDetails([ |
| 48 | + { field: 'body.email', message: 'Invalid email', code: 'INVALID_FORMAT' }, |
| 49 | + { field: 4, message: 'ignored', code: 'BAD' }, |
| 50 | + null, |
| 51 | + ])).toEqual([{ field: 'body.email', message: 'Invalid email', code: 'INVALID_FORMAT' }]); |
| 52 | + expect(safeValidationDetails([])).toBeUndefined(); |
| 53 | + expect(safeValidationDetails('not-details')).toBeUndefined(); |
| 54 | + expect(safeValidationDetails([{ field: 'x', message: 'y' }])).toBeUndefined(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('bounds untrusted retry metadata to one day', () => { |
| 58 | + expect(boundedRetryAfterMs(0)).toBe(0); |
| 59 | + expect(boundedRetryAfterMs(12.9)).toBe(12); |
| 60 | + expect(boundedRetryAfterMs(999_999_999)).toBe(86_400_000); |
| 61 | + expect(boundedRetryAfterMs(-1)).toBeUndefined(); |
| 62 | + expect(boundedRetryAfterMs(Number.NaN)).toBeUndefined(); |
| 63 | + expect(boundedRetryAfterMs('100')).toBeUndefined(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('normalizes trusted validation errors into the versioned contract', () => { |
| 67 | + expect(normalizeError({ |
| 68 | + statusCode: 422, |
| 69 | + code: 'VALIDATION_ERROR', |
| 70 | + message: 'Request validation failed', |
| 71 | + details: [{ field: 'body.amount', message: 'Required', code: 'INVALID_TYPE' }], |
| 72 | + trusted: true, |
| 73 | + })).toEqual({ |
| 74 | + statusCode: 422, |
| 75 | + code: 'VALIDATION_ERROR', |
| 76 | + message: 'Request validation failed', |
| 77 | + details: [{ field: 'body.amount', message: 'Required', code: 'INVALID_TYPE' }], |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('normalizes rate-limit metadata without exposing unsupported fields', () => { |
| 82 | + expect(normalizeError({ statusCode: 429, message: 'Too many requests', retryAfterMs: 5_000, trusted: true })).toEqual({ |
| 83 | + statusCode: 429, |
| 84 | + code: 'TOO_MANY_REQUESTS', |
| 85 | + message: 'Too many requests', |
| 86 | + retryAfterMs: 5_000, |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('uses generic messages for unknown client errors in production', () => { |
| 91 | + const result = normalizeError({ statusCode: 400, message: 'private internal detail', trusted: false, development: false }); |
| 92 | + expect(result).toMatchObject({ code: 'BAD_REQUEST', message: 'Request failed' }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('keeps safe developer diagnostics only for non-sensitive client failures', () => { |
| 96 | + expect(normalizeError({ statusCode: 400, message: 'field x is invalid', trusted: false, development: true }).message).toBe('field x is invalid'); |
| 97 | + expect(normalizeError({ statusCode: 500, message: 'field x is invalid', trusted: false, development: true }).message).toBe('Internal server error'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('truncates oversized validation values', () => { |
| 101 | + const result = safeValidationDetails([{ field: 'f'.repeat(500), message: 'm'.repeat(800), code: 'c'.repeat(200) }]); |
| 102 | + expect(result?.[0].field).toHaveLength(200); |
| 103 | + expect(result?.[0].message).toHaveLength(500); |
| 104 | + expect(result?.[0].code).toHaveLength(100); |
| 105 | + }); |
| 106 | +}); |
0 commit comments