test: add the middleware get the params from global prefix test

This commit is contained in:
codytseng
2022-10-11 15:33:36 +08:00
parent 75d5d64ec7
commit b7f458f3fe
3 changed files with 23 additions and 1 deletions

View File

@@ -119,6 +119,23 @@ describe('Global prefix', () => {
await request(server).get('/api/v1/middleware/foo').expect(404);
});
it(`should get the params in the global prefix`, async () => {
app.setGlobalPrefix('/api/:version', {
exclude: ['/hello/:name'],
});
server = app.getHttpServer();
await app.init();
await request(server)
.get('/api/v1/middlewareParam')
.expect(200, { '0': 'middlewareParam', version: 'v1' });
await request(server)
.get('/hello/foo')
.expect(200, 'Hello: Data attached in middleware');
});
afterEach(async () => {
await app.close();
});

View File

@@ -21,4 +21,9 @@ export class AppController {
postTest(): string {
return 'test';
}
@Get('middlewareParam')
getMiddlewareParam(@Req() req): string {
return req.extras?.param;
}
}

View File

@@ -19,7 +19,7 @@ export class AppModule {
.apply((req, res, next) => res.status(201).end(MIDDLEWARE_PARAM_VALUE))
.forRoutes({ path: MIDDLEWARE_VALUE + '/*', method: RequestMethod.POST })
.apply((req, res, next) => {
req.extras = { data: 'Data attached in middleware' };
req.extras = { data: 'Data attached in middleware', param: req.params };
next();
})
.forRoutes({ path: '*', method: RequestMethod.GET });