|
| 1 | +import { idempotency, MemoryIdempotencyStore } from '../middleware/idempotency'; |
| 2 | +import { Request, Response } from 'express'; |
| 3 | + |
| 4 | +type FakeRes = Response & { |
| 5 | + captured: any; |
| 6 | + statusCode: number; |
| 7 | + jsoned: boolean; |
| 8 | + headers: Record<string, string>; |
| 9 | +}; |
| 10 | + |
| 11 | +function mkRes(): FakeRes { |
| 12 | + const r: any = { |
| 13 | + statusCode: 200, |
| 14 | + jsoned: false, |
| 15 | + captured: undefined, |
| 16 | + headers: {}, |
| 17 | + set(k: string, v: string) { this.headers[k.toLowerCase()] = v; return this; }, |
| 18 | + status(code: number) { this.statusCode = code; return this; }, |
| 19 | + json(body: any) { this.captured = body; this.jsoned = true; return this; }, |
| 20 | + }; |
| 21 | + return r as FakeRes; |
| 22 | +} |
| 23 | + |
| 24 | +function mkReq(headers: Record<string, string> = {}, path = '/api/v1/payment', method = 'POST'): Request { |
| 25 | + return { headers, path, method, route: { path } } as unknown as Request; |
| 26 | +} |
| 27 | + |
| 28 | +/** Run the middleware and resolve once it either calls next() or replies. */ |
| 29 | +function run(mw: any, req: Request, res: FakeRes): Promise<{ next: boolean }> { |
| 30 | + return new Promise<{ next: boolean }>((resolve) => { |
| 31 | + let done = false; |
| 32 | + const finish = (nextCalled: boolean) => { |
| 33 | + if (!done) { done = true; resolve({ next: nextCalled }); } |
| 34 | + }; |
| 35 | + // resolve on next() |
| 36 | + const next = () => finish(true); |
| 37 | + // resolve when the middleware writes a response |
| 38 | + const origJson = res.json.bind(res); |
| 39 | + res.json = ((body: any) => { |
| 40 | + const out = origJson(body); |
| 41 | + finish(false); |
| 42 | + return out; |
| 43 | + }) as Response['json']; |
| 44 | + mw(req, res as unknown as Response, next); |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +describe('idempotency middleware', () => { |
| 49 | + it('passes through when no Idempotency-Key header is present', async () => { |
| 50 | + const store = new MemoryIdempotencyStore(); |
| 51 | + const mw = idempotency({ store }); |
| 52 | + const res = mkRes(); |
| 53 | + const { next } = await run(mw, mkReq({}), res); |
| 54 | + expect(next).toBe(true); |
| 55 | + expect(res.jsoned).toBe(false); |
| 56 | + }); |
| 57 | + |
| 58 | + it('first request processes; identical retry replays the cached response', async () => { |
| 59 | + const store = new MemoryIdempotencyStore(); |
| 60 | + const mw = idempotency({ store, ttlSeconds: 60 }); |
| 61 | + const key = 'abc-123'; |
| 62 | + |
| 63 | + const res1 = mkRes(); |
| 64 | + const r1 = await run(mw, mkReq({ 'idempotency-key': key }), res1); |
| 65 | + expect(r1.next).toBe(true); |
| 66 | + // simulate the route handler writing the response (captured by our patch) |
| 67 | + res1.status(200).json({ success: true, transactionId: 'tx-1' }); |
| 68 | + |
| 69 | + const res2 = mkRes(); |
| 70 | + const r2 = await run(mw, mkReq({ 'idempotency-key': key }), res2); |
| 71 | + expect(r2.next).toBe(false); // replayed from cache, handler not invoked |
| 72 | + expect(res2.statusCode).toBe(200); |
| 73 | + expect(res2.captured).toEqual({ success: true, transactionId: 'tx-1' }); |
| 74 | + expect(res2.headers['x-idempotent-replay']).toBe('true'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('returns 409 when a request with the same key is already in flight', async () => { |
| 78 | + const store = new MemoryIdempotencyStore(); |
| 79 | + const mw = idempotency({ store, ttlSeconds: 60 }); |
| 80 | + const key = 'inflight-key'; |
| 81 | + |
| 82 | + const res1 = mkRes(); |
| 83 | + await run(mw, mkReq({ 'idempotency-key': key }), res1); // acquired, "in flight" |
| 84 | + |
| 85 | + const res2 = mkRes(); |
| 86 | + const r2 = await run(mw, mkReq({ 'idempotency-key': key }), res2); |
| 87 | + expect(r2.next).toBe(false); |
| 88 | + expect(res2.statusCode).toBe(409); |
| 89 | + expect(res2.captured.error).toBe('IDEMPOTENCY_IN_FLIGHT'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('does not cache 5xx so the client can retry', async () => { |
| 93 | + const store = new MemoryIdempotencyStore(); |
| 94 | + const mw = idempotency({ store, ttlSeconds: 60 }); |
| 95 | + const key = 'err-key'; |
| 96 | + |
| 97 | + const res1 = mkRes(); |
| 98 | + await run(mw, mkReq({ 'idempotency-key': key }), res1); |
| 99 | + res1.status(500).json({ error: 'boom' }); |
| 100 | + |
| 101 | + const res2 = mkRes(); |
| 102 | + const r2 = await run(mw, mkReq({ 'idempotency-key': key }), res2); |
| 103 | + expect(r2.next).toBe(true); // not replayed — handler runs again |
| 104 | + }); |
| 105 | + |
| 106 | + it('scopes keys per route (same client key, different route => independent)', async () => { |
| 107 | + const store = new MemoryIdempotencyStore(); |
| 108 | + const mw = idempotency({ store, ttlSeconds: 60 }); |
| 109 | + const key = 'shared-key'; |
| 110 | + |
| 111 | + const resA = mkRes(); |
| 112 | + await run(mw, mkReq({ 'idempotency-key': key }, '/api/v1/payment'), resA); |
| 113 | + resA.status(200).json({ route: 'a' }); |
| 114 | + |
| 115 | + const resB = mkRes(); |
| 116 | + const rB = await run(mw, mkReq({ 'idempotency-key': key }, '/api/v2/payment'), resB); |
| 117 | + expect(rB.next).toBe(true); // different route -> not a replay |
| 118 | + }); |
| 119 | +}); |
0 commit comments