|
| 1 | +const { cacheResponse } = require('./cache'); |
| 2 | +const { MemoryCacheStore } = require('../services/cacheStore'); |
| 3 | + |
| 4 | +/** |
| 5 | + * Creates a minimal mock response object for testing. |
| 6 | + * |
| 7 | + * @returns {object} Mock Express response. |
| 8 | + */ |
| 9 | +function createMockRes() { |
| 10 | + const res = { |
| 11 | + statusCode: 200, |
| 12 | + headers: {}, |
| 13 | + body: null, |
| 14 | + status(code) { |
| 15 | + res.statusCode = code; |
| 16 | + return res; |
| 17 | + }, |
| 18 | + json(data) { |
| 19 | + res.body = data; |
| 20 | + return res; |
| 21 | + }, |
| 22 | + set(name, value) { |
| 23 | + res.headers[name] = value; |
| 24 | + return res; |
| 25 | + }, |
| 26 | + }; |
| 27 | + return res; |
| 28 | +} |
| 29 | + |
| 30 | +describe('cacheResponse', () => { |
| 31 | + let store; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + store = new MemoryCacheStore(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('calls next on cache miss and caches the 2xx response', (done) => { |
| 38 | + const middleware = cacheResponse({ ttl: 5000, store }); |
| 39 | + const req = { originalUrl: '/api/escrow/123' }; |
| 40 | + const res = createMockRes(); |
| 41 | + |
| 42 | + middleware(req, res, () => { |
| 43 | + // Simulate handler sending response |
| 44 | + res.json({ data: 'from handler' }); |
| 45 | + |
| 46 | + expect(res.body).toEqual({ data: 'from handler' }); |
| 47 | + expect(res.headers['X-Cache']).toBe('MISS'); |
| 48 | + expect(store.get('/api/escrow/123')).toEqual({ data: 'from handler' }); |
| 49 | + done(); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('returns cached response on cache hit without calling next', () => { |
| 54 | + const middleware = cacheResponse({ ttl: 5000, store }); |
| 55 | + const req = { originalUrl: '/api/escrow/123' }; |
| 56 | + const res = createMockRes(); |
| 57 | + |
| 58 | + store.set('/api/escrow/123', { data: 'cached' }, 5000); |
| 59 | + |
| 60 | + let nextCalled = false; |
| 61 | + middleware(req, res, () => { nextCalled = true; }); |
| 62 | + |
| 63 | + expect(nextCalled).toBe(false); |
| 64 | + expect(res.body).toEqual({ data: 'cached' }); |
| 65 | + expect(res.headers['X-Cache']).toBe('HIT'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('does not cache non-2xx responses', (done) => { |
| 69 | + const middleware = cacheResponse({ ttl: 5000, store }); |
| 70 | + const req = { originalUrl: '/api/escrow/bad' }; |
| 71 | + const res = createMockRes(); |
| 72 | + |
| 73 | + middleware(req, res, () => { |
| 74 | + res.status(500).json({ error: 'fail' }); |
| 75 | + |
| 76 | + expect(res.body).toEqual({ error: 'fail' }); |
| 77 | + expect(store.get('/api/escrow/bad')).toBeUndefined(); |
| 78 | + done(); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('uses custom keyFn to generate cache key', (done) => { |
| 83 | + const keyFn = (r) => `custom:${r.params.id}`; |
| 84 | + const middleware = cacheResponse({ ttl: 5000, store, keyFn }); |
| 85 | + const req = { originalUrl: '/api/escrow/456', params: { id: '456' } }; |
| 86 | + const res = createMockRes(); |
| 87 | + |
| 88 | + middleware(req, res, () => { |
| 89 | + res.json({ data: 'keyed' }); |
| 90 | + expect(store.get('custom:456')).toEqual({ data: 'keyed' }); |
| 91 | + done(); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('falls through to handler when cache store throws', (done) => { |
| 96 | + const brokenStore = { |
| 97 | + get() { throw new Error('store broken'); }, |
| 98 | + set() { throw new Error('store broken'); }, |
| 99 | + }; |
| 100 | + const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 101 | + const middleware = cacheResponse({ ttl: 5000, store: brokenStore }); |
| 102 | + const req = { originalUrl: '/api/escrow/123' }; |
| 103 | + const res = createMockRes(); |
| 104 | + |
| 105 | + middleware(req, res, () => { |
| 106 | + res.json({ data: 'fallthrough' }); |
| 107 | + expect(res.body).toEqual({ data: 'fallthrough' }); |
| 108 | + expect(warnSpy).toHaveBeenCalled(); |
| 109 | + warnSpy.mockRestore(); |
| 110 | + done(); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it('logs warning but still sends response when cache store set throws', (done) => { |
| 115 | + const setErrorStore = { |
| 116 | + get() { return undefined; }, |
| 117 | + set() { throw new Error('set broken'); }, |
| 118 | + }; |
| 119 | + const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 120 | + const middleware = cacheResponse({ ttl: 5000, store: setErrorStore }); |
| 121 | + const req = { originalUrl: '/api/escrow/789' }; |
| 122 | + const res = createMockRes(); |
| 123 | + |
| 124 | + middleware(req, res, () => { |
| 125 | + res.json({ data: 'still works' }); |
| 126 | + expect(res.body).toEqual({ data: 'still works' }); |
| 127 | + expect(warnSpy).toHaveBeenCalledWith('Cache store set error:', 'set broken'); |
| 128 | + warnSpy.mockRestore(); |
| 129 | + done(); |
| 130 | + }); |
| 131 | + }); |
| 132 | +}); |
0 commit comments