adapter tests
Some checks failed
CI / lint-typecheck (pull_request) Failing after 4m51s
CI / tests (pull_request) Has been skipped
CI / contract-tests (pull_request) Has been skipped
CI / e2e-tests (pull_request) Has been skipped
CI / comment-pr (pull_request) Has been skipped
CI / commit-types (pull_request) Has been skipped
Some checks failed
CI / lint-typecheck (pull_request) Failing after 4m51s
CI / tests (pull_request) Has been skipped
CI / contract-tests (pull_request) Has been skipped
CI / e2e-tests (pull_request) Has been skipped
CI / comment-pr (pull_request) Has been skipped
CI / commit-types (pull_request) Has been skipped
This commit is contained in:
63
adapters/http/RequestContext.test.ts
Normal file
63
adapters/http/RequestContext.test.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { getHttpRequestContext, requestContextMiddleware, tryGetHttpRequestContext } from './RequestContext';
|
||||
|
||||
describe('RequestContext', () => {
|
||||
it('should return null when accessed outside of middleware', () => {
|
||||
// When
|
||||
const ctx = tryGetHttpRequestContext();
|
||||
|
||||
// Then
|
||||
expect(ctx).toBeNull();
|
||||
});
|
||||
|
||||
it('should throw error when getHttpRequestContext is called outside of middleware', () => {
|
||||
// When & Then
|
||||
expect(() => getHttpRequestContext()).toThrow('HttpRequestContext is not available');
|
||||
});
|
||||
|
||||
it('should provide request and response within middleware scope', () => {
|
||||
// Given
|
||||
const mockReq = { id: 'req-1' } as unknown as Request;
|
||||
const mockRes = { id: 'res-1' } as unknown as Response;
|
||||
|
||||
// When
|
||||
return new Promise<void>((resolve) => {
|
||||
requestContextMiddleware(mockReq, mockRes, () => {
|
||||
// Then
|
||||
const ctx = getHttpRequestContext();
|
||||
expect(ctx.req).toBe(mockReq);
|
||||
expect(ctx.res).toBe(mockRes);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should maintain separate contexts for concurrent requests', () => {
|
||||
// Given
|
||||
const req1 = { id: '1' } as unknown as Request;
|
||||
const res1 = { id: '1' } as unknown as Response;
|
||||
const req2 = { id: '2' } as unknown as Request;
|
||||
const res2 = { id: '2' } as unknown as Response;
|
||||
|
||||
// When
|
||||
const p1 = new Promise<void>((resolve) => {
|
||||
requestContextMiddleware(req1, res1, () => {
|
||||
setTimeout(() => {
|
||||
expect(getHttpRequestContext().req).toBe(req1);
|
||||
resolve();
|
||||
}, 10);
|
||||
});
|
||||
});
|
||||
|
||||
const p2 = new Promise<void>((resolve) => {
|
||||
requestContextMiddleware(req2, res2, () => {
|
||||
setTimeout(() => {
|
||||
expect(getHttpRequestContext().req).toBe(req2);
|
||||
resolve();
|
||||
}, 5);
|
||||
});
|
||||
});
|
||||
|
||||
return Promise.all([p1, p2]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user