-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.spec.ts
More file actions
209 lines (168 loc) · 7.15 KB
/
context.spec.ts
File metadata and controls
209 lines (168 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { NextFunction, Request, Response } from 'express';
import type { Context, Next } from 'hono';
import type { Mock } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import * as hooks from '../../src/core/hooks';
import { Uuid } from '../../src/core/uuid';
import { setContext, setContextHono, setCorrelationId, setCorrelationIdHono, setTenantId, setTenantIdHono } from '../../src/middlewares/context';
// Mock dependencies
vi.mock('../../src/core/hooks');
vi.mock('../../src/core/uuid');
describe('Express Context Middleware', () => {
let mockRequest: Partial<Request>;
let mockResponse: Partial<Response>;
let mockNext: NextFunction;
beforeEach(() => {
mockRequest = {
headers: {},
body: {}
};
mockResponse = {};
mockNext = vi.fn();
vi.clearAllMocks();
});
describe('set context', () => {
it('should call setHookContext and next', () => {
setContext(mockRequest as Request, mockResponse as Response, mockNext);
expect(hooks.setHookContext).toHaveBeenCalled();
});
});
describe('set correlation id', () => {
it('should use correlation ID from header', () => {
const testCorrelationId = 'test-correlation-id';
mockRequest.headers = {
'x-correlation-id': testCorrelationId
};
setCorrelationId(mockRequest as Request, mockResponse as Response, mockNext);
expect(hooks.setHookCorrelationId).toHaveBeenCalledWith(testCorrelationId);
expect(mockNext).toHaveBeenCalled();
});
it('should use correlation ID from message attributes', () => {
const testCorrelationId = 'test-correlation-id';
mockRequest.body = {
message: {
attributes: {
correlationId: testCorrelationId
}
}
};
setCorrelationId(mockRequest as Request, mockResponse as Response, mockNext);
expect(hooks.setHookCorrelationId).toHaveBeenCalledWith(testCorrelationId);
expect(mockNext).toHaveBeenCalled();
});
it('should generate new correlation ID if none provided', () => {
const mockUuid = 'generated-uuid';
(Uuid.random as Mock).mockReturnValue({ value: mockUuid });
setCorrelationId(mockRequest as Request, mockResponse as Response, mockNext);
expect(hooks.setHookCorrelationId).toHaveBeenCalledWith(mockUuid);
expect(mockNext).toHaveBeenCalled();
});
it('should prefer message attributes over header for correlation ID', () => {
const headerCorrelationId = 'header-correlation-id';
const attributeCorrelationId = 'attribute-correlation-id';
mockRequest.headers = {
'x-correlation-id': headerCorrelationId
};
mockRequest.body = {
message: {
attributes: {
correlationId: attributeCorrelationId
}
}
};
setCorrelationId(mockRequest as Request, mockResponse as Response, mockNext);
expect(hooks.setHookCorrelationId).toHaveBeenCalledWith(attributeCorrelationId);
expect(mockNext).toHaveBeenCalled();
});
});
describe('set tenant id', () => {
const defaultTenantId = 'default-tenant';
it('should use tenant ID from header', () => {
const testTenantId = 'test-tenant';
mockRequest.headers = {
'x-tenant-id': testTenantId
};
const middleware = setTenantId(defaultTenantId);
middleware(mockRequest as Request, mockResponse as Response, mockNext);
expect(hooks.setHookTenantId).toHaveBeenCalledWith(testTenantId);
expect(mockNext).toHaveBeenCalled();
});
it('should use default tenant ID if none provided in header', () => {
const middleware = setTenantId(defaultTenantId);
middleware(mockRequest as Request, mockResponse as Response, mockNext);
expect(hooks.setHookTenantId).toHaveBeenCalledWith(defaultTenantId);
expect(mockNext).toHaveBeenCalled();
});
it('should throw an error if tenant ID is not provided in header nor default tenant ID', () => {
const middleware = setTenantId();
expect(() => {
middleware(mockRequest as Request, mockResponse as Response, mockNext)
}).toThrow('Tenant ID is required, but it is not present in the request headers');
});
});
});
describe('Hono Context Middleware', () => {
let mockContext: Partial<Context>;
let mockNext: Next;
beforeEach(() => {
mockContext = {
req: {
header: vi.fn(),
} as any,
};
mockNext = vi.fn().mockResolvedValue(undefined);
vi.clearAllMocks();
(hooks.setHookContext as Mock).mockImplementation(callback => callback());
});
describe('setContextHono', () => {
it('should call setHookContext and next', async () => {
await setContextHono(mockContext as Context, mockNext);
expect(hooks.setHookContext).toHaveBeenCalled();
expect(mockNext).toHaveBeenCalled();
});
});
describe('setCorrelationIdHono', () => {
it('should use correlation ID from header', async () => {
const testCorrelationId = 'test-correlation-id';
(mockContext.req!.header as Mock).mockReturnValue(testCorrelationId);
await setCorrelationIdHono(mockContext as Context, mockNext);
expect(mockContext.req!.header).toHaveBeenCalledWith('x-correlation-id');
expect(hooks.setHookCorrelationId).toHaveBeenCalledWith(testCorrelationId);
expect(mockNext).toHaveBeenCalled();
});
it('should generate new correlation ID if none provided', async () => {
const mockUuid = 'generated-uuid';
(mockContext.req!.header as Mock).mockReturnValue(undefined);
(Uuid.random as Mock).mockReturnValue({ value: mockUuid });
await setCorrelationIdHono(mockContext as Context, mockNext);
expect(hooks.setHookCorrelationId).toHaveBeenCalledWith(mockUuid);
expect(mockNext).toHaveBeenCalled();
});
});
describe('setTenantIdHono', () => {
const defaultTenantId = 'default-tenant';
it('should use tenant ID from header', async () => {
const testTenantId = 'test-tenant';
(mockContext.req!.header as Mock).mockReturnValue(testTenantId);
const middleware = setTenantIdHono(defaultTenantId);
await middleware(mockContext as Context, mockNext);
expect(mockContext.req!.header).toHaveBeenCalledWith('x-tenant-id');
expect(hooks.setHookTenantId).toHaveBeenCalledWith(testTenantId);
expect(mockNext).toHaveBeenCalled();
});
it('should use default tenant ID if none provided in header', async () => {
(mockContext.req!.header as Mock).mockReturnValue(undefined);
const middleware = setTenantIdHono(defaultTenantId);
await middleware(mockContext as Context, mockNext);
expect(hooks.setHookTenantId).toHaveBeenCalledWith(defaultTenantId);
expect(mockNext).toHaveBeenCalled();
});
it('should throw an error if tenant ID is not provided in header nor default tenant ID', async () => {
(mockContext.req!.header as Mock).mockReturnValue(undefined);
const middleware = setTenantIdHono();
await expect(middleware(mockContext as Context, mockNext))
.rejects.toThrow('Tenant ID is required, but it is not present in the request headers');
});
});
});