mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
test: add the middleware get the params from global prefix test
This commit is contained in:
@@ -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();
|
||||
});
|
||||
|
||||
@@ -21,4 +21,9 @@ export class AppController {
|
||||
postTest(): string {
|
||||
return 'test';
|
||||
}
|
||||
|
||||
@Get('middlewareParam')
|
||||
getMiddlewareParam(@Req() req): string {
|
||||
return req.extras?.param;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 });
|
||||
|
||||
Reference in New Issue
Block a user