|
| 1 | +import { APIGatewayProxyEvent } from 'aws-lambda'; |
| 2 | +import { extractCommonIds } from '../commonIds'; |
| 3 | + |
| 4 | +const mockDeps = { |
| 5 | + env: { |
| 6 | + APIM_CORRELATION_HEADER: 'x-correlation-id', |
| 7 | + SUPPLIER_ID_HEADER: 'x-supplier-id', |
| 8 | + } |
| 9 | +} as any; |
| 10 | + |
| 11 | +const mockContext = {} as APIGatewayProxyEvent['requestContext']; |
| 12 | + |
| 13 | +describe('extractCommonIds', () => { |
| 14 | + it('returns error if headers are missing', () => { |
| 15 | + expect(extractCommonIds({}, mockContext, mockDeps)).toEqual({ |
| 16 | + ok: false, |
| 17 | + error: expect.any(Error) |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + it('returns error if correlation id is missing', () => { |
| 22 | + const headers = { 'x-supplier-id': 'SUP123', 'x-request-id': 'REQ123' }; |
| 23 | + expect(extractCommonIds(headers, mockContext, mockDeps)).toEqual({ |
| 24 | + ok: false, |
| 25 | + error: expect.any(Error) |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + it('returns error if request id is missing', () => { |
| 30 | + const headers = { 'x-correlation-id': 'CORR123', 'x-supplier-id': 'SUP123' }; |
| 31 | + expect(extractCommonIds(headers, mockContext, mockDeps)).toEqual({ |
| 32 | + ok: false, |
| 33 | + error: expect.any(Error), |
| 34 | + correlationId: 'CORR123' |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('returns error if supplier id is missing', () => { |
| 39 | + const headers = { 'x-correlation-id': 'CORR123', 'x-request-id': 'REQ123' }; |
| 40 | + expect(extractCommonIds(headers, mockContext, mockDeps)).toEqual({ |
| 41 | + ok: false, |
| 42 | + error: expect.any(Error), |
| 43 | + correlationId: 'CORR123' |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('returns ok and ids if all present', () => { |
| 48 | + const headers = { |
| 49 | + 'x-correlation-id': 'CORR123', |
| 50 | + 'x-request-id': 'REQ123', |
| 51 | + 'x-supplier-id': 'SUP123' |
| 52 | + }; |
| 53 | + expect(extractCommonIds(headers, mockContext, mockDeps)).toEqual({ |
| 54 | + ok: true, |
| 55 | + value: { |
| 56 | + correlationId: 'CORR123', |
| 57 | + supplierId: 'SUP123' |
| 58 | + } |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('handles mixed case header names', () => { |
| 63 | + const headers = { |
| 64 | + 'X-Correlation-Id': 'CORR123', |
| 65 | + 'X-Request-Id': 'REQ123', |
| 66 | + 'X-Supplier-Id': 'SUP123' |
| 67 | + }; |
| 68 | + expect(extractCommonIds(headers, mockContext, mockDeps)).toEqual({ |
| 69 | + ok: true, |
| 70 | + value: { |
| 71 | + correlationId: 'CORR123', |
| 72 | + supplierId: 'SUP123' |
| 73 | + } |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('uses the supplier id from the authorizer if present', () => { |
| 78 | + const headers = { 'x-correlation-id': 'CORR123', 'x-supplier-id': 'SUP123', 'x-request-id': 'REQ123' }; |
| 79 | + const context = { 'authorizer': {'principalId': 'SUP456'}} as unknown as APIGatewayProxyEvent['requestContext']; |
| 80 | + expect(extractCommonIds(headers, context, mockDeps)).toEqual({ |
| 81 | + ok: true, |
| 82 | + value: { |
| 83 | + correlationId: 'CORR123', |
| 84 | + supplierId: 'SUP456' |
| 85 | + } |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('refuses to use the supplier id from the header if authorizer is present', () => { |
| 90 | + const headers = { 'x-correlation-id': 'CORR123', 'x-supplier-id': 'SUP123', 'x-request-id': 'REQ123' }; |
| 91 | + const context = { 'authorizer': {}} as unknown as APIGatewayProxyEvent['requestContext']; |
| 92 | + expect(extractCommonIds(headers, context, mockDeps)).toEqual({ |
| 93 | + ok: false, |
| 94 | + error: expect.any(Error), |
| 95 | + correlationId: 'CORR123' |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments