@nest-yalc-2/api-strategy 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -0
- package/README.md +40 -0
- package/package.json +33 -0
- package/src/__tests__/call-strategy-wrappers.spec.ts +217 -0
- package/src/__tests__/context-call.service.spec.ts +52 -0
- package/src/__tests__/context-event.service.spec.ts +52 -0
- package/src/__tests__/event-strategy-composition.spec.ts +185 -0
- package/src/__tests__/header-whitelist.helper.spec.ts +30 -0
- package/src/__tests__/http-abstract-call.strategy.spec.ts +17 -0
- package/src/__tests__/nest-http-call.strategy.spec.ts +164 -0
- package/src/__tests__/nest-local-call.strategy.spec.ts +263 -0
- package/src/__tests__/nest-local-event.strategy.spec.ts +55 -0
- package/src/__tests__/rabbitmq-event.strategy.spec.ts +220 -0
- package/src/__tests__/strategy-selector.provider.spec.ts +182 -0
- package/src/context-call.interface.d.ts +12 -0
- package/src/context-call.interface.js +3 -0
- package/src/context-call.interface.js.map +1 -0
- package/src/context-call.interface.ts +49 -0
- package/src/context-call.service.d.ts +7 -0
- package/src/context-call.service.js +32 -0
- package/src/context-call.service.js.map +1 -0
- package/src/context-call.service.ts +31 -0
- package/src/context-event.interface.d.ts +4 -0
- package/src/context-event.interface.js +3 -0
- package/src/context-event.interface.js.map +1 -0
- package/src/context-event.interface.ts +4 -0
- package/src/context-event.service.d.ts +7 -0
- package/src/context-event.service.js +32 -0
- package/src/context-event.service.js.map +1 -0
- package/src/context-event.service.ts +31 -0
- package/src/header-whitelist.helper.d.ts +2 -0
- package/src/header-whitelist.helper.js +13 -0
- package/src/header-whitelist.helper.js.map +1 -0
- package/src/header-whitelist.helper.ts +12 -0
- package/src/index.d.ts +15 -0
- package/src/index.js +32 -0
- package/src/index.js.map +1 -0
- package/src/index.ts +17 -0
- package/src/strategies/composite-event.strategy.d.ts +14 -0
- package/src/strategies/composite-event.strategy.js +52 -0
- package/src/strategies/composite-event.strategy.js.map +1 -0
- package/src/strategies/composite-event.strategy.ts +76 -0
- package/src/strategies/conditional-call.strategy.d.ts +17 -0
- package/src/strategies/conditional-call.strategy.js +41 -0
- package/src/strategies/conditional-call.strategy.js.map +1 -0
- package/src/strategies/conditional-call.strategy.ts +70 -0
- package/src/strategies/conditional-event.strategy.d.ts +16 -0
- package/src/strategies/conditional-event.strategy.js +38 -0
- package/src/strategies/conditional-event.strategy.js.map +1 -0
- package/src/strategies/conditional-event.strategy.ts +54 -0
- package/src/strategies/fallback-call.strategy.d.ts +14 -0
- package/src/strategies/fallback-call.strategy.js +41 -0
- package/src/strategies/fallback-call.strategy.js.map +1 -0
- package/src/strategies/fallback-call.strategy.ts +67 -0
- package/src/strategies/http-abstract-call.strategy.d.ts +44 -0
- package/src/strategies/http-abstract-call.strategy.js +19 -0
- package/src/strategies/http-abstract-call.strategy.js.map +1 -0
- package/src/strategies/http-abstract-call.strategy.ts +238 -0
- package/src/strategies/nest-http-call.strategy.d.ts +30 -0
- package/src/strategies/nest-http-call.strategy.js +68 -0
- package/src/strategies/nest-http-call.strategy.js.map +1 -0
- package/src/strategies/nest-http-call.strategy.ts +121 -0
- package/src/strategies/nest-local-call.strategy.d.ts +32 -0
- package/src/strategies/nest-local-call.strategy.js +84 -0
- package/src/strategies/nest-local-call.strategy.js.map +1 -0
- package/src/strategies/nest-local-call.strategy.ts +153 -0
- package/src/strategies/nest-local-event.strategy.d.ts +17 -0
- package/src/strategies/nest-local-event.strategy.js +30 -0
- package/src/strategies/nest-local-event.strategy.js.map +1 -0
- package/src/strategies/nest-local-event.strategy.ts +42 -0
- package/src/strategies/rabbitmq-event.strategy.d.ts +34 -0
- package/src/strategies/rabbitmq-event.strategy.js +97 -0
- package/src/strategies/rabbitmq-event.strategy.js.map +1 -0
- package/src/strategies/rabbitmq-event.strategy.ts +154 -0
- package/src/strategies/shadow-call.strategy.d.ts +17 -0
- package/src/strategies/shadow-call.strategy.js +42 -0
- package/src/strategies/shadow-call.strategy.js.map +1 -0
- package/src/strategies/shadow-call.strategy.ts +87 -0
- package/src/strategy-selector.provider.d.ts +19 -0
- package/src/strategy-selector.provider.js +52 -0
- package/src/strategy-selector.provider.js.map +1 -0
- package/src/strategy-selector.provider.ts +103 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import {
|
|
2
|
+
expect,
|
|
3
|
+
jest,
|
|
4
|
+
describe,
|
|
5
|
+
it,
|
|
6
|
+
beforeEach,
|
|
7
|
+
beforeAll,
|
|
8
|
+
afterAll,
|
|
9
|
+
afterEach,
|
|
10
|
+
} from '@jest/globals';
|
|
11
|
+
import { createMock, DeepMocked } from '@golevelup/ts-jest';
|
|
12
|
+
import { HttpService } from '@nestjs/axios';
|
|
13
|
+
import { AxiosInstance } from 'axios';
|
|
14
|
+
import {
|
|
15
|
+
NestHttpCallStrategy,
|
|
16
|
+
NestHttpCallStrategyProvider,
|
|
17
|
+
} from '../strategies/nest-http-call.strategy.js';
|
|
18
|
+
import { YalcGlobalClsService } from '../../../app/src/cls.module.js';
|
|
19
|
+
|
|
20
|
+
describe('NestHttpCallStrategy', () => {
|
|
21
|
+
let httpService: HttpService;
|
|
22
|
+
let clsService: DeepMocked<YalcGlobalClsService>;
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
const axiosRef = createMock<AxiosInstance>();
|
|
26
|
+
httpService = createMock<HttpService>({
|
|
27
|
+
axiosRef,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
clsService = createMock<YalcGlobalClsService>();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should be defined', () => {
|
|
34
|
+
expect(NestHttpCallStrategy).toBeDefined();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should be instantiable', () => {
|
|
38
|
+
const instance = new NestHttpCallStrategy(httpService, clsService);
|
|
39
|
+
expect(instance).toBeDefined();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should be able to execute the call method', async () => {
|
|
43
|
+
const instance = new NestHttpCallStrategy(httpService, clsService);
|
|
44
|
+
const result = await instance.call('http://localhost:3000', {
|
|
45
|
+
method: 'GET',
|
|
46
|
+
});
|
|
47
|
+
expect(result).toBeDefined();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should be able to execute the get method', async () => {
|
|
51
|
+
const instance = new NestHttpCallStrategy(httpService, clsService);
|
|
52
|
+
const result = await instance.get('http://localhost:3000');
|
|
53
|
+
expect(result).toBeDefined();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('should be able to execute the post method', async () => {
|
|
57
|
+
const instance = new NestHttpCallStrategy(httpService, clsService);
|
|
58
|
+
const result = await instance.post('http://localhost:3000', {});
|
|
59
|
+
expect(result).toBeDefined();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should create a provider', () => {
|
|
63
|
+
const provider = NestHttpCallStrategyProvider('test');
|
|
64
|
+
expect(provider).toBeDefined();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should create a provider and execute the useFactory method', () => {
|
|
68
|
+
const provider = NestHttpCallStrategyProvider('test');
|
|
69
|
+
expect(provider.useFactory(httpService, clsService)).toBeDefined();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should be able to execute the call method and return a json', async () => {
|
|
73
|
+
const axiosRef = createMock<AxiosInstance>({
|
|
74
|
+
request: (config: any) => ({ data: '{}' }),
|
|
75
|
+
});
|
|
76
|
+
httpService = createMock<HttpService>({
|
|
77
|
+
axiosRef,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const instance = new NestHttpCallStrategy(httpService, clsService);
|
|
81
|
+
const result = await instance.call('http://localhost:3000', {
|
|
82
|
+
method: 'GET',
|
|
83
|
+
});
|
|
84
|
+
expect(result).toBeDefined();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('should be able to execute the call method with parameters', async () => {
|
|
88
|
+
const instance = new NestHttpCallStrategy(httpService, clsService);
|
|
89
|
+
const result = await instance.call('http://localhost:3000', {
|
|
90
|
+
method: 'GET',
|
|
91
|
+
parameters: {
|
|
92
|
+
test1: { test: 'test' },
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
expect(result).toBeDefined();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('should add internal request token header when provided', async () => {
|
|
99
|
+
const requestSpy = jest.fn().mockResolvedValue({ data: '{}' });
|
|
100
|
+
const axiosRef = createMock<AxiosInstance>({
|
|
101
|
+
request: requestSpy as any,
|
|
102
|
+
});
|
|
103
|
+
httpService = createMock<HttpService>({
|
|
104
|
+
axiosRef,
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const instance = new NestHttpCallStrategy(
|
|
108
|
+
httpService,
|
|
109
|
+
clsService,
|
|
110
|
+
'http://localhost:3000',
|
|
111
|
+
{
|
|
112
|
+
internalRequestHeader: 'x-internal-token',
|
|
113
|
+
internalRequestToken: 'test-token-123',
|
|
114
|
+
},
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
await instance.call('/test', {
|
|
118
|
+
method: 'GET',
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
expect(requestSpy).toHaveBeenCalledWith(
|
|
122
|
+
expect.objectContaining({
|
|
123
|
+
headers: expect.objectContaining({
|
|
124
|
+
'x-internal-token': 'test-token-123',
|
|
125
|
+
}),
|
|
126
|
+
}),
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('should not override existing internal request token header', async () => {
|
|
131
|
+
const requestSpy = jest.fn().mockResolvedValue({ data: '{}' });
|
|
132
|
+
const axiosRef = createMock<AxiosInstance>({
|
|
133
|
+
request: requestSpy as any,
|
|
134
|
+
});
|
|
135
|
+
httpService = createMock<HttpService>({
|
|
136
|
+
axiosRef,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const instance = new NestHttpCallStrategy(
|
|
140
|
+
httpService,
|
|
141
|
+
clsService,
|
|
142
|
+
'http://localhost:3000',
|
|
143
|
+
{
|
|
144
|
+
internalRequestHeader: 'x-internal-token',
|
|
145
|
+
internalRequestToken: 'test-token-123',
|
|
146
|
+
},
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
await instance.call('/test', {
|
|
150
|
+
method: 'GET',
|
|
151
|
+
headers: {
|
|
152
|
+
'x-internal-token': 'user-provided-token',
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
expect(requestSpy).toHaveBeenCalledWith(
|
|
157
|
+
expect.objectContaining({
|
|
158
|
+
headers: expect.objectContaining({
|
|
159
|
+
'x-internal-token': 'user-provided-token',
|
|
160
|
+
}),
|
|
161
|
+
}),
|
|
162
|
+
);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import {
|
|
2
|
+
expect,
|
|
3
|
+
jest,
|
|
4
|
+
describe,
|
|
5
|
+
it,
|
|
6
|
+
beforeEach,
|
|
7
|
+
beforeAll,
|
|
8
|
+
afterAll,
|
|
9
|
+
afterEach,
|
|
10
|
+
} from '@jest/globals';
|
|
11
|
+
import { createMock, DeepMocked, PartialFuncReturn } from '@golevelup/ts-jest';
|
|
12
|
+
import { HttpAdapterHost } from '@nestjs/core';
|
|
13
|
+
import { FastifyAdapter } from '@nestjs/platform-fastify';
|
|
14
|
+
import { InjectOptions } from 'fastify';
|
|
15
|
+
import {
|
|
16
|
+
NestLocalCallStrategy,
|
|
17
|
+
NestLocalCallStrategyProvider,
|
|
18
|
+
} from '../strategies/nest-local-call.strategy.js';
|
|
19
|
+
import { YalcGlobalClsService } from '../../../app/src/cls.module.js';
|
|
20
|
+
|
|
21
|
+
describe('NestLocalCallStrategy', () => {
|
|
22
|
+
let adapterHost: HttpAdapterHost;
|
|
23
|
+
let clsService: DeepMocked<YalcGlobalClsService>;
|
|
24
|
+
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
adapterHost = createMock<HttpAdapterHost>({
|
|
27
|
+
httpAdapter: {
|
|
28
|
+
getInstance: () =>
|
|
29
|
+
createMock<FastifyAdapter>({
|
|
30
|
+
inject: (
|
|
31
|
+
_opts: string | InjectOptions,
|
|
32
|
+
): PartialFuncReturn<Promise<any>> =>
|
|
33
|
+
({
|
|
34
|
+
body: '{}',
|
|
35
|
+
json: () => ({}),
|
|
36
|
+
} as any),
|
|
37
|
+
}),
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
clsService = createMock<YalcGlobalClsService>();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should be defined', () => {
|
|
45
|
+
expect(NestLocalCallStrategy).toBeDefined();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should be instantiable', () => {
|
|
49
|
+
const instance = new NestLocalCallStrategy(adapterHost, clsService);
|
|
50
|
+
expect(instance).toBeDefined();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('should be able to execute the call method', async () => {
|
|
54
|
+
const instance = new NestLocalCallStrategy(adapterHost, clsService);
|
|
55
|
+
const result = await instance.call('http://localhost:3000', {
|
|
56
|
+
method: 'GET',
|
|
57
|
+
});
|
|
58
|
+
expect(result).toBeDefined();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should be able to execute the get method', async () => {
|
|
62
|
+
const instance = new NestLocalCallStrategy(adapterHost, clsService);
|
|
63
|
+
const result = await instance.get('http://localhost:3000');
|
|
64
|
+
expect(result).toBeDefined();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should be able to execute the post method', async () => {
|
|
68
|
+
const instance = new NestLocalCallStrategy(adapterHost, clsService);
|
|
69
|
+
const result = await instance.post('http://localhost:3000', {});
|
|
70
|
+
expect(result).toBeDefined();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('should create a provider', () => {
|
|
74
|
+
const provider = NestLocalCallStrategyProvider('test');
|
|
75
|
+
expect(provider).toBeDefined();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('should create a provider and execute the useFactory method', () => {
|
|
79
|
+
const provider = NestLocalCallStrategyProvider('test');
|
|
80
|
+
expect(provider.useFactory(adapterHost, clsService)).toBeDefined();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should be able to execute the call method with parameters', async () => {
|
|
84
|
+
const instance = new NestLocalCallStrategy(adapterHost, clsService);
|
|
85
|
+
const result = await instance.call('http://localhost:3000', {
|
|
86
|
+
method: 'GET',
|
|
87
|
+
parameters: {
|
|
88
|
+
test1: { test: 'test' },
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
expect(result).toBeDefined();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('should skip parsing json if json parse throws an error', async () => {
|
|
95
|
+
adapterHost = createMock<HttpAdapterHost>({
|
|
96
|
+
httpAdapter: {
|
|
97
|
+
getInstance: () =>
|
|
98
|
+
createMock<FastifyAdapter>({
|
|
99
|
+
inject: (
|
|
100
|
+
_opts: string | InjectOptions,
|
|
101
|
+
): PartialFuncReturn<Promise<any>> =>
|
|
102
|
+
({
|
|
103
|
+
body: '{',
|
|
104
|
+
json: () => {
|
|
105
|
+
throw new Error('test');
|
|
106
|
+
},
|
|
107
|
+
} as any),
|
|
108
|
+
}),
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const instance = new NestLocalCallStrategy(adapterHost, clsService);
|
|
113
|
+
expect(instance).toBeDefined();
|
|
114
|
+
|
|
115
|
+
const result = await instance.call('http://localhost:3000', {
|
|
116
|
+
method: 'GET',
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
expect(result.data).toEqual('{');
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('should skip parsing json if shouldSkipJsonParse is true', async () => {
|
|
123
|
+
const instance = new NestLocalCallStrategy(
|
|
124
|
+
adapterHost,
|
|
125
|
+
clsService,
|
|
126
|
+
// @ts-expect-error isnt needed for test
|
|
127
|
+
{},
|
|
128
|
+
'',
|
|
129
|
+
{
|
|
130
|
+
shouldSkipJsonParse: (body) => true,
|
|
131
|
+
},
|
|
132
|
+
);
|
|
133
|
+
expect(instance).toBeDefined();
|
|
134
|
+
|
|
135
|
+
const result = await instance.call('http://localhost:3000', {
|
|
136
|
+
method: 'GET',
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
expect(result.data).toEqual('{}');
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('should parse json if shouldSkipJsonParse is false', async () => {
|
|
143
|
+
const instance = new NestLocalCallStrategy(
|
|
144
|
+
adapterHost,
|
|
145
|
+
clsService,
|
|
146
|
+
// @ts-expect-error isnt needed for test
|
|
147
|
+
{},
|
|
148
|
+
'',
|
|
149
|
+
{
|
|
150
|
+
shouldSkipJsonParse: (body) => false,
|
|
151
|
+
},
|
|
152
|
+
);
|
|
153
|
+
expect(instance).toBeDefined();
|
|
154
|
+
|
|
155
|
+
const result = await instance.call('http://localhost:3000', {
|
|
156
|
+
method: 'GET',
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
expect(result.data).toMatchObject({});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('should parse json if shouldSkipJsonParse is undefined', async () => {
|
|
163
|
+
const instance = new NestLocalCallStrategy(adapterHost, clsService);
|
|
164
|
+
expect(instance).toBeDefined();
|
|
165
|
+
|
|
166
|
+
const result = await instance.call('http://localhost:3000', {
|
|
167
|
+
method: 'GET',
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
expect(result.data).toMatchObject({});
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('should add internal request token header when provided', async () => {
|
|
174
|
+
const injectSpy = jest.fn().mockResolvedValue({
|
|
175
|
+
body: '{}',
|
|
176
|
+
json: () => ({}),
|
|
177
|
+
});
|
|
178
|
+
adapterHost = createMock<HttpAdapterHost>({
|
|
179
|
+
httpAdapter: {
|
|
180
|
+
getInstance: () =>
|
|
181
|
+
createMock<FastifyAdapter>({
|
|
182
|
+
inject: injectSpy as any,
|
|
183
|
+
}),
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
const configService = {
|
|
188
|
+
values: {
|
|
189
|
+
internalRequestToken: 'test-token-123',
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const instance = new NestLocalCallStrategy(
|
|
194
|
+
adapterHost,
|
|
195
|
+
clsService,
|
|
196
|
+
// @ts-expect-error minimal config for test
|
|
197
|
+
configService,
|
|
198
|
+
'',
|
|
199
|
+
{
|
|
200
|
+
internalRequestHeader: 'x-internal-token',
|
|
201
|
+
},
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
await instance.call('/test', {
|
|
205
|
+
method: 'GET',
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
expect(injectSpy).toHaveBeenCalledWith(
|
|
209
|
+
expect.objectContaining({
|
|
210
|
+
headers: expect.objectContaining({
|
|
211
|
+
'x-internal-token': 'test-token-123',
|
|
212
|
+
}),
|
|
213
|
+
}),
|
|
214
|
+
);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('should not override existing internal request token header', async () => {
|
|
218
|
+
const injectSpy = jest.fn().mockResolvedValue({
|
|
219
|
+
body: '{}',
|
|
220
|
+
json: () => ({}),
|
|
221
|
+
});
|
|
222
|
+
adapterHost = createMock<HttpAdapterHost>({
|
|
223
|
+
httpAdapter: {
|
|
224
|
+
getInstance: () =>
|
|
225
|
+
createMock<FastifyAdapter>({
|
|
226
|
+
inject: injectSpy as any,
|
|
227
|
+
}),
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
const configService = {
|
|
232
|
+
values: {
|
|
233
|
+
internalRequestToken: 'test-token-123',
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const instance = new NestLocalCallStrategy(
|
|
238
|
+
adapterHost,
|
|
239
|
+
clsService,
|
|
240
|
+
// @ts-expect-error minimal config for test
|
|
241
|
+
configService,
|
|
242
|
+
'',
|
|
243
|
+
{
|
|
244
|
+
internalRequestHeader: 'x-internal-token',
|
|
245
|
+
},
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
await instance.call('/test', {
|
|
249
|
+
method: 'GET',
|
|
250
|
+
headers: {
|
|
251
|
+
'x-internal-token': 'user-provided-token',
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
expect(injectSpy).toHaveBeenCalledWith(
|
|
256
|
+
expect.objectContaining({
|
|
257
|
+
headers: expect.objectContaining({
|
|
258
|
+
'x-internal-token': 'user-provided-token',
|
|
259
|
+
}),
|
|
260
|
+
}),
|
|
261
|
+
);
|
|
262
|
+
});
|
|
263
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {
|
|
2
|
+
expect,
|
|
3
|
+
jest,
|
|
4
|
+
describe,
|
|
5
|
+
it,
|
|
6
|
+
beforeEach,
|
|
7
|
+
beforeAll,
|
|
8
|
+
afterAll,
|
|
9
|
+
afterEach,
|
|
10
|
+
} from '@jest/globals';
|
|
11
|
+
import { createMock } from '@golevelup/ts-jest';
|
|
12
|
+
import { EventEmitter2 } from '@nestjs/event-emitter';
|
|
13
|
+
import {
|
|
14
|
+
NestLocalEventStrategy,
|
|
15
|
+
NestLocalEventStrategyProvider,
|
|
16
|
+
} from '../strategies/nest-local-event.strategy.js';
|
|
17
|
+
|
|
18
|
+
describe('NestLocalEventStrategy', () => {
|
|
19
|
+
let eventEmitter: EventEmitter2;
|
|
20
|
+
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
eventEmitter = createMock<EventEmitter2>();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should be defined', () => {
|
|
26
|
+
expect(NestLocalEventStrategy).toBeDefined();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should be instantiable', () => {
|
|
30
|
+
const instance = new NestLocalEventStrategy(eventEmitter);
|
|
31
|
+
expect(instance).toBeDefined();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should call method emit', () => {
|
|
35
|
+
const instance = new NestLocalEventStrategy(eventEmitter);
|
|
36
|
+
instance.emit('test', {});
|
|
37
|
+
expect(eventEmitter.emit).toHaveBeenCalled();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('should call method emit Async', () => {
|
|
41
|
+
const instance = new NestLocalEventStrategy(eventEmitter);
|
|
42
|
+
instance.emitAsync('test', {});
|
|
43
|
+
expect(eventEmitter.emitAsync).toHaveBeenCalled();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should create a provider', () => {
|
|
47
|
+
const provider = NestLocalEventStrategyProvider('test');
|
|
48
|
+
expect(provider).toBeDefined();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should create a provider and execute the useFactory method', () => {
|
|
52
|
+
const provider = NestLocalEventStrategyProvider('test');
|
|
53
|
+
expect(provider.useFactory(eventEmitter)).toBeDefined();
|
|
54
|
+
});
|
|
55
|
+
});
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { jest, beforeEach, describe, expect, it } from "@jest/globals";
|
|
2
|
+
import { createMock } from "@golevelup/ts-jest";
|
|
3
|
+
import { EventEmitter2 } from "@nestjs/event-emitter";
|
|
4
|
+
import amqp from "amqplib";
|
|
5
|
+
import { RabbitMqEventStrategy, RabbitMqEventStrategyProvider } from "../strategies/rabbitmq-event.strategy.js";
|
|
6
|
+
|
|
7
|
+
const publish = jest.fn(() => true);
|
|
8
|
+
const assertExchange = jest.fn();
|
|
9
|
+
const channelClose = jest.fn();
|
|
10
|
+
const connectionClose = jest.fn();
|
|
11
|
+
const createChannel = jest.fn();
|
|
12
|
+
const connect = jest.spyOn(amqp, 'connect' as never) as unknown as jest.Mock;
|
|
13
|
+
|
|
14
|
+
describe("RabbitMqEventStrategy", () => {
|
|
15
|
+
let eventEmitter: EventEmitter2;
|
|
16
|
+
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
jest.clearAllMocks();
|
|
19
|
+
|
|
20
|
+
eventEmitter = createMock<EventEmitter2>({
|
|
21
|
+
emit: jest.fn(() => true),
|
|
22
|
+
emitAsync: jest.fn(() => Promise.resolve([])),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
publish.mockReturnValue(true);
|
|
26
|
+
assertExchange.mockResolvedValue(undefined as never);
|
|
27
|
+
channelClose.mockResolvedValue(undefined as never);
|
|
28
|
+
connectionClose.mockResolvedValue(undefined as never);
|
|
29
|
+
createChannel.mockResolvedValue({
|
|
30
|
+
assertExchange,
|
|
31
|
+
publish,
|
|
32
|
+
close: channelClose,
|
|
33
|
+
} as never);
|
|
34
|
+
connect.mockResolvedValue({
|
|
35
|
+
createChannel,
|
|
36
|
+
close: connectionClose,
|
|
37
|
+
} as never);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("publishes to RabbitMQ asynchronously", async () => {
|
|
41
|
+
const strategy = new RabbitMqEventStrategy({
|
|
42
|
+
url: "amqp://localhost",
|
|
43
|
+
exchange: "test.events",
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const result = await strategy.emitAsync("task.created", { id: "task-1" });
|
|
47
|
+
|
|
48
|
+
expect(result).toBe(true);
|
|
49
|
+
expect(eventEmitter.emitAsync).not.toHaveBeenCalled();
|
|
50
|
+
expect(connect).toHaveBeenCalledWith("amqp://localhost");
|
|
51
|
+
expect(assertExchange).toHaveBeenCalledWith("test.events", "topic", {
|
|
52
|
+
durable: true,
|
|
53
|
+
});
|
|
54
|
+
expect(publish).toHaveBeenCalledWith(
|
|
55
|
+
"test.events",
|
|
56
|
+
"task.created",
|
|
57
|
+
Buffer.from(JSON.stringify({ id: "task-1" })),
|
|
58
|
+
{
|
|
59
|
+
contentType: "application/json",
|
|
60
|
+
persistent: true,
|
|
61
|
+
}
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("uses custom broker options and buffer serialization", async () => {
|
|
66
|
+
const body = Buffer.from("encoded");
|
|
67
|
+
const strategy = new RabbitMqEventStrategy({
|
|
68
|
+
url: "amqp://localhost",
|
|
69
|
+
exchange: "test.events",
|
|
70
|
+
exchangeType: "fanout",
|
|
71
|
+
durable: false,
|
|
72
|
+
persistent: false,
|
|
73
|
+
contentType: "application/custom",
|
|
74
|
+
publishOptions: {
|
|
75
|
+
expiration: "1000",
|
|
76
|
+
},
|
|
77
|
+
serialize: () => body,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
await strategy.emitAsync("task.created", { id: "task-1" });
|
|
81
|
+
|
|
82
|
+
expect(assertExchange).toHaveBeenCalledWith("test.events", "fanout", {
|
|
83
|
+
durable: false,
|
|
84
|
+
});
|
|
85
|
+
expect(publish).toHaveBeenCalledWith("test.events", "task.created", body, {
|
|
86
|
+
contentType: "application/custom",
|
|
87
|
+
persistent: false,
|
|
88
|
+
expiration: "1000",
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("reuses the open RabbitMQ channel", async () => {
|
|
93
|
+
const strategy = new RabbitMqEventStrategy({
|
|
94
|
+
url: "amqp://localhost",
|
|
95
|
+
exchange: "test.events",
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
await strategy.emitAsync("task.created", { id: "task-1" });
|
|
99
|
+
await strategy.emitAsync("task.updated", { id: "task-1" });
|
|
100
|
+
|
|
101
|
+
expect(connect).toHaveBeenCalledTimes(1);
|
|
102
|
+
expect(createChannel).toHaveBeenCalledTimes(1);
|
|
103
|
+
expect(publish).toHaveBeenCalledTimes(2);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("publishes in the background for sync emit", async () => {
|
|
107
|
+
const strategy = new RabbitMqEventStrategy({
|
|
108
|
+
url: "amqp://localhost",
|
|
109
|
+
exchange: "test.events",
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const result = strategy.emit("task.created", { id: "task-1" });
|
|
113
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
114
|
+
|
|
115
|
+
expect(result).toBe(true);
|
|
116
|
+
expect(eventEmitter.emit).not.toHaveBeenCalled();
|
|
117
|
+
expect(publish).toHaveBeenCalled();
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("does not fail sync emit when background publishing fails", async () => {
|
|
121
|
+
const strategy = new RabbitMqEventStrategy({
|
|
122
|
+
url: "amqp://localhost",
|
|
123
|
+
exchange: "test.events",
|
|
124
|
+
});
|
|
125
|
+
connect.mockRejectedValueOnce(new Error("publish failed") as never);
|
|
126
|
+
|
|
127
|
+
const result = strategy.emit("task.created", { id: "task-1" });
|
|
128
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
129
|
+
|
|
130
|
+
expect(result).toBe(true);
|
|
131
|
+
expect(connect).toHaveBeenCalled();
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("closes RabbitMQ resources on module destroy", async () => {
|
|
135
|
+
const strategy = new RabbitMqEventStrategy({
|
|
136
|
+
url: "amqp://localhost",
|
|
137
|
+
exchange: "test.events",
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
await strategy.emitAsync("task.created", { id: "task-1" });
|
|
141
|
+
await strategy.onModuleDestroy();
|
|
142
|
+
|
|
143
|
+
expect(channelClose).toHaveBeenCalled();
|
|
144
|
+
expect(connectionClose).toHaveBeenCalled();
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("ignores closing errors on module destroy", async () => {
|
|
148
|
+
const strategy = new RabbitMqEventStrategy({
|
|
149
|
+
url: "amqp://localhost",
|
|
150
|
+
exchange: "test.events",
|
|
151
|
+
});
|
|
152
|
+
channelClose.mockRejectedValue(new Error("already closing") as never);
|
|
153
|
+
connectionClose.mockRejectedValue(new Error("connection closing") as never);
|
|
154
|
+
|
|
155
|
+
await strategy.emitAsync("task.created", { id: "task-1" });
|
|
156
|
+
|
|
157
|
+
await expect(strategy.onModuleDestroy()).resolves.toBeUndefined();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("ignores synchronous closing errors on module destroy", async () => {
|
|
161
|
+
const strategy = new RabbitMqEventStrategy({
|
|
162
|
+
url: "amqp://localhost",
|
|
163
|
+
exchange: "test.events",
|
|
164
|
+
});
|
|
165
|
+
channelClose.mockImplementationOnce(() => {
|
|
166
|
+
throw new Error("already closing");
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
await strategy.emitAsync("task.created", { id: "task-1" });
|
|
170
|
+
|
|
171
|
+
await expect(strategy.onModuleDestroy()).resolves.toBeUndefined();
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("throws unexpected close errors on module destroy", async () => {
|
|
175
|
+
const strategy = new RabbitMqEventStrategy({
|
|
176
|
+
url: "amqp://localhost",
|
|
177
|
+
exchange: "test.events",
|
|
178
|
+
});
|
|
179
|
+
channelClose.mockRejectedValue(new Error("boom") as never);
|
|
180
|
+
|
|
181
|
+
await strategy.emitAsync("task.created", { id: "task-1" });
|
|
182
|
+
|
|
183
|
+
await expect(strategy.onModuleDestroy()).rejects.toThrow("boom");
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("does not close RabbitMQ resources before a connection is opened", async () => {
|
|
187
|
+
const strategy = new RabbitMqEventStrategy({
|
|
188
|
+
url: "amqp://localhost",
|
|
189
|
+
exchange: "test.events",
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
await strategy.onModuleDestroy();
|
|
193
|
+
|
|
194
|
+
expect(channelClose).not.toHaveBeenCalled();
|
|
195
|
+
expect(connectionClose).not.toHaveBeenCalled();
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("creates a provider", () => {
|
|
199
|
+
const provider = RabbitMqEventStrategyProvider("test", {
|
|
200
|
+
options: {
|
|
201
|
+
url: "amqp://localhost",
|
|
202
|
+
exchange: "test.events",
|
|
203
|
+
},
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
expect(provider).toBeDefined();
|
|
207
|
+
expect(provider.useFactory()).toBeDefined();
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("creates a provider with an options factory", () => {
|
|
211
|
+
const provider = RabbitMqEventStrategyProvider("test", {
|
|
212
|
+
options: () => ({
|
|
213
|
+
url: "amqp://localhost",
|
|
214
|
+
exchange: "test.events",
|
|
215
|
+
}),
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
expect(provider.useFactory()).toBeDefined();
|
|
219
|
+
});
|
|
220
|
+
});
|