Skip to content

Commit 81404bb

Browse files
committed
test: add integration test 'should execute middleware only once'
1 parent 963437c commit 81404bb

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

integration/nest-application/global-prefix/e2e/global-prefix.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ describe('Global prefix', () => {
130130
.expect(200, { '0': 'params', tenantId: 'test' });
131131
});
132132

133+
it(`should execute middleware only once`, async () => {
134+
app.setGlobalPrefix('/api', { exclude: ['/'] });
135+
136+
server = app.getHttpServer();
137+
await app.init();
138+
139+
await request(server).get('/').expect(200, '1');
140+
await request(server).get('/api/count').expect(200, '2');
141+
});
142+
133143
afterEach(async () => {
134144
await app.close();
135145
});

integration/nest-application/global-prefix/src/app.controller.ts

+10
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@ export class AppController {
2626
postTest(): string {
2727
return 'test';
2828
}
29+
30+
@Get()
31+
getHome(@Req() req) {
32+
return req.count;
33+
}
34+
35+
@Get('count')
36+
getCount(@Req() req) {
37+
return req.count;
38+
}
2939
}

integration/nest-application/global-prefix/src/app.module.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const MIDDLEWARE_PARAM_VALUE = 'middleware_param';
88
controllers: [AppController],
99
})
1010
export class AppModule {
11+
private count = 0;
1112
configure(consumer: MiddlewareConsumer) {
1213
consumer
1314
.apply((req, res, next) => res.end(MIDDLEWARE_VALUE))
@@ -27,6 +28,12 @@ export class AppModule {
2728
req.middlewareParams = req.params;
2829
next();
2930
})
30-
.forRoutes({ path: '*', method: RequestMethod.GET });
31+
.forRoutes({ path: '*', method: RequestMethod.GET })
32+
.apply((req, res, next) => {
33+
this.count += 1;
34+
req.count = this.count;
35+
next();
36+
})
37+
.forRoutes('*');
3138
}
3239
}

0 commit comments

Comments
 (0)