mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
88 lines
2.0 KiB
TypeScript
88 lines
2.0 KiB
TypeScript
import { Controller, Get, MiddlewareConsumer, Module } from '@nestjs/common';
|
|
import {
|
|
FastifyAdapter,
|
|
NestFastifyApplication,
|
|
} from '@nestjs/platform-fastify';
|
|
import { Test } from '@nestjs/testing';
|
|
import { expect } from 'chai';
|
|
import { ApplicationModule } from '../src/app.module';
|
|
|
|
const RETURN_VALUE = 'test';
|
|
const SCOPED_VALUE = 'test_scoped';
|
|
const WILDCARD_VALUE = 'test_wildcard';
|
|
|
|
@Controller()
|
|
class TestController {
|
|
@Get('test')
|
|
test() {
|
|
return RETURN_VALUE;
|
|
}
|
|
|
|
@Get('tests/wildcard_nested')
|
|
// eslint-disable-next-line @typescript-eslint/camelcase
|
|
wildcard_nested() {
|
|
return RETURN_VALUE;
|
|
}
|
|
}
|
|
|
|
@Module({
|
|
imports: [ApplicationModule],
|
|
controllers: [TestController],
|
|
})
|
|
class TestModule {
|
|
configure(consumer: MiddlewareConsumer) {
|
|
consumer
|
|
.apply((req, res, next) => res.end(WILDCARD_VALUE))
|
|
.forRoutes('tests/(.*)')
|
|
.apply((req, res, next) => res.end(SCOPED_VALUE))
|
|
.forRoutes(TestController)
|
|
.apply((req, res, next) => res.end(RETURN_VALUE))
|
|
.forRoutes('(.*)');
|
|
}
|
|
}
|
|
|
|
describe('Middleware (FastifyAdapter)', () => {
|
|
let app: NestFastifyApplication;
|
|
|
|
beforeEach(async () => {
|
|
app = (
|
|
await Test.createTestingModule({
|
|
imports: [TestModule],
|
|
}).compile()
|
|
).createNestApplication<NestFastifyApplication>(new FastifyAdapter());
|
|
|
|
await app.init();
|
|
});
|
|
|
|
it(`forRoutes(*)`, () => {
|
|
return app
|
|
.inject({
|
|
method: 'GET',
|
|
url: '/hello',
|
|
})
|
|
.then(({ payload }) => expect(payload).to.be.eql(RETURN_VALUE));
|
|
});
|
|
|
|
it(`forRoutes(TestController)`, () => {
|
|
return app
|
|
.inject({
|
|
method: 'GET',
|
|
url: '/test',
|
|
})
|
|
.then(({ payload }) => expect(payload).to.be.eql(SCOPED_VALUE));
|
|
});
|
|
|
|
it(`forRoutes(tests/*)`, () => {
|
|
return app
|
|
.inject({
|
|
method: 'GET',
|
|
url: '/tests/wildcard',
|
|
})
|
|
.then(({ payload }) => expect(payload).to.be.eql(WILDCARD_VALUE));
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await app.close();
|
|
});
|
|
});
|