mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
test: add integration test
This commit is contained in:
@@ -23,9 +23,13 @@ describe('Durable providers', () => {
|
||||
});
|
||||
|
||||
describe('when service is durable', () => {
|
||||
const performHttpCall = (tenantId: number, end: (err?: any) => void) =>
|
||||
const performHttpCall = (
|
||||
tenantId: number,
|
||||
end: (err?: any) => void,
|
||||
endpoint = '/durable',
|
||||
) =>
|
||||
request(server)
|
||||
.get('/durable')
|
||||
.get(endpoint)
|
||||
.set({ ['x-tenant-id']: tenantId })
|
||||
.end((err, res) => {
|
||||
if (err) return end(err);
|
||||
@@ -67,6 +71,19 @@ describe('Durable providers', () => {
|
||||
);
|
||||
expect(result.text).equal('Hello world! Counter: 1');
|
||||
});
|
||||
|
||||
it(`should register a custom per-tenant request payload`, async () => {
|
||||
let result: request.Response;
|
||||
result = await new Promise<request.Response>(resolve =>
|
||||
performHttpCall(1, resolve, '/durable/echo'),
|
||||
);
|
||||
expect(result.body).deep.equal({ tenantId: '1' });
|
||||
|
||||
result = await new Promise<request.Response>(resolve =>
|
||||
performHttpCall(3, resolve, '/durable/echo'),
|
||||
);
|
||||
expect(result.body).deep.equal({ tenantId: '3' });
|
||||
});
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
|
||||
@@ -14,7 +14,10 @@ export class DurableContextIdStrategy implements ContextIdStrategy {
|
||||
tenantSubTreeId = { id: +tenantId } as ContextId;
|
||||
tenants.set(tenantId, tenantSubTreeId);
|
||||
}
|
||||
return (info: HostComponentInfo) =>
|
||||
info.isTreeDurable ? tenantSubTreeId : contextId;
|
||||
return {
|
||||
resolve: (info: HostComponentInfo) =>
|
||||
info.isTreeDurable ? tenantSubTreeId : contextId,
|
||||
payload: { tenantId },
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,4 +9,9 @@ export class DurableController {
|
||||
greeting(): string {
|
||||
return this.durableService.greeting();
|
||||
}
|
||||
|
||||
@Get('echo')
|
||||
echo() {
|
||||
return this.durableService.requestPayload;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import { Injectable, Scope } from '@nestjs/common';
|
||||
import { Inject, Injectable, Scope } from '@nestjs/common';
|
||||
import { REQUEST } from '@nestjs/core';
|
||||
|
||||
@Injectable({ scope: Scope.REQUEST, durable: true })
|
||||
export class DurableService {
|
||||
public instanceCounter = 0;
|
||||
|
||||
constructor(@Inject(REQUEST) public readonly requestPayload: unknown) {}
|
||||
|
||||
greeting() {
|
||||
++this.instanceCounter;
|
||||
return `Hello world! Counter: ${this.instanceCounter}`;
|
||||
|
||||
Reference in New Issue
Block a user