mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
wrap test case for uri versioning context to facilitate the addition of tests for other versioning types
82 lines
1.7 KiB
TypeScript
82 lines
1.7 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
INestApplication,
|
|
MiddlewareConsumer,
|
|
Module,
|
|
Version,
|
|
RequestMethod,
|
|
VersioningType,
|
|
VERSION_NEUTRAL,
|
|
VersioningOptions,
|
|
} from '@nestjs/common';
|
|
import { Test } from '@nestjs/testing';
|
|
import * as request from 'supertest';
|
|
import { AppModule } from '../src/app.module';
|
|
|
|
const RETURN_VALUE = 'test';
|
|
const VERSIONED_VALUE = 'test_versioned';
|
|
|
|
@Controller()
|
|
class TestController {
|
|
@Version('1')
|
|
@Get('versioned')
|
|
versionedTest() {
|
|
return RETURN_VALUE;
|
|
}
|
|
}
|
|
|
|
@Module({
|
|
imports: [AppModule],
|
|
controllers: [TestController],
|
|
})
|
|
class TestModule {
|
|
configure(consumer: MiddlewareConsumer) {
|
|
consumer
|
|
.apply((req, res, next) => res.send(VERSIONED_VALUE))
|
|
.forRoutes({
|
|
path: '/versioned',
|
|
version: '1',
|
|
method: RequestMethod.ALL,
|
|
});
|
|
}
|
|
}
|
|
|
|
describe('Middleware', () => {
|
|
let app: INestApplication;
|
|
|
|
describe('when using URI versioning', () => {
|
|
beforeEach(async () => {
|
|
app = await createAppWithVersioningType(VersioningType.URI);
|
|
});
|
|
|
|
it(`forRoutes({ path: 'tests/versioned', version: '1', method: RequestMethod.ALL })`, () => {
|
|
return request(app.getHttpServer())
|
|
.get('/v1/versioned')
|
|
.expect(200, VERSIONED_VALUE);
|
|
});
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await app.close();
|
|
});
|
|
});
|
|
|
|
async function createAppWithVersioningType(
|
|
versioningType: VersioningType,
|
|
): Promise<INestApplication> {
|
|
const app = (
|
|
await Test.createTestingModule({
|
|
imports: [TestModule],
|
|
}).compile()
|
|
).createNestApplication();
|
|
|
|
app.enableVersioning({
|
|
type: versioningType,
|
|
defaultVersion: VERSION_NEUTRAL,
|
|
} as VersioningOptions);
|
|
await app.init();
|
|
|
|
return app;
|
|
}
|