mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
162 lines
3.5 KiB
TypeScript
162 lines
3.5 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
INestApplication,
|
|
MiddlewareConsumer,
|
|
Module,
|
|
Post,
|
|
RequestMethod,
|
|
} from '@nestjs/common';
|
|
import {
|
|
FastifyAdapter,
|
|
NestFastifyApplication,
|
|
} from '@nestjs/platform-fastify';
|
|
import { Test } from '@nestjs/testing';
|
|
import * as request from 'supertest';
|
|
import { AppModule } from '../src/app.module';
|
|
|
|
const RETURN_VALUE = 'test';
|
|
const MIDDLEWARE_VALUE = 'middleware';
|
|
|
|
@Controller()
|
|
class TestController {
|
|
@Get('test')
|
|
test() {
|
|
return RETURN_VALUE;
|
|
}
|
|
|
|
@Get('test/test')
|
|
testTest() {
|
|
return RETURN_VALUE;
|
|
}
|
|
|
|
@Get('test2')
|
|
test2() {
|
|
return RETURN_VALUE;
|
|
}
|
|
|
|
@Get('middleware')
|
|
middleware() {
|
|
return RETURN_VALUE;
|
|
}
|
|
|
|
@Post('middleware')
|
|
noMiddleware() {
|
|
return RETURN_VALUE;
|
|
}
|
|
|
|
@Get('wildcard/overview')
|
|
testOverview() {
|
|
return RETURN_VALUE;
|
|
}
|
|
|
|
@Get('legacy-wildcard/overview')
|
|
testLegacyWildcard() {
|
|
return RETURN_VALUE;
|
|
}
|
|
|
|
@Get('splat-wildcard/overview')
|
|
testSplatWildcard() {
|
|
return RETURN_VALUE;
|
|
}
|
|
|
|
@Get('overview/:id')
|
|
overviewById() {
|
|
return RETURN_VALUE;
|
|
}
|
|
}
|
|
|
|
@Module({
|
|
imports: [AppModule],
|
|
controllers: [TestController],
|
|
})
|
|
class TestModule {
|
|
configure(consumer: MiddlewareConsumer) {
|
|
consumer
|
|
.apply((req, res, next) => res.end(MIDDLEWARE_VALUE))
|
|
.exclude(
|
|
'test',
|
|
'overview/:id',
|
|
'wildcard/*',
|
|
'legacy-wildcard/(.*)',
|
|
'splat-wildcard/*splat',
|
|
{
|
|
path: 'middleware',
|
|
method: RequestMethod.POST,
|
|
},
|
|
)
|
|
.forRoutes('*');
|
|
}
|
|
}
|
|
|
|
describe('Exclude middleware (fastify)', () => {
|
|
let app: INestApplication;
|
|
|
|
beforeEach(async () => {
|
|
app = (
|
|
await Test.createTestingModule({
|
|
imports: [TestModule],
|
|
}).compile()
|
|
).createNestApplication<NestFastifyApplication>(new FastifyAdapter());
|
|
|
|
await app.init();
|
|
await app.getHttpAdapter().getInstance().ready();
|
|
});
|
|
|
|
it(`should exclude "/test" endpoint`, () => {
|
|
return request(app.getHttpServer()).get('/test').expect(200, RETURN_VALUE);
|
|
});
|
|
|
|
it(`should not exclude "/test/test" endpoint`, () => {
|
|
return request(app.getHttpServer())
|
|
.get('/test/test')
|
|
.expect(200, MIDDLEWARE_VALUE);
|
|
});
|
|
|
|
it(`should not exclude "/test2" endpoint`, () => {
|
|
return request(app.getHttpServer())
|
|
.get('/test2')
|
|
.expect(200, MIDDLEWARE_VALUE);
|
|
});
|
|
|
|
it(`should run middleware for "/middleware" endpoint`, () => {
|
|
return request(app.getHttpServer())
|
|
.get('/middleware')
|
|
.expect(200, MIDDLEWARE_VALUE);
|
|
});
|
|
|
|
it(`should exclude POST "/middleware" endpoint`, () => {
|
|
return request(app.getHttpServer())
|
|
.post('/middleware')
|
|
.expect(201, RETURN_VALUE);
|
|
});
|
|
|
|
it(`should exclude "/overview/:id" endpoint (by param)`, () => {
|
|
return request(app.getHttpServer())
|
|
.get('/overview/1')
|
|
.expect(200, RETURN_VALUE);
|
|
});
|
|
|
|
it(`should exclude "/wildcard/overview" endpoint (by wildcard)`, () => {
|
|
return request(app.getHttpServer())
|
|
.get('/wildcard/overview')
|
|
.expect(200, RETURN_VALUE);
|
|
});
|
|
|
|
it(`should exclude "/legacy-wildcard/overview" endpoint (by wildcard, legacy syntax)`, () => {
|
|
return request(app.getHttpServer())
|
|
.get('/legacy-wildcard/overview')
|
|
.expect(200, RETURN_VALUE);
|
|
});
|
|
|
|
it(`should exclude "/splat-wildcard/overview" endpoint (by wildcard, new syntax)`, () => {
|
|
return request(app.getHttpServer())
|
|
.get('/splat-wildcard/overview')
|
|
.expect(200, RETURN_VALUE);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await app.close();
|
|
});
|
|
});
|