|
| 1 | +import { Request, Response, NextFunction } from 'express'; |
| 2 | +import { bodyParseErrorMiddleware } from './body-parse-error.middleware'; |
| 3 | +import { logger } from '../utils/logger.utils'; |
| 4 | + |
| 5 | +jest.mock('../utils/logger.utils', () => ({ |
| 6 | + logger: { error: jest.fn() }, |
| 7 | +})); |
| 8 | + |
| 9 | +jest.mock('../utils/client-ip.utils', () => ({ |
| 10 | + getClientIp: jest.fn(() => '10.0.0.1'), |
| 11 | +})); |
| 12 | + |
| 13 | +function makeReq(method: string, url = '/api/v1/resource'): Request { |
| 14 | + return { |
| 15 | + method, |
| 16 | + originalUrl: url, |
| 17 | + url, |
| 18 | + requestId: 'req-test-id', |
| 19 | + socket: { remoteAddress: '127.0.0.1' }, |
| 20 | + headers: {}, |
| 21 | + } as unknown as Request; |
| 22 | +} |
| 23 | + |
| 24 | +function makeRes(): Partial<Response> { |
| 25 | + const res: Partial<Response> = {}; |
| 26 | + res.status = jest.fn().mockReturnValue(res); |
| 27 | + res.json = jest.fn().mockReturnValue(res); |
| 28 | + return res; |
| 29 | +} |
| 30 | + |
| 31 | +function makeSyntaxError(): SyntaxError & { body: string } { |
| 32 | + const err = new SyntaxError('Unexpected token } in JSON') as SyntaxError & { body: string }; |
| 33 | + err.body = 'raw body'; |
| 34 | + return err; |
| 35 | +} |
| 36 | + |
| 37 | +function makeEntityTooLargeError() { |
| 38 | + return Object.assign(new Error('request entity too large'), { |
| 39 | + type: 'entity.too.large', |
| 40 | + status: 413, |
| 41 | + limit: 10 * 1024 * 1024, |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +describe('bodyParseErrorMiddleware', () => { |
| 46 | + let next: jest.Mock; |
| 47 | + |
| 48 | + beforeEach(() => { |
| 49 | + next = jest.fn(); |
| 50 | + jest.clearAllMocks(); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('malformed JSON on mutation methods', () => { |
| 54 | + const MUTATION_METHODS = ['POST', 'PUT', 'PATCH', 'DELETE']; |
| 55 | + |
| 56 | + it.each(MUTATION_METHODS)( |
| 57 | + 'logs error and returns 400 for %s with invalid JSON', |
| 58 | + (method) => { |
| 59 | + const err = makeSyntaxError(); |
| 60 | + const req = makeReq(method); |
| 61 | + const res = makeRes(); |
| 62 | + |
| 63 | + bodyParseErrorMiddleware(err, req, res as Response, next as NextFunction); |
| 64 | + |
| 65 | + expect(logger.error).toHaveBeenCalledWith( |
| 66 | + expect.objectContaining({ |
| 67 | + type: 'body_parse_failure', |
| 68 | + method, |
| 69 | + path: '/api/v1/resource', |
| 70 | + requestId: 'req-test-id', |
| 71 | + errorType: 'invalid_json', |
| 72 | + }) |
| 73 | + ); |
| 74 | + expect(res.status).toHaveBeenCalledWith(400); |
| 75 | + expect(res.json).toHaveBeenCalledWith( |
| 76 | + expect.objectContaining({ success: false, message: 'Invalid JSON in request body' }) |
| 77 | + ); |
| 78 | + expect(next).not.toHaveBeenCalled(); |
| 79 | + } |
| 80 | + ); |
| 81 | + |
| 82 | + it('does not include raw body in the log', () => { |
| 83 | + const err = makeSyntaxError(); |
| 84 | + const req = makeReq('POST'); |
| 85 | + const res = makeRes(); |
| 86 | + |
| 87 | + bodyParseErrorMiddleware(err, req, res as Response, next as NextFunction); |
| 88 | + |
| 89 | + const logCall = (logger.error as jest.Mock).mock.calls[0][0]; |
| 90 | + expect(logCall).not.toHaveProperty('body'); |
| 91 | + expect(logCall).not.toHaveProperty('rawBody'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('logs and returns 413 for entity.too.large on mutation', () => { |
| 95 | + const err = makeEntityTooLargeError(); |
| 96 | + const req = makeReq('POST'); |
| 97 | + const res = makeRes(); |
| 98 | + |
| 99 | + bodyParseErrorMiddleware(err, req, res as Response, next as NextFunction); |
| 100 | + |
| 101 | + expect(logger.error).toHaveBeenCalledWith( |
| 102 | + expect.objectContaining({ errorType: 'entity.too.large' }) |
| 103 | + ); |
| 104 | + expect(res.status).toHaveBeenCalledWith(413); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe('non-mutation methods', () => { |
| 109 | + it('calls next() for GET with a SyntaxError (not a mutation)', () => { |
| 110 | + const err = makeSyntaxError(); |
| 111 | + const req = makeReq('GET'); |
| 112 | + const res = makeRes(); |
| 113 | + |
| 114 | + bodyParseErrorMiddleware(err, req, res as Response, next as NextFunction); |
| 115 | + |
| 116 | + expect(next).toHaveBeenCalledWith(err); |
| 117 | + expect(logger.error).not.toHaveBeenCalled(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('calls next() for HEAD with a SyntaxError', () => { |
| 121 | + const err = makeSyntaxError(); |
| 122 | + const req = makeReq('HEAD'); |
| 123 | + const res = makeRes(); |
| 124 | + |
| 125 | + bodyParseErrorMiddleware(err, req, res as Response, next as NextFunction); |
| 126 | + |
| 127 | + expect(next).toHaveBeenCalledWith(err); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + describe('non-parse errors', () => { |
| 132 | + it('calls next() for a generic Error on a mutation method', () => { |
| 133 | + const err = new Error('Something else entirely'); |
| 134 | + const req = makeReq('POST'); |
| 135 | + const res = makeRes(); |
| 136 | + |
| 137 | + bodyParseErrorMiddleware(err, req, res as Response, next as NextFunction); |
| 138 | + |
| 139 | + expect(next).toHaveBeenCalledWith(err); |
| 140 | + expect(logger.error).not.toHaveBeenCalled(); |
| 141 | + }); |
| 142 | + |
| 143 | + it('calls next() for an ApiError on a mutation method', () => { |
| 144 | + const err = Object.assign(new Error('Not found'), { statusCode: 404 }); |
| 145 | + const req = makeReq('PUT'); |
| 146 | + const res = makeRes(); |
| 147 | + |
| 148 | + bodyParseErrorMiddleware(err, req, res as Response, next as NextFunction); |
| 149 | + |
| 150 | + expect(next).toHaveBeenCalledWith(err); |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + describe('client response behaviour unchanged', () => { |
| 155 | + it('returns 400 JSON with success:false and does not call next', () => { |
| 156 | + const err = makeSyntaxError(); |
| 157 | + const req = makeReq('POST'); |
| 158 | + const res = makeRes(); |
| 159 | + |
| 160 | + bodyParseErrorMiddleware(err, req, res as Response, next as NextFunction); |
| 161 | + |
| 162 | + expect(res.status).toHaveBeenCalledWith(400); |
| 163 | + expect((res.json as jest.Mock).mock.calls[0][0]).toMatchObject({ |
| 164 | + success: false, |
| 165 | + }); |
| 166 | + expect(next).not.toHaveBeenCalled(); |
| 167 | + }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments